75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
|
||
|
"git.sense-t.eu.org/ACE/ace/servers/qemuserver"
|
||
|
"git.sense-t.eu.org/ACE/ace/servers/webserver"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
"gopkg.in/yaml.v3"
|
||
|
)
|
||
|
|
||
|
var Command *cli.Command
|
||
|
|
||
|
type Config struct {
|
||
|
Debug bool `yaml:"debug"`
|
||
|
WEBServer *webserver.Options `yaml:"webserver"`
|
||
|
Qemu *qemuserver.Options `yaml:"qemu"`
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
Command = &cli.Command{
|
||
|
Name: "config",
|
||
|
Usage: "config file options",
|
||
|
Aliases: []string{"conf", "cfg"},
|
||
|
Subcommands: []*cli.Command{
|
||
|
{
|
||
|
Name: "generate",
|
||
|
Usage: "generate config file",
|
||
|
Action: genconf,
|
||
|
Aliases: []string{"gen"},
|
||
|
},
|
||
|
{
|
||
|
Name: "check",
|
||
|
Usage: "check config file",
|
||
|
Action: checkconf,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewConfig() *Config {
|
||
|
return &Config{
|
||
|
Debug: true,
|
||
|
WEBServer: webserver.ExampleOptions(),
|
||
|
Qemu: qemuserver.ExampleOptions(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func genconf(c *cli.Context) error {
|
||
|
f, err := os.OpenFile(c.String("config"), os.O_CREATE|os.O_WRONLY, 0644)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
return yaml.NewEncoder(f).Encode(NewConfig())
|
||
|
}
|
||
|
|
||
|
func checkconf(c *cli.Context) error {
|
||
|
_, err := ReadConfig(c)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func ReadConfig(c *cli.Context) (*Config, error) {
|
||
|
f, err := os.OpenFile(c.String("config"), os.O_RDONLY, 0644)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
config := &Config{}
|
||
|
err = yaml.NewDecoder(f).Decode(config)
|
||
|
return config, err
|
||
|
}
|