bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/cmd/synsec-cli/parsers.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "bitbucket.org/Aishee/synsec/pkg/cwhub" 7 8 log "github.com/sirupsen/logrus" 9 10 "github.com/spf13/cobra" 11 ) 12 13 func NewParsersCmd() *cobra.Command { 14 var cmdParsers = &cobra.Command{ 15 Use: "parsers [action] [config]", 16 Short: "Install/Remove/Upgrade/Inspect parser(s) from hub", 17 Example: `ccscli parsers install breakteam/sshd-logs 18 ccscli parsers inspect breakteam/sshd-logs 19 ccscli parsers upgrade breakteam/sshd-logs 20 ccscli parsers list 21 ccscli parsers remove breakteam/sshd-logs 22 `, 23 Args: cobra.MinimumNArgs(1), 24 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 25 if err := csConfig.LoadHub(); err != nil { 26 log.Fatalf(err.Error()) 27 } 28 if csConfig.Hub == nil { 29 return fmt.Errorf("you must configure cli before interacting with hub") 30 } 31 32 if err := setHubBranch(); err != nil { 33 return fmt.Errorf("error while setting hub branch: %s", err) 34 } 35 36 if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { 37 log.Fatalf("Failed to get Hub index : %v", err) 38 log.Infoln("Run 'sudo ccscli hub update' to get the hub index") 39 } 40 return nil 41 }, 42 PersistentPostRun: func(cmd *cobra.Command, args []string) { 43 if cmd.Name() == "inspect" || cmd.Name() == "list" { 44 return 45 } 46 log.Infof(ReloadMessage()) 47 }, 48 } 49 50 var cmdParsersInstall = &cobra.Command{ 51 Use: "install [config]", 52 Short: "Install given parser(s)", 53 Long: `Fetch and install given parser(s) from hub`, 54 Example: `ccscli parsers install synsec/xxx synsec/xyz`, 55 Args: cobra.MinimumNArgs(1), 56 Run: func(cmd *cobra.Command, args []string) { 57 for _, name := range args { 58 InstallItem(name, cwhub.PARSERS, forceAction) 59 } 60 }, 61 } 62 cmdParsersInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable") 63 cmdParsersInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files") 64 cmdParsers.AddCommand(cmdParsersInstall) 65 66 var cmdParsersRemove = &cobra.Command{ 67 Use: "remove [config]", 68 Short: "Remove given parser(s)", 69 Long: `Remove given parse(s) from hub`, 70 Example: `ccscli parsers remove synsec/xxx synsec/xyz`, 71 Args: cobra.MinimumNArgs(1), 72 Run: func(cmd *cobra.Command, args []string) { 73 if all { 74 RemoveMany(cwhub.PARSERS, "") 75 } else { 76 for _, name := range args { 77 RemoveMany(cwhub.PARSERS, name) 78 } 79 } 80 }, 81 } 82 cmdParsersRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too") 83 cmdParsersRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files") 84 cmdParsersRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the parsers") 85 cmdParsers.AddCommand(cmdParsersRemove) 86 87 var cmdParsersUpgrade = &cobra.Command{ 88 Use: "upgrade [config]", 89 Short: "Upgrade given parser(s)", 90 Long: `Fetch and upgrade given parser(s) from hub`, 91 Example: `ccscli parsers upgrade synsec/xxx synsec/xyz`, 92 Run: func(cmd *cobra.Command, args []string) { 93 if all { 94 UpgradeConfig(cwhub.PARSERS, "", forceAction) 95 } else { 96 if len(args) == 0 { 97 log.Fatalf("no target parser to upgrade") 98 } 99 for _, name := range args { 100 UpgradeConfig(cwhub.PARSERS, name, forceAction) 101 } 102 } 103 }, 104 } 105 cmdParsersUpgrade.PersistentFlags().BoolVar(&all, "all", false, "Upgrade all the parsers") 106 cmdParsersUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files") 107 cmdParsers.AddCommand(cmdParsersUpgrade) 108 109 var cmdParsersInspect = &cobra.Command{ 110 Use: "inspect [name]", 111 Short: "Inspect given parser", 112 Long: `Inspect given parser`, 113 Example: `ccscli parsers inspect synsec/xxx`, 114 Args: cobra.MinimumNArgs(1), 115 Run: func(cmd *cobra.Command, args []string) { 116 InspectItem(args[0], cwhub.PARSERS) 117 }, 118 } 119 cmdParsersInspect.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url") 120 cmdParsers.AddCommand(cmdParsersInspect) 121 122 var cmdParsersList = &cobra.Command{ 123 Use: "list [name]", 124 Short: "List all parsers or given one", 125 Long: `List all parsers or given one`, 126 Example: `ccscli parsers list 127 ccscli parser list breakteam/xxx`, 128 Run: func(cmd *cobra.Command, args []string) { 129 ListItem(cwhub.PARSERS, args) 130 }, 131 } 132 cmdParsersList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items") 133 cmdParsers.AddCommand(cmdParsersList) 134 135 return cmdParsers 136 }