From 930e7e166ab2ced9c227b31086e15ee3ea0eb828 Mon Sep 17 00:00:00 2001 From: Dalton Hubble Date: Sun, 5 May 2024 06:41:43 -0700 Subject: [PATCH] Remove dependency on github.com/coreos/pkg * Remove dependency on github.com/coreos/pkg because the module is no longer maintained and it doesn't follow the usual release conventions * The last time flagutils was touched as 9 years ago by me. Bring the function into the matchbox repo --- cmd/matchbox/flakgutil.go | 33 +++++++++++++++++++++++++++++++++ cmd/matchbox/main.go | 3 +-- go.mod | 1 - go.sum | 2 -- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 cmd/matchbox/flakgutil.go diff --git a/cmd/matchbox/flakgutil.go b/cmd/matchbox/flakgutil.go new file mode 100644 index 00000000..fd685f64 --- /dev/null +++ b/cmd/matchbox/flakgutil.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "fmt" + "os" + "strings" +) + +// SetFlagsFromEnv parses all registered flags in the given flagset, +// and if they are not already set it attempts to set their values from +// environment variables. Environment variables take the name of the flag but +// are UPPERCASE, and any dashes are replaced by underscores. Environment +// variables additionally are prefixed by the given string followed by +// and underscore. For example, if prefix=PREFIX: some-flag => PREFIX_SOME_FLAG +func SetFlagsFromEnv(fs *flag.FlagSet, prefix string) (err error) { + alreadySet := make(map[string]bool) + fs.Visit(func(f *flag.Flag) { + alreadySet[f.Name] = true + }) + fs.VisitAll(func(f *flag.Flag) { + if !alreadySet[f.Name] { + key := prefix + "_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1)) + val := os.Getenv(key) + if val != "" { + if serr := fs.Set(f.Name, val); serr != nil { + err = fmt.Errorf("invalid value %q for %s: %v", val, key, serr) + } + } + } + }) + return err +} diff --git a/cmd/matchbox/main.go b/cmd/matchbox/main.go index 5d293594..d2469c3b 100644 --- a/cmd/matchbox/main.go +++ b/cmd/matchbox/main.go @@ -7,7 +7,6 @@ import ( "net/http" "os" - "github.com/coreos/pkg/flagutil" web "github.com/poseidon/matchbox/matchbox/http" "github.com/poseidon/matchbox/matchbox/rpc" "github.com/poseidon/matchbox/matchbox/server" @@ -69,7 +68,7 @@ func main() { // parse command-line and environment variable arguments flag.Parse() - if err := flagutil.SetFlagsFromEnv(flag.CommandLine, "MATCHBOX"); err != nil { + if err := SetFlagsFromEnv(flag.CommandLine, "MATCHBOX"); err != nil { log.Fatal(err.Error()) } // restrict OpenPGP passphrase to pass via environment variable only diff --git a/go.mod b/go.mod index 9c4f3f92..7de43167 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/coreos/butane v0.20.0 github.com/coreos/coreos-cloudinit v1.14.0 github.com/coreos/ignition/v2 v2.18.0 - github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf github.com/golang/protobuf v1.5.4 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 diff --git a/go.sum b/go.sum index e1a62099..3e24b981 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,6 @@ github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8 github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/ignition/v2 v2.18.0 h1:sPSGGsxaCuFMpKOMBQ71I9RIR20SIF4dWnoTomcPEYQ= github.com/coreos/ignition/v2 v2.18.0/go.mod h1:TURPHDqWUWTmej8c+CEMBENMU3N/Lt6GfreHJuoDMbA= -github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf h1:GOPo6vn/vTN+3IwZBvXX0y5doJfSC7My0cdzelyOCsQ= -github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/coreos/vcontext v0.0.0-20230201181013-d72178a18687 h1:uSmlDgJGbUB0bwQBcZomBTottKwEDF5fF8UjSwKSzWM= github.com/coreos/vcontext v0.0.0-20230201181013-d72178a18687/go.mod h1:Salmysdw7DAVuobBW/LwsKKgpyCPHUhjyJoMJD+ZJiI= github.com/coreos/yaml v0.0.0-20141224210557-6b16a5714269 h1:/1sjrpK5Mb6IwyFOKd+u7321tXfNAsj0Ci8CivZmSlo=