bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/cmd/synsec-cli/collections.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 NewCollectionsCmd() *cobra.Command { 14 var cmdCollections = &cobra.Command{ 15 Use: "collections [action]", 16 Short: "Manage collections from hub", 17 Long: `Install/Remove/Upgrade/Inspect collections from the SynSec Hub.`, 18 /*TBD fix help*/ 19 Args: cobra.MinimumNArgs(1), 20 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 21 if err := csConfig.LoadHub(); err != nil { 22 log.Fatalf(err.Error()) 23 } 24 if csConfig.Hub == nil { 25 return fmt.Errorf("you must configure cli before interacting with hub") 26 } 27 28 if err := setHubBranch(); err != nil { 29 return fmt.Errorf("error while setting hub branch: %s", err) 30 } 31 32 if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { 33 log.Fatalf("Failed to get Hub index : %v", err) 34 log.Infoln("Run 'sudo ccscli hub update' to get the hub index") 35 } 36 37 return nil 38 }, 39 PersistentPostRun: func(cmd *cobra.Command, args []string) { 40 if cmd.Name() == "inspect" || cmd.Name() == "list" { 41 return 42 } 43 log.Infof(ReloadMessage()) 44 }, 45 } 46 47 var cmdCollectionsInstall = &cobra.Command{ 48 Use: "install collection", 49 Short: "Install given collection(s)", 50 Long: `Fetch and install given collection(s) from hub`, 51 Example: `ccscli collections install synsec/xxx synsec/xyz`, 52 Args: cobra.MinimumNArgs(1), 53 Run: func(cmd *cobra.Command, args []string) { 54 for _, name := range args { 55 InstallItem(name, cwhub.COLLECTIONS, forceAction) 56 } 57 }, 58 } 59 cmdCollectionsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable") 60 cmdCollectionsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files") 61 cmdCollections.AddCommand(cmdCollectionsInstall) 62 63 var cmdCollectionsRemove = &cobra.Command{ 64 Use: "remove collection", 65 Short: "Remove given collection(s)", 66 Long: `Remove given collection(s) from hub`, 67 Example: `ccscli collections remove synsec/xxx synsec/xyz`, 68 Args: cobra.MinimumNArgs(1), 69 Run: func(cmd *cobra.Command, args []string) { 70 if all { 71 RemoveMany(cwhub.COLLECTIONS, "") 72 } else { 73 for _, name := range args { 74 if !forceAction { 75 item := cwhub.GetItem(cwhub.COLLECTIONS, name) 76 if len(item.BelongsToCollections) > 0 { 77 log.Warningf("%s belongs to other collections :\n%s\n", name, item.BelongsToCollections) 78 log.Printf("Run 'sudo ccscli collections remove %s --force' if you want to force remove this sub collection\n", name) 79 continue 80 } 81 } 82 RemoveMany(cwhub.COLLECTIONS, name) 83 } 84 } 85 }, 86 } 87 cmdCollectionsRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too") 88 cmdCollectionsRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files") 89 cmdCollectionsRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the collections") 90 cmdCollections.AddCommand(cmdCollectionsRemove) 91 92 var cmdCollectionsUpgrade = &cobra.Command{ 93 Use: "upgrade collection", 94 Short: "Upgrade given collection(s)", 95 Long: `Fetch and upgrade given collection(s) from hub`, 96 Example: `ccscli collections upgrade synsec/xxx synsec/xyz`, 97 Run: func(cmd *cobra.Command, args []string) { 98 if all { 99 UpgradeConfig(cwhub.COLLECTIONS, "", forceAction) 100 } else { 101 if len(args) == 0 { 102 log.Fatalf("no target collection to upgrade") 103 } 104 for _, name := range args { 105 UpgradeConfig(cwhub.COLLECTIONS, name, forceAction) 106 } 107 } 108 }, 109 } 110 cmdCollectionsUpgrade.PersistentFlags().BoolVarP(&all, "all", "a", false, "Upgrade all the collections") 111 cmdCollectionsUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files") 112 cmdCollections.AddCommand(cmdCollectionsUpgrade) 113 114 var cmdCollectionsInspect = &cobra.Command{ 115 Use: "inspect collection", 116 Short: "Inspect given collection", 117 Long: `Inspect given collection`, 118 Example: `ccscli collections inspect synsec/xxx synsec/xyz`, 119 Args: cobra.MinimumNArgs(1), 120 Run: func(cmd *cobra.Command, args []string) { 121 for _, name := range args { 122 InspectItem(name, cwhub.COLLECTIONS) 123 } 124 }, 125 } 126 cmdCollectionsInspect.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url") 127 cmdCollections.AddCommand(cmdCollectionsInspect) 128 129 var cmdCollectionsList = &cobra.Command{ 130 Use: "list collection [-a]", 131 Short: "List all collections or given one", 132 Long: `List all collections or given one`, 133 Example: `ccscli collections list`, 134 Args: cobra.ExactArgs(0), 135 Run: func(cmd *cobra.Command, args []string) { 136 ListItem(cwhub.COLLECTIONS, args) 137 }, 138 } 139 cmdCollectionsList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items") 140 cmdCollections.AddCommand(cmdCollectionsList) 141 142 return cmdCollections 143 }