Skip to content

Commit

Permalink
Merge pull request #1 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 a0f1a16 + 2c333f7 commit 6346cd3
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 21 deletions.
22 changes: 20 additions & 2 deletions cmd/facter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"flag"

"fmt"
"os"

"github.com/KittenConnect/go-facter/lib/facter"
"github.com/KittenConnect/go-facter/lib/formatter"
)
Expand Down Expand Up @@ -33,5 +35,21 @@ func main() {
}

facter := facter.New(&conf)
facter.Print()

args := flag.Args()

if len(args) >= 1 {
out := make(map[string]interface{})
for _, query := range args {
value, ok := facter.Get(query)
if !ok {
fmt.Fprintf(os.Stderr, "%s not found\n", query)
continue
}
out[query] = value
}
conf.Formatter.Print(out)
} else {
facter.Print()
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/nqd/flat v0.2.0
github.com/peterbourgon/mergemap v0.0.1
github.com/shirou/gopsutil v2.0.1-0.20160614092826-22a03b5be3f3+incompatible
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/nqd/flat v0.2.0 h1:g6lXtMxsxrz6PZOO+rNnAJUn/GGRrK4FgVEhy/v+cHI=
github.com/nqd/flat v0.2.0/go.mod h1:FOuslZmNY082wVfVUUb7qAGWKl8z8Nor9FMg+Xj2Nss=
github.com/peterbourgon/mergemap v0.0.1 h1:5/brtSACv34REV0xoYjPQ8JXZnx3nurGt6WInLRwqX4=
github.com/peterbourgon/mergemap v0.0.1/go.mod h1:jQyRpOpE/KbvPc0VKXjAqctYglwUO5W6zAcGcFfbvlo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/shirou/gopsutil v2.0.1-0.20160614092826-22a03b5be3f3+incompatible h1:IWd7gvZ/boLBFxyAEix6pdZVfgeYkCDvhxMkgHqXdD8=
github.com/shirou/gopsutil v2.0.1-0.20160614092826-22a03b5be3f3+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
Expand Down
9 changes: 9 additions & 0 deletions lib/facter/facter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"runtime"

"github.com/KittenConnect/go-facter/lib/formatter"
"github.com/peterbourgon/mergemap"
"github.com/nqd/flat"
)

type FetcherFunc func(IFacter) error
Expand Down Expand Up @@ -115,6 +117,13 @@ func (f *Facter) Fetch() *Facter {
fmt.Fprintf(os.Stderr, "%s() failed for reason : %s\n", describeFunc(fetcher), err)
}
}
// Flatten Facters
flatFacts, err := flat.Flatten(f.facts, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%s() failed for reason : %s\n", describeFunc(flat.Flatten), err)
}

f.facts = mergemap.Merge(f.facts, flatFacts)
return f
}

Expand Down
23 changes: 4 additions & 19 deletions lib/formatter/plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ import (
)

// PlainTextFormatter prints-out facts in k=>v format
type PlainTextFormatter struct {
// indent bool
}
type PlainTextFormatter struct {}

// NewFormatter returns new plain-text formatter
func NewFormatter() *PlainTextFormatter {
return &PlainTextFormatter{
// indent: false,
}
}

func isMap(x interface{}) bool {
t := fmt.Sprintf("%T", x)
return strings.HasPrefix(t, "map[")
return &PlainTextFormatter{}
}

// Print prints-out facts in k=>v format
Expand All @@ -32,7 +23,7 @@ func (pf PlainTextFormatter) PrintIndent(facts map[string]interface{}, prefix, i
sort.Strings(keys)
for _, k := range keys {
v := facts[k]
if isMap(v) {
if strings.HasPrefix(fmt.Sprintf("%T", v), "map[") {
fmt.Printf("%s%v => {\n", prefix, k)
pf.PrintIndent(v.(map[string]interface{}), prefix+indent, indent)
fmt.Printf("%s}\n", prefix)
Expand All @@ -52,16 +43,10 @@ func (pf PlainTextFormatter) Print(facts map[string]interface{}) error {
sort.Strings(keys)
for _, k := range keys {
v := facts[k]
if isMap(v) {
// if pf.indent {
if strings.HasPrefix(fmt.Sprintf("%T", v), "map[") {
fmt.Printf("%v => {\n", k)
pf.PrintIndent(v.(map[string]interface{}), " ", " ")
fmt.Printf("}\n")
// } else {
// fmt.Printf("%v => { ", k)
// pf.Print(v.(map[string]interface{}))
// fmt.Printf(" }")
// }
} else {
fmt.Printf("%v => %v\n", k, v)
}
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/peterbourgon/mergemap/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/peterbourgon/mergemap/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/github.com/peterbourgon/mergemap/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions vendor/github.com/peterbourgon/mergemap/mergemap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ github.com/imdario/mergo
# github.com/nqd/flat v0.2.0
## explicit; go 1.16
github.com/nqd/flat
# github.com/peterbourgon/mergemap v0.0.1
## explicit
github.com/peterbourgon/mergemap
# github.com/shirou/gopsutil v2.0.1-0.20160614092826-22a03b5be3f3+incompatible
## explicit
github.com/shirou/gopsutil/cpu
Expand Down

0 comments on commit 6346cd3

Please sign in to comment.