Skip to content

Commit

Permalink
cscli: extract package 'crowdsec-cli/clientinfo'
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Aug 27, 2024
1 parent f97855e commit 54fe8bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
43 changes: 5 additions & 38 deletions cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/ask"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clientinfo"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
Expand All @@ -27,40 +28,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

type featureflagProvider interface {
GetFeatureflags() string
}

type osProvider interface {
GetOsname() string
GetOsversion() string
}

func getOSNameAndVersion(o osProvider) string {
ret := o.GetOsname()
if o.GetOsversion() != "" {
if ret != "" {
ret += "/"
}

ret += o.GetOsversion()
}

if ret == "" {
return "?"
}

return ret
}

func getFeatureFlagList(o featureflagProvider) []string {
if o.GetFeatureflags() == "" {
return nil
}

return strings.Split(o.GetFeatureflags(), ",")
}

type cliBouncers struct {
db *database.Client
cfg configGetter
Expand Down Expand Up @@ -156,8 +123,8 @@ func newBouncerInfo(b *ent.Bouncer) bouncerInfo {
Version: b.Version,
LastPull: b.LastPull,
AuthType: b.AuthType,
OS: getOSNameAndVersion(b),
Featureflags: getFeatureFlagList(b),
OS: clientinfo.GetOSNameAndVersion(b),
Featureflags: clientinfo.GetFeatureFlagList(b),
}
}

Expand Down Expand Up @@ -463,10 +430,10 @@ func (cli *cliBouncers) inspectHuman(out io.Writer, bouncer *ent.Bouncer) {
{"Version", bouncer.Version},
{"Last Pull", lastPull},
{"Auth type", bouncer.AuthType},
{"OS", getOSNameAndVersion(bouncer)},
{"OS", clientinfo.GetOSNameAndVersion(bouncer)},

Check warning on line 433 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L433

Added line #L433 was not covered by tests
})

for _, ff := range getFeatureFlagList(bouncer) {
for _, ff := range clientinfo.GetFeatureFlagList(bouncer) {

Check warning on line 436 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L436

Added line #L436 was not covered by tests
t.AppendRow(table.Row{"Feature Flags", ff})
}

Expand Down
39 changes: 39 additions & 0 deletions cmd/crowdsec-cli/clientinfo/clientinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package clientinfo

import (
"strings"
)

type featureflagProvider interface {
GetFeatureflags() string
}

type osProvider interface {
GetOsname() string
GetOsversion() string
}

func GetOSNameAndVersion(o osProvider) string {
ret := o.GetOsname()
if o.GetOsversion() != "" {
if ret != "" {
ret += "/"
}

Check warning on line 21 in cmd/crowdsec-cli/clientinfo/clientinfo.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clientinfo/clientinfo.go#L21

Added line #L21 was not covered by tests

ret += o.GetOsversion()
}

if ret == "" {
return "?"
}

Check warning on line 28 in cmd/crowdsec-cli/clientinfo/clientinfo.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clientinfo/clientinfo.go#L28

Added line #L28 was not covered by tests

return ret
}

func GetFeatureFlagList(o featureflagProvider) []string {
if o.GetFeatureflags() == "" {
return nil
}

Check warning on line 36 in cmd/crowdsec-cli/clientinfo/clientinfo.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clientinfo/clientinfo.go#L36

Added line #L36 was not covered by tests

return strings.Split(o.GetFeatureflags(), ",")
}
11 changes: 6 additions & 5 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/ask"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clientinfo"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/idgen"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
Expand Down Expand Up @@ -139,7 +140,7 @@ func (cli *cliMachines) listHuman(out io.Writer, machines ent.Machines) {
hb = emoji.Warning + " " + hb
}

t.AppendRow(table.Row{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, getOSNameAndVersion(m), m.AuthType, hb})
t.AppendRow(table.Row{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, clientinfo.GetOSNameAndVersion(m), m.AuthType, hb})
}

io.WriteString(out, t.Render() + "\n")
Expand Down Expand Up @@ -172,8 +173,8 @@ func newMachineInfo(m *ent.Machine) machineInfo {
Version: m.Version,
IsValidated: m.IsValidated,
AuthType: m.AuthType,
OS: getOSNameAndVersion(m),
Featureflags: getFeatureFlagList(m),
OS: clientinfo.GetOSNameAndVersion(m),
Featureflags: clientinfo.GetFeatureFlagList(m),
Datasources: m.Datasources,
}
}
Expand Down Expand Up @@ -589,15 +590,15 @@ func (cli *cliMachines) inspectHuman(out io.Writer, machine *ent.Machine) {
{"Last Heartbeat", machine.LastHeartbeat},
{"Validated?", machine.IsValidated},
{"CrowdSec version", machine.Version},
{"OS", getOSNameAndVersion(machine)},
{"OS", clientinfo.GetOSNameAndVersion(machine)},

Check warning on line 593 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L593

Added line #L593 was not covered by tests
{"Auth type", machine.AuthType},
})

for dsName, dsCount := range machine.Datasources {
t.AppendRow(table.Row{"Datasources", fmt.Sprintf("%s: %d", dsName, dsCount)})
}

for _, ff := range getFeatureFlagList(machine) {
for _, ff := range clientinfo.GetFeatureFlagList(machine) {

Check warning on line 601 in cmd/crowdsec-cli/machines.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L601

Added line #L601 was not covered by tests
t.AppendRow(table.Row{"Feature Flags", ff})
}

Expand Down

0 comments on commit 54fe8bb

Please sign in to comment.