Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cscli refact: extract packages ask, clientinfo #3197

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/crowdsec-cli/ask/ask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ask

import (
"github.com/AlecAivazis/survey/v2"
)

func YesNo(message string, defaultAnswer bool) (bool, error) {
var answer bool

prompt := &survey.Confirm{
Message: message,
Default: defaultAnswer,
}

if err := survey.AskOne(prompt, &answer); err != nil {
return defaultAnswer, err
}

Check warning on line 17 in cmd/crowdsec-cli/ask/ask.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/ask/ask.go#L7-L17

Added lines #L7 - L17 were not covered by tests

return answer, nil

Check warning on line 19 in cmd/crowdsec-cli/ask/ask.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/ask/ask.go#L19

Added line #L19 was not covered by tests
}
64 changes: 8 additions & 56 deletions cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"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,55 +28,6 @@
"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(), ",")
}

func askYesNo(message string, defaultAnswer bool) (bool, error) {
var answer bool

prompt := &survey.Confirm{
Message: message,
Default: defaultAnswer,
}

if err := survey.AskOne(prompt, &answer); err != nil {
return defaultAnswer, err
}

return answer, nil
}

type cliBouncers struct {
db *database.Client
cfg configGetter
Expand Down Expand Up @@ -171,8 +123,8 @@
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 @@ -385,7 +337,7 @@

func (cli *cliBouncers) prune(duration time.Duration, force bool) error {
if duration < 2*time.Minute {
if yes, err := askYesNo(
if yes, err := ask.YesNo(

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L340

Added line #L340 was not covered by tests
"The duration you provided is less than 2 minutes. "+
"This may remove active bouncers. Continue?", false); err != nil {
return err
Expand All @@ -408,7 +360,7 @@
cli.listHuman(color.Output, bouncers)

if !force {
if yes, err := askYesNo(
if yes, err := ask.YesNo(

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L363

Added line #L363 was not covered by tests
"You are about to PERMANENTLY remove the above bouncers from the database. "+
"These will NOT be recoverable. Continue?", false); err != nil {
return err
Expand Down Expand Up @@ -478,10 +430,10 @@
{"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(), ",")
}
16 changes: 9 additions & 7 deletions cmd/crowdsec-cli/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"github.com/spf13/cobra"
"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 @@ -138,7 +140,7 @@
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 @@ -171,8 +173,8 @@
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 @@ -466,7 +468,7 @@

func (cli *cliMachines) prune(duration time.Duration, notValidOnly bool, force bool) error {
if duration < 2*time.Minute && !notValidOnly {
if yes, err := askYesNo(
if yes, err := ask.YesNo(

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L471

Added line #L471 was not covered by tests
"The duration you provided is less than 2 minutes. "+
"This can break installations if the machines are only temporarily disconnected. Continue?", false); err != nil {
return err
Expand Down Expand Up @@ -495,7 +497,7 @@
cli.listHuman(color.Output, machines)

if !force {
if yes, err := askYesNo(
if yes, err := ask.YesNo(

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/machines.go#L500

Added line #L500 was not covered by tests
"You are about to PERMANENTLY remove the above machines from the database. "+
"These will NOT be recoverable. Continue?", false); err != nil {
return err
Expand Down Expand Up @@ -588,15 +590,15 @@
{"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