diff --git a/config.go b/config.go index ceee616..23e0435 100644 --- a/config.go +++ b/config.go @@ -40,6 +40,11 @@ func (m *Repos) ConfigSave() error { } func (m *Repos) ConfigLoad() error { + if os.Getenv("FORGE_HOME") == "" { + homeDir, _ := os.UserHomeDir() + fullpath := filepath.Join(homeDir, ".config/forge") + os.Setenv("FORGE_HOME", fullpath) + } var data []byte var err error if m == nil { @@ -50,7 +55,6 @@ func (m *Repos) ConfigLoad() error { // something went wrong loading the file return err } - if data != nil { // this means the forge.pb file exists and was read if len(data) == 0 { @@ -61,6 +65,7 @@ func (m *Repos) ConfigLoad() error { log.Warn("broken forge.pb config file") return err } + log.Info("config load found", len(m.Repos), "repos") return nil } @@ -80,6 +85,27 @@ func (m *Repos) ConfigLoad() error { log.Warn("broken forge.text config file") return err } + log.Info("config load found", len(m.Repos), "repos") + return nil + } + + // forge.text doesn't exist. try forge.json + // this lets the user hand edit the config + if data, err = loadFile("forge.json"); err != nil { + // something went wrong loading the file + return err + } + + if data != nil { + // this means the forge.text file exists and was read + if len(data) == 0 { + // todo: error out if the file is empty? + } + if err = m.UnmarshalJSON(data); err != nil { + log.Warn("broken forge.json config file") + return err + } + log.Info("config load found", len(m.Repos), "repos") return nil } diff --git a/forgeConfig/Makefile b/forgeConfig/Makefile index 9c34ac1..8c6325d 100644 --- a/forgeConfig/Makefile +++ b/forgeConfig/Makefile @@ -1,8 +1,18 @@ +VERSION = $(shell git describe --tags) +BUILDTIME = $(shell date +%Y.%m.%d) + build: - GO111MODULE=off go build + GO111MODULE=off go build \ + -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" ./forgeConfig FORGE_HOME=/tmp/forge ./forgeConfig +list: + ./forgeConfig --list + +add: + ./forgeConfig --add --name 'foo' --gopath 'go.wit.com/apps/foo' + goimports: goimports -w *.go diff --git a/forgeConfig/argv.go b/forgeConfig/argv.go new file mode 100644 index 0000000..44aff23 --- /dev/null +++ b/forgeConfig/argv.go @@ -0,0 +1,43 @@ +package main + +import ( + "os" + + "github.com/alexflint/go-arg" +) + +var argv args + +type args struct { + ConfigDir string `arg:"env:FORGE_HOME" help:"defaults to ~/.config/forge/"` + List bool `arg:"--list" default:"false" help:"list repos in your config"` + Add bool `arg:"--add" default:"false" help:"add a new repo"` + Name string `arg:"--name" help:"name of the repo"` + GoPath string `arg:"--gopath" help:"gopath of the repo"` +} + +func (a args) Description() string { + return ` + forgeConfig -- add entries to your config files + +This is just example protobuf code to test forgepb is working +but it could be used to automagically create a config file too. + +If you need to change your config file, just edit the forge.text or forge.json +files then remove the forge.pb and ConfigLoad() will attempt to load those files instead +` +} + +func (args) Version() string { + return "virtigo " + VERSION +} + +func init() { + var pp *arg.Parser + pp = arg.MustParse(&argv) + + if pp == nil { + pp.WriteHelp(os.Stdout) + os.Exit(0) + } +} diff --git a/forgeConfig/main.go b/forgeConfig/main.go index 058412d..cdd70cf 100644 --- a/forgeConfig/main.go +++ b/forgeConfig/main.go @@ -8,6 +8,9 @@ import ( "go.wit.com/log" ) +// sent via ldflags +var VERSION string + func main() { var repos *forgepb.Repos repos = new(forgepb.Repos) @@ -15,6 +18,30 @@ func main() { log.Warn("forgepb.ConfigLoad() failed", err) os.Exit(-1) } + if argv.List { + log.Info(forgepb.RepoHeader()) + loop := repos.SortByName() // get the list of droplets + for loop.Scan() { + r := loop.Repo() + log.Info("repo:", r.Name, r.Gopath) + } + os.Exit(0) + } + if argv.Add { + log.Info("going to add a new repo", argv.Name, argv.GoPath) + new1 := new(forgepb.Repo) + new1.Name = argv.Name + new1.Gopath = argv.GoPath + if repos.Append(new1) { + log.Info("added", new1.Name, "ok") + } else { + log.Info("added", new1.Name, "failed") + os.Exit(-1) + } + repos.ConfigSave() + os.Exit(0) + } + testAddRepos(repos) repos.ConfigSave() } diff --git a/human.go b/human.go new file mode 100644 index 0000000..479c6d6 --- /dev/null +++ b/human.go @@ -0,0 +1,12 @@ +package forgepb + +// mostly just functions related to making STDOUT +// more readable by us humans + +// also function shortcuts the do fixed limited formatting (it's like COBOL) +// so reporting tables of the status of what droplets and hypervisors +// are in text columns and rows that can be easily read in a terminal + +func RepoHeader() string { + return "Name Path" +}