From 43d653a8c040d68a4386d1fc1cc7cfa18ce34e83 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 10 Sep 2025 22:35:20 -0500 Subject: [PATCH] add config file support --- config.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 14 +------------- structs.go | 7 ++++--- 3 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..51d0110 --- /dev/null +++ b/config.go @@ -0,0 +1,49 @@ +package main + +// functions to import and export the protobuf +// data to and from config files + +import ( + "errors" + "os" + + "go.wit.com/lib/config" + "go.wit.com/lib/protobuf/forgepb" + "go.wit.com/log" +) + +func configSave() error { + return config.ConfigSave(me.configs) +} + +func (me *mainType) configInit() error { + if argv.Hostname != "" { + HOSTNAME = argv.Hostname + } + + // the default forged dir is /home/forge + if os.Getenv("FORGE_GOSRC") == "" { + os.Setenv("FORGE_GOSRC", "/home/forge") + } + + if os.Getenv("FORGE_PATCHDIR") == "" { + os.Setenv("FORGE_PATCHDIR", "/var/lib/forged") + } + + me.configs = new(forgepb.ForgeConfigs) + err := config.ConfigLoad(me.configs, ARGNAME, "forge") + if errors.Is(err, os.ErrNotExist) { + me.configs.ReposDir = "/home/forge" + me.configs.ReposPB = "/home/forge/repos.pb" + me.configs.PatchDir = "/var/lib/forged" + if err := configSave(); err != nil { + badExit(err) + } + log.Info("made a default config file here", me.configs.Filename) + okExit("") + } + if err != nil { + badExit(err) + } + return err +} diff --git a/main.go b/main.go index 3d601a5..be466a3 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "embed" "fmt" "net/http" - "os" "time" "go.wit.com/dev/alexflint/arg" @@ -31,18 +30,7 @@ func main() { me.myGui = prep.Gui() // prepares the GUI package for go-args me.pp = arg.MustParse(&argv) - if argv.Hostname != "" { - HOSTNAME = argv.Hostname - } - - // the default forged dir is /home/forge - if os.Getenv("FORGE_GOSRC") == "" { - os.Setenv("FORGE_GOSRC", "/home/forge") - } - - if os.Getenv("FORGE_PATCHDIR") == "" { - os.Setenv("FORGE_PATCHDIR", "/var/lib/forged") - } + me.configInit() // reads in the config file me.forge = forgepb.RawInitPB() diff --git a/structs.go b/structs.go index d0c5e30..d21e9f8 100644 --- a/structs.go +++ b/structs.go @@ -13,7 +13,8 @@ var me *mainType // this app's variables type mainType struct { - pp *arg.Parser // for parsing the command line args. Yay to alexf lint! - forge *forgepb.Forge // for holding the forge protobuf files - myGui *prep.GuiPrep // the gui toolkit handle + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! + forge *forgepb.Forge // for holding the forge protobuf files + myGui *prep.GuiPrep // the gui toolkit handle + configs *forgepb.ForgeConfigs // for holding the forge protobuf files }