diff --git a/config/out-of-tree.go b/config/out-of-tree.go new file mode 100644 index 0000000..fafe497 --- /dev/null +++ b/config/out-of-tree.go @@ -0,0 +1,68 @@ +// Copyright 2019 Mikhail Klementev. All rights reserved. +// Use of this source code is governed by a AGPLv3 license +// (or later) that can be found in the LICENSE file. + +package config + +import ( + "os/user" + + "github.com/naoina/toml" +) + +type OutOfTree struct { + Kernels string + UserKernels string + + Database string + + Qemu struct { + Timeout string + } + + Docker struct { + Timeout string + Registry string + } +} + +func ReadOutOfTreeConf(path string) (c OutOfTree, err error) { + buf, err := readFileAll(path) + if err == nil { + err = toml.Unmarshal(buf, &c) + if err != nil { + return + } + } else { + // It's ok if there's no configuration + // then we'll just set default values + err = nil + } + + usr, err := user.Current() + if err != nil { + return + } + + if c.Kernels == "" { + c.Kernels = usr.HomeDir + "/.out-of-tree/kernels.toml" + } + + if c.UserKernels == "" { + c.UserKernels = usr.HomeDir + "/.out-of-tree/kernels.user.toml" + } + + if c.Database == "" { + c.Database = usr.HomeDir + "/.out-of-tree/db.sqlite" + } + + if c.Qemu.Timeout == "" { + c.Qemu.Timeout = "1m" + } + + if c.Docker.Timeout == "" { + c.Docker.Timeout = "1m" + } + + return +} diff --git a/main.go b/main.go index ef7ba4a..e21fb73 100644 --- a/main.go +++ b/main.go @@ -95,25 +95,27 @@ func main() { } os.MkdirAll(usr.HomeDir+"/.out-of-tree", os.ModePerm) - defaultKcfgPath := usr.HomeDir + "/.out-of-tree/kernels.toml" + confPath := usr.HomeDir + "/.out-of-tree/out-of-tree.toml" + conf, err := config.ReadOutOfTreeConf(confPath) + if err != nil { + return + } kcfgPathFlag := app.Flag("kernels", "Path to main kernels config") - kcfgPath := kcfgPathFlag.Default(defaultKcfgPath).String() + kcfgPath := kcfgPathFlag.Default(conf.Kernels).String() - defaultDbPath := usr.HomeDir + "/.out-of-tree/db.sqlite" dbPathFlag := app.Flag("db", "Path to database") - dbPath := dbPathFlag.Default(defaultDbPath).String() + dbPath := dbPathFlag.Default(conf.Database).String() - defaultUserKcfgPath := usr.HomeDir + "/.out-of-tree/kernels.user.toml" userKcfgPathFlag := app.Flag("user-kernels", "User kernels config") userKcfgPathEnv := userKcfgPathFlag.Envar("OUT_OF_TREE_KCFG") - userKcfgPath := userKcfgPathEnv.Default(defaultUserKcfgPath).String() + userKcfgPath := userKcfgPathEnv.Default(conf.UserKernels).String() qemuTimeoutFlag := app.Flag("qemu-timeout", "Timeout for qemu") - qemuTimeout := qemuTimeoutFlag.Default("1m").Duration() + qemuTimeout := qemuTimeoutFlag.Default(conf.Qemu.Timeout).Duration() dockerTimeoutFlag := app.Flag("docker-timeout", "Timeout for docker") - dockerTimeout := dockerTimeoutFlag.Default("1m").Duration() + dockerTimeout := dockerTimeoutFlag.Default(conf.Docker.Timeout).Duration() pewCommand := app.Command("pew", "Build, run and test module/exploit")