Skip to content

Commit

Permalink
Merge pull request #2 from KittenConnect/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
itzwam authored May 24, 2023
2 parents 6346cd3 + d12cb6f commit a4631c5
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 66 deletions.
10 changes: 6 additions & 4 deletions cmd/facter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions lib/facter/facter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main
package base

// import ()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package base

import (
_ "github.com/KittenConnect/go-facter/lib/facters/cpu"
Expand Down
28 changes: 0 additions & 28 deletions lib/facters/routehOS/bird.go

This file was deleted.

5 changes: 5 additions & 0 deletions lib/facters/routehOS/extended/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package extended

import (
_ "github.com/KittenConnect/go-facter/lib/facters/routehOS/extended/networking"
)
59 changes: 59 additions & 0 deletions lib/facters/routehOS/extended/networking/bird/bird.go
Original file line number Diff line number Diff line change
@@ -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]+<config-file>[\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
}
6 changes: 6 additions & 0 deletions lib/facters/routehOS/extended/networking/imports.go
Original file line number Diff line number Diff line change
@@ -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"
)
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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],
Expand Down
29 changes: 14 additions & 15 deletions lib/facters/routehOS/facter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
var PluginName = "routehOS"

var fetcherFuncs = []facter.FetcherFunc{
GetFacts,
GetRADVDFacts,
GetBirdFacts,
// GetFacts,
// GetRADVDFacts,
// GetBirdFacts,
}

func init() {
facter.RegisterSafe(PluginName, []string{"foo", "bird_", "radvd", "radvd_"}, GetAllFacts)
facter.RegisterSafe(PluginName, []string{}, GetAllFacts)
}

func debug(f string, v ...any) {
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions lib/facters/routehOS/imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package routehOS

import (
_ "github.com/KittenConnect/go-facter/lib/facters/routehOS/base"
)
8 changes: 8 additions & 0 deletions lib/facters/routehOS/imports_extended.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build facter_extended
// +build facter_extended

package routehOS

import (
_ "github.com/KittenConnect/go-facter/lib/facters/routehOS/extended"
)

0 comments on commit a4631c5

Please sign in to comment.