diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2017-02-04 18:16:22 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2017-02-04 18:16:22 -0500 |
| commit | 0e346bf5ad4852db5db82343b2aca5911c38ad00 (patch) | |
| tree | 5a2528211318da0c550dfb5ca576a0292ed8176b /lib/config | |
| parent | 41752a0cf50735bc29dae18271539719f7b59f7c (diff) | |
| download | steam-export-0e346bf5ad4852db5db82343b2aca5911c38ad00.tar.gz steam-export-0e346bf5ad4852db5db82343b2aca5911c38ad00.tar.xz | |
Moved the libraries around
Diffstat (limited to 'lib/config')
| -rw-r--r-- | lib/config/config.go | 89 | ||||
| -rw-r--r-- | lib/config/config_unix.go | 21 | ||||
| -rw-r--r-- | lib/config/config_windows.go | 22 |
3 files changed, 132 insertions, 0 deletions
diff --git a/lib/config/config.go b/lib/config/config.go new file mode 100644 index 0000000..839acbf --- /dev/null +++ b/lib/config/config.go @@ -0,0 +1,89 @@ +// Used to define and load this application's specific configuration +package config + +import ( + "gopkg.in/yaml.v2" + "io/ioutil" + "os" + "os/exec" +) + +type Config struct { + SteamRepositories []string `yaml:"SteamRepositories"` + DefaultRepository string `yaml:"DefaultRepository"` + Listen string +} + +var configFilename string = "steam-export.yml" + +// By default it reads 'config.yml' in the current directory +func LoadConfig() (*Config, error) { + c := &Config{} + err := c.ReadDefaultConfig() + return c, err +} + +func (c *Config) ReadConfig(cfg string) error { + contents, err := ioutil.ReadFile(cfg) + if err != nil { + return err + } + return yaml.Unmarshal([]byte(contents), c) +} + +func checkConfigFile(cf string) error { + // Check if our config file exists, or create it + if _, err := os.Stat(cf); err != nil { + if f, err := os.Create(cf); err != nil { + return err + } else { + f.WriteString(` +--- +listen: 0.0.0.0:9413 +# Repositories that you want to expose on the web server +SteamRepositories: + # We're ideally looking for the full path to the steamapps folder + # On windows you're going to escape the slashes + - "C:\\Program Files (x86)\\Steam\\steamapps" + # - "/usr/mitch/SteamGames/steamapps" + +# This defaults to: +# Only change this if your default Steam library is different than the Steam default +# DefaultRepository: "/usr/mitch/SteamGames/steamapps" +# DefaultRepository: "Z:\\SteamGames\\steamapps" +`) + f.Close() + } + } + return nil +} + +func (c *Config) ReadDefaultConfig() error { + if err := c.ReadConfig(configFilename); err != nil { + if err = checkConfigFile(fn); err != nil { + return err + } + return c.ReadConfig(fn) + } + return nil +} + +func EditDefaultConfig(e string) error { + if e != "" { + editor = e + } + + if _, err := os.Stat(configFilename); err != nil { + if _, err := os.Stat(fn); err != nil { + return err + } + c := exec.Command(editor, fn) + c.Stdout = os.Stdout + c.Stdin = os.Stdin + c.Stderr = os.Stderr + return c.Run() + } + cmd := exec.Command(editor, configFilename) + return cmd.Run() + +} diff --git a/lib/config/config_unix.go b/lib/config/config_unix.go new file mode 100644 index 0000000..8be1967 --- /dev/null +++ b/lib/config/config_unix.go @@ -0,0 +1,21 @@ +// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris + +package config + +import ( + "os" + "path/filepath" +) + +var ( + editor string = setEditor() + fn string = filepath.Join(os.Getenv("HOME"), "."+configFilename) +) + +func setEditor() string { + e := os.Getenv("EDITOR") + if e == "" { + return "vi" + } + return e +} diff --git a/lib/config/config_windows.go b/lib/config/config_windows.go new file mode 100644 index 0000000..39b6b6c --- /dev/null +++ b/lib/config/config_windows.go @@ -0,0 +1,22 @@ +// +build windows + +package config + +import ( + "os" + // "os/exec" + "path/filepath" +) + +var ( + editor string = setEditor() + fn string = filepath.Join(os.Getenv("LOCALAPPDATA"), configFilename) +) + +func setEditor() string { + e := os.Getenv("EDITOR") + if e == "" { + return `C:\Program Files\Windows NT\Accessories\wordpad.exe` + } + return e +} |
