aboutsummaryrefslogtreecommitdiff
path: root/lib/config/config.go
blob: ab003cbd9e07f0864d7863144e3c105fd42740da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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" json:"SteamRepositories"`
	DefaultRepository string   `yaml:"DefaultRepository" json:"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()

}