From baa8f9bc45c48519d9b8dd884073ffe79f4a0ee1 Mon Sep 17 00:00:00 2001 From: Toinux Date: Wed, 24 May 2023 18:13:55 +0200 Subject: [PATCH 1/2] Fix RegisterSafe to avoid duplicate facter declarations which leads to facter overwrite --- cmd/facter/imports.go | 2 +- lib/facter/facter.go | 4 +- lib/facters/routehOS/bird.go | 28 ---------- lib/facters/routehOS/bird/bird.go | 59 ++++++++++++++++++++++ lib/facters/routehOS/facter.go | 27 +++++----- lib/facters/routehOS/networking/imports.go | 6 +++ lib/facters/routehOS/{ => radvd}/radvd.go | 25 ++++----- 7 files changed, 94 insertions(+), 57 deletions(-) delete mode 100644 lib/facters/routehOS/bird.go create mode 100644 lib/facters/routehOS/bird/bird.go create mode 100644 lib/facters/routehOS/networking/imports.go rename lib/facters/routehOS/{ => radvd}/radvd.go (52%) diff --git a/cmd/facter/imports.go b/cmd/facter/imports.go index cb04510..1fa867c 100644 --- a/cmd/facter/imports.go +++ b/cmd/facter/imports.go @@ -5,5 +5,5 @@ import ( _ "github.com/KittenConnect/go-facter/lib/facters/mem" _ "github.com/KittenConnect/go-facter/lib/facters/net" - _ "github.com/KittenConnect/go-facter/lib/facters/routehOS" + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/networking" ) diff --git a/lib/facter/facter.go b/lib/facter/facter.go index f695b0b..3c38cf3 100644 --- a/lib/facter/facter.go +++ b/lib/facter/facter.go @@ -7,8 +7,8 @@ import ( "runtime" "github.com/KittenConnect/go-facter/lib/formatter" - "github.com/peterbourgon/mergemap" "github.com/nqd/flat" + "github.com/peterbourgon/mergemap" ) type FetcherFunc func(IFacter) error @@ -68,7 +68,7 @@ func Register(name string, f FetcherFunc) (err error) { func RegisterSafe(name string, facts []string, f FetcherFunc) (err error) { // Ensure multiple providers dont override same fact for _, fact := range facts { - fetcher, ok := registeredFacts[name] + fetcher, ok := registeredFacts[fact] if ok { err = fmt.Errorf("go-facter facter %s already defined as -> %s", fact, describeFunc(fetchers[fetcher])) fmt.Fprintf(os.Stderr, "%s(%s) failed for reason : %s\n", describeFunc(RegisterSafe), describeFunc(f), err) diff --git a/lib/facters/routehOS/bird.go b/lib/facters/routehOS/bird.go deleted file mode 100644 index e8442a5..0000000 --- a/lib/facters/routehOS/bird.go +++ /dev/null @@ -1,28 +0,0 @@ -package routehOS - -import ( - "os/exec" - "regexp" - - "github.com/KittenConnect/go-facter/lib/facter" -) - -var reBirdVersion = regexp.MustCompile(`BIRD version ([\d\.]+)`) - -func GetBirdFacts(f facter.IFacter) error { - cmd := exec.Command("bird", "--version") - - // Run the command and retrieve the output - output, err := cmd.CombinedOutput() - if err != nil { - return err - } - - result := reBirdVersion.FindStringSubmatch(string(output)) - - if len(result) > 0 { - f.Add("bird_version", result[1]) - } - - return nil -} diff --git a/lib/facters/routehOS/bird/bird.go b/lib/facters/routehOS/bird/bird.go new file mode 100644 index 0000000..02b846c --- /dev/null +++ b/lib/facters/routehOS/bird/bird.go @@ -0,0 +1,59 @@ +package routehOS + +import ( + "fmt" + "os/exec" + "regexp" + + "github.com/KittenConnect/go-facter/lib/facter" +) + +var ( + PluginName = "bird" + + reBirdVersion = regexp.MustCompile(`BIRD version ([\d\.]+)`) + reBirdConfig = regexp.MustCompile(`[\s]+-c[\s]+[\w\s]+instead[\w\s]+\n[\s]+([\/-_.\w]+)`) +) + +func init() { + facter.RegisterSafe(PluginName, []string{"bird."}, GetBirdFacts) +} + +func GetBirdFacts(f facter.IFacter) error { + cmd := exec.Command("bird", "--version") + + // Run the command and retrieve the output + output, err := cmd.CombinedOutput() + if err != nil { + return err + } + + versionOut := string(output) + + result := reBirdVersion.FindStringSubmatch(versionOut) + + if len(result) < 1 { + return fmt.Errorf("cannot find [ %s ] in BIRD version output", reBirdVersion.String()) + } + + cmd = exec.Command("bird", "--help") + + // Run the command and retrieve the output + output, err = cmd.CombinedOutput() + if err != nil { + return err + } + + helpOut := string(output) + + f.Add("bird", map[string]interface{}{ + "version": result[1], + "conf_file": reBirdConfig.FindStringSubmatch(helpOut)[1], + + // "pid_file": reRADVDPidFile.FindStringSubmatch(out)[1], + // "log_file": reRADVDLogFile.FindStringSubmatch(out)[1], + // "log_facility": reRADVDSyslogFc.FindStringSubmatch(out)[1], + }) + + return nil +} diff --git a/lib/facters/routehOS/facter.go b/lib/facters/routehOS/facter.go index 66eed3d..ce9e60a 100644 --- a/lib/facters/routehOS/facter.go +++ b/lib/facters/routehOS/facter.go @@ -11,12 +11,12 @@ var PluginName = "routehOS" var fetcherFuncs = []facter.FetcherFunc{ GetFacts, - GetRADVDFacts, - GetBirdFacts, + // GetRADVDFacts, + // GetBirdFacts, } func init() { - facter.RegisterSafe(PluginName, []string{"foo", "bird_", "radvd", "radvd_"}, GetAllFacts) + facter.RegisterSafe(PluginName, []string{"bird.", "radvd."}, GetAllFacts) } func debug(f string, v ...any) { @@ -39,16 +39,15 @@ func GetAllFacts(f facter.IFacter) (e error) { // GetFacts gathers facts related to KittenConnect's RoutehOS Appliance func GetFacts(f facter.IFacter) error { - f.Add("foo", map[string]interface{}{ - "version": "1.0.2", - "bar": "dfjkgdfgdfg", - "baz": "dfkgnldgdfsg", - "test": "gfdsgdfsgdsg", - "tos": "dfjkngbkdfngdfs", - "toz": map[string]interface{}{ - "beep": "boop", - }, - }) - + // f.Add("foo", map[string]interface{}{ + // "version": "1.0.2", + // "bar": "dfjkgdfgdfg", + // "baz": "dfkgnldgdfsg", + // "test": "gfdsgdfsgdsg", + // "tos": "dfjkngbkdfngdfs", + // "toz": map[string]interface{}{ + // "beep": "boop", + // }, + // }) return nil } diff --git a/lib/facters/routehOS/networking/imports.go b/lib/facters/routehOS/networking/imports.go new file mode 100644 index 0000000..5a08b4c --- /dev/null +++ b/lib/facters/routehOS/networking/imports.go @@ -0,0 +1,6 @@ +package networking + +import ( + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/bird" + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/radvd" +) diff --git a/lib/facters/routehOS/radvd.go b/lib/facters/routehOS/radvd/radvd.go similarity index 52% rename from lib/facters/routehOS/radvd.go rename to lib/facters/routehOS/radvd/radvd.go index 950e868..8047408 100644 --- a/lib/facters/routehOS/radvd.go +++ b/lib/facters/routehOS/radvd/radvd.go @@ -8,12 +8,19 @@ import ( "github.com/KittenConnect/go-facter/lib/facter" ) -var reRADVDVersion = regexp.MustCompile(`Version: ([\d\.]+)`) +var ( + PluginName = "radvd" + + reRADVDVersion = regexp.MustCompile(`Version: ([\d\.]+)`) + reRADVDConfig = regexp.MustCompile(`default[\s]+config[\s]+file[\s]+"([\/-_.\w]+)"`) + reRADVDPidFile = regexp.MustCompile(`default[\s]+pidfile[\s]+"([\/-_.\w]+)"`) + reRADVDLogFile = regexp.MustCompile(`default[\s]+logfile[\s]+"([\/-_.\w]+)"`) + reRADVDSyslogFc = regexp.MustCompile(`default[\s]+syslog[\s]+facility[\s]+(\d+)`) +) -var reRADVDConfig = regexp.MustCompile(`default[\s]+config[\s]+file[\s]+"([\/-_.\w]+)"`) -var reRADVDPidFile = regexp.MustCompile(`default[\s]+pidfile[\s]+"([\/-_.\w]+)"`) -var reRADVDLogFile = regexp.MustCompile(`default[\s]+logfile[\s]+"([\/-_.\w]+)"`) -var reRADVDSyslogFc = regexp.MustCompile(`default[\s]+syslog[\s]+facility[\s]+(\d+)`) +func init() { + facter.RegisterSafe(PluginName, []string{"radvd."}, GetRADVDFacts) +} func GetRADVDFacts(f facter.IFacter) error { cmd := exec.Command("radvd", "--version") @@ -30,15 +37,9 @@ func GetRADVDFacts(f facter.IFacter) error { result := reRADVDVersion.FindStringSubmatch(out) if len(result) < 1 { - return fmt.Errorf("Cannot find [ %s ] in RADvd version output", reRADVDVersion.String()) + return fmt.Errorf("cannot find [ %s ] in RADvd version output", reRADVDVersion.String()) } - f.Add("radvd_version", result[1]) - f.Add("radvd_conf_file", reRADVDConfig.FindStringSubmatch(out)[1]) - f.Add("radvd_pid_file", reRADVDPidFile.FindStringSubmatch(out)[1]) - f.Add("radvd_log_file", reRADVDLogFile.FindStringSubmatch(out)[1]) - f.Add("radvd_log_facility", reRADVDSyslogFc.FindStringSubmatch(out)[1]) - f.Add("radvd", map[string]interface{}{ "version": result[1], "conf_file": reRADVDConfig.FindStringSubmatch(out)[1], From d12cb6f26ca78854ada79d8047541016867c4abb Mon Sep 17 00:00:00 2001 From: Toinux Date: Wed, 24 May 2023 18:29:37 +0200 Subject: [PATCH 2/2] cmd support extended mode or more limited one --- cmd/facter/main.go | 10 ++++++---- {cmd/facter => lib/facters/routehOS/base}/imports.go | 4 +--- .../facters/routehOS/base}/imports_darwin.go | 2 +- .../facters/routehOS/base}/imports_linux.go | 2 +- lib/facters/routehOS/extended/imports.go | 5 +++++ .../routehOS/{ => extended/networking}/bird/bird.go | 0 lib/facters/routehOS/extended/networking/imports.go | 6 ++++++ .../routehOS/{ => extended/networking}/radvd/radvd.go | 0 lib/facters/routehOS/facter.go | 4 ++-- lib/facters/routehOS/imports.go | 5 +++++ lib/facters/routehOS/imports_extended.go | 8 ++++++++ lib/facters/routehOS/networking/imports.go | 6 ------ 12 files changed, 35 insertions(+), 17 deletions(-) rename {cmd/facter => lib/facters/routehOS/base}/imports.go (67%) rename {cmd/facter => lib/facters/routehOS/base}/imports_darwin.go (51%) rename {cmd/facter => lib/facters/routehOS/base}/imports_linux.go (90%) create mode 100644 lib/facters/routehOS/extended/imports.go rename lib/facters/routehOS/{ => extended/networking}/bird/bird.go (100%) create mode 100644 lib/facters/routehOS/extended/networking/imports.go rename lib/facters/routehOS/{ => extended/networking}/radvd/radvd.go (100%) create mode 100644 lib/facters/routehOS/imports.go create mode 100644 lib/facters/routehOS/imports_extended.go delete mode 100644 lib/facters/routehOS/networking/imports.go diff --git a/cmd/facter/main.go b/cmd/facter/main.go index 7a57ac2..45e4259 100644 --- a/cmd/facter/main.go +++ b/cmd/facter/main.go @@ -4,9 +4,11 @@ import ( "flag" "fmt" "os" - + "github.com/KittenConnect/go-facter/lib/facter" "github.com/KittenConnect/go-facter/lib/formatter" + + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS" ) func main() { @@ -24,11 +26,11 @@ func main() { // if *ptFormat == true { // conf.Formatter = formatter.NewFormatter() // } else - if *kvFormat == true { + if *kvFormat { conf.Formatter = formatter.NewKeyValueFormatter() - } else if *jsonFormat == true { + } else if *jsonFormat { conf.Formatter = formatter.NewJSONFormatter() - } else if *yamlFormat == true { + } else if *yamlFormat { conf.Formatter = formatter.NewYAMLFormatter() } else { conf.Formatter = formatter.NewFormatter() diff --git a/cmd/facter/imports.go b/lib/facters/routehOS/base/imports.go similarity index 67% rename from cmd/facter/imports.go rename to lib/facters/routehOS/base/imports.go index 1fa867c..d7080f4 100644 --- a/cmd/facter/imports.go +++ b/lib/facters/routehOS/base/imports.go @@ -1,9 +1,7 @@ -package main +package base import ( _ "github.com/KittenConnect/go-facter/lib/facters/disk" _ "github.com/KittenConnect/go-facter/lib/facters/mem" _ "github.com/KittenConnect/go-facter/lib/facters/net" - - _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/networking" ) diff --git a/cmd/facter/imports_darwin.go b/lib/facters/routehOS/base/imports_darwin.go similarity index 51% rename from cmd/facter/imports_darwin.go rename to lib/facters/routehOS/base/imports_darwin.go index 706bd01..784ab54 100644 --- a/cmd/facter/imports_darwin.go +++ b/lib/facters/routehOS/base/imports_darwin.go @@ -1,3 +1,3 @@ -package main +package base // import () diff --git a/cmd/facter/imports_linux.go b/lib/facters/routehOS/base/imports_linux.go similarity index 90% rename from cmd/facter/imports_linux.go rename to lib/facters/routehOS/base/imports_linux.go index 73e6fba..9fd671d 100644 --- a/cmd/facter/imports_linux.go +++ b/lib/facters/routehOS/base/imports_linux.go @@ -1,4 +1,4 @@ -package main +package base import ( _ "github.com/KittenConnect/go-facter/lib/facters/cpu" diff --git a/lib/facters/routehOS/extended/imports.go b/lib/facters/routehOS/extended/imports.go new file mode 100644 index 0000000..2cdb6c6 --- /dev/null +++ b/lib/facters/routehOS/extended/imports.go @@ -0,0 +1,5 @@ +package extended + +import ( + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/extended/networking" +) diff --git a/lib/facters/routehOS/bird/bird.go b/lib/facters/routehOS/extended/networking/bird/bird.go similarity index 100% rename from lib/facters/routehOS/bird/bird.go rename to lib/facters/routehOS/extended/networking/bird/bird.go diff --git a/lib/facters/routehOS/extended/networking/imports.go b/lib/facters/routehOS/extended/networking/imports.go new file mode 100644 index 0000000..af1d6ba --- /dev/null +++ b/lib/facters/routehOS/extended/networking/imports.go @@ -0,0 +1,6 @@ +package networking + +import ( + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/extended/networking/bird" + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/extended/networking/radvd" +) diff --git a/lib/facters/routehOS/radvd/radvd.go b/lib/facters/routehOS/extended/networking/radvd/radvd.go similarity index 100% rename from lib/facters/routehOS/radvd/radvd.go rename to lib/facters/routehOS/extended/networking/radvd/radvd.go diff --git a/lib/facters/routehOS/facter.go b/lib/facters/routehOS/facter.go index ce9e60a..6724f8d 100644 --- a/lib/facters/routehOS/facter.go +++ b/lib/facters/routehOS/facter.go @@ -10,13 +10,13 @@ import ( var PluginName = "routehOS" var fetcherFuncs = []facter.FetcherFunc{ - GetFacts, + // GetFacts, // GetRADVDFacts, // GetBirdFacts, } func init() { - facter.RegisterSafe(PluginName, []string{"bird.", "radvd."}, GetAllFacts) + facter.RegisterSafe(PluginName, []string{}, GetAllFacts) } func debug(f string, v ...any) { diff --git a/lib/facters/routehOS/imports.go b/lib/facters/routehOS/imports.go new file mode 100644 index 0000000..a839dcb --- /dev/null +++ b/lib/facters/routehOS/imports.go @@ -0,0 +1,5 @@ +package routehOS + +import ( + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/base" +) diff --git a/lib/facters/routehOS/imports_extended.go b/lib/facters/routehOS/imports_extended.go new file mode 100644 index 0000000..4938884 --- /dev/null +++ b/lib/facters/routehOS/imports_extended.go @@ -0,0 +1,8 @@ +//go:build facter_extended +// +build facter_extended + +package routehOS + +import ( + _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/extended" +) diff --git a/lib/facters/routehOS/networking/imports.go b/lib/facters/routehOS/networking/imports.go deleted file mode 100644 index 5a08b4c..0000000 --- a/lib/facters/routehOS/networking/imports.go +++ /dev/null @@ -1,6 +0,0 @@ -package networking - -import ( - _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/bird" - _ "github.com/KittenConnect/go-facter/lib/facters/routehOS/radvd" -)