ace/cmd/config/config.go

75 lines
1.4 KiB
Go
Raw Normal View History

2022-09-26 03:04:07 +00:00
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 {
2022-10-02 11:29:57 +00:00
f, err := os.OpenFile(c.String("config"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
2022-09-26 03:04:07 +00:00
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
}