diff --git a/cmd/crowdsec-cli/bouncers.go b/cmd/crowdsec-cli/bouncers.go index a67ff9b6f..eadf245b9 100644 --- a/cmd/crowdsec-cli/bouncers.go +++ b/cmd/crowdsec-cli/bouncers.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "strings" "time" "github.com/fatih/color" @@ -153,6 +154,25 @@ cscli bouncers add MyBouncerName -k %s`, generatePassword(32)), Args: cobra.MinimumNArgs(1), Aliases: []string{"remove"}, DisableAutoGenTag: true, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var err error + dbClient, err = getDBClient() + if err != nil { + cobra.CompError("unable to create new database client: " + err.Error()) + return nil, cobra.ShellCompDirectiveNoFileComp + } + bouncers, err := dbClient.ListBouncers() + if err != nil { + cobra.CompError("unable to list bouncers " + err.Error()) + } + ret := make([]string, 0) + for _, bouncer := range bouncers { + if strings.Contains(bouncer.Name, toComplete) && !inSlice(bouncer.Name, args) { + ret = append(ret, bouncer.Name) + } + } + return ret, cobra.ShellCompDirectiveNoFileComp + }, Run: func(cmd *cobra.Command, args []string) { for _, bouncerID := range args { err := dbClient.DeleteBouncer(bouncerID) diff --git a/cmd/crowdsec-cli/machines.go b/cmd/crowdsec-cli/machines.go index c327b7935..f61844999 100644 --- a/cmd/crowdsec-cli/machines.go +++ b/cmd/crowdsec-cli/machines.go @@ -311,6 +311,25 @@ cscli machines add MyTestMachine --password MyPassword log.Fatalf("unable to create new database client: %s", err) } }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var err error + dbClient, err = getDBClient() + if err != nil { + cobra.CompError("unable to create new database client: " + err.Error()) + return nil, cobra.ShellCompDirectiveNoFileComp + } + machines, err := dbClient.ListMachines() + if err != nil { + cobra.CompError("unable to list machines " + err.Error()) + } + ret := make([]string, 0) + for _, machine := range machines { + if strings.Contains(machine.MachineId, toComplete) && !inSlice(machine.MachineId, args) { + ret = append(ret, machine.MachineId) + } + } + return ret, cobra.ShellCompDirectiveNoFileComp + }, Run: func(cmd *cobra.Command, args []string) { machineID = args[0] for _, machineID := range args { diff --git a/cmd/crowdsec-cli/utils.go b/cmd/crowdsec-cli/utils.go index 89c20f69b..00a7b21f7 100644 --- a/cmd/crowdsec-cli/utils.go +++ b/cmd/crowdsec-cli/utils.go @@ -22,6 +22,7 @@ import ( "gopkg.in/yaml.v2" "github.com/crowdsecurity/crowdsec/pkg/cwhub" + "github.com/crowdsecurity/crowdsec/pkg/database" "github.com/crowdsecurity/crowdsec/pkg/types" ) @@ -732,3 +733,18 @@ func formatNumber(num int) string { res := math.Round(float64(num)/float64(goodUnit.value)*100) / 100 return fmt.Sprintf("%.2f%s", res, goodUnit.symbol) } + +func getDBClient() (*database.Client, error) { + var err error + if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI { + return nil, err + } + if err := csConfig.LoadDBConfig(); err != nil { + return nil, err + } + ret, err := database.NewClient(csConfig.DbConfig) + if err != nil { + return nil, err + } + return ret, nil +} diff --git a/go.sum b/go.sum index 152b73d30..f44ef9c13 100644 --- a/go.sum +++ b/go.sum @@ -563,6 +563,7 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9 github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=