Skip to content

Commit

Permalink
cscli hub: flags as closure variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 31, 2024
1 parent 8a9365c commit a9f433b
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions cmd/crowdsec-cli/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewCLIHub(getconfig func() *csconfig.Config) *cliHub {
}
}

func (cli cliHub) NewCommand() *cobra.Command {
func (cli *cliHub) NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "hub [action]",
Short: "Manage hub index",
Expand All @@ -47,14 +47,7 @@ cscli hub upgrade`,
return cmd
}

func (cli cliHub) list(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()

all, err := flags.GetBool("all")
if err != nil {
return err
}

func (cli *cliHub) list(all bool) error {
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
if err != nil {
return err
Expand Down Expand Up @@ -85,22 +78,26 @@ func (cli cliHub) list(cmd *cobra.Command, args []string) error {
return nil
}

func (cli cliHub) newListCmd() *cobra.Command {
func (cli *cliHub) newListCmd() *cobra.Command {
var all bool

cmd := &cobra.Command{
Use: "list [-a]",
Short: "List all installed configurations",
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: cli.list,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.list(all)
},
}

flags := cmd.Flags()
flags.BoolP("all", "a", false, "List disabled items as well")
flags.BoolVarP(&all, "all", "a", false, "List disabled items as well")

return cmd
}

func (cli cliHub) update(cmd *cobra.Command, args []string) error {
func (cli *cliHub) update() error {
local := cli.cfg().Hub
remote := require.RemoteHub(cli.cfg())

Expand All @@ -117,7 +114,7 @@ func (cli cliHub) update(cmd *cobra.Command, args []string) error {
return nil
}

func (cli cliHub) newUpdateCmd() *cobra.Command {
func (cli *cliHub) newUpdateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Download the latest index (catalog of available configurations)",
Expand All @@ -126,20 +123,15 @@ Fetches the .index.json file from the hub, containing the list of available conf
`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: cli.update,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.update()
},
}

return cmd
}

func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()

force, err := flags.GetBool("force")
if err != nil {
return err
}

func (cli *cliHub) upgrade(force bool) error {
hub, err := require.Hub(cli.cfg(), require.RemoteHub(cli.cfg()), log.StandardLogger())
if err != nil {
return err
Expand Down Expand Up @@ -172,7 +164,9 @@ func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error {
return nil
}

func (cli cliHub) newUpgradeCmd() *cobra.Command {
func (cli *cliHub) newUpgradeCmd() *cobra.Command {
var force bool

cmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade all configurations to their latest version",
Expand All @@ -181,16 +175,18 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if
`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: cli.upgrade,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.upgrade(force)
},
}

flags := cmd.Flags()
flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
flags.BoolVar(&force, "force", false, "Force upgrade: overwrite tainted and outdated files")

return cmd
}

func (cli cliHub) types(cmd *cobra.Command, args []string) error {
func (cli *cliHub) types() error {
switch cli.cfg().Cscli.Output {
case "human":
s, err := yaml.Marshal(cwhub.ItemTypes)
Expand All @@ -215,7 +211,7 @@ func (cli cliHub) types(cmd *cobra.Command, args []string) error {
return nil
}

func (cli cliHub) newTypesCmd() *cobra.Command {
func (cli *cliHub) newTypesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "types",
Short: "List supported item types",
Expand All @@ -224,7 +220,9 @@ List the types of supported hub items.
`,
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: cli.types,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.types()
},
}

return cmd
Expand Down

0 comments on commit a9f433b

Please sign in to comment.