bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/cmd/synsec-cli/hub.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  	"github.com/spf13/cobra"
    10  )
    11  
    12  func NewHubCmd() *cobra.Command {
    13  	/* ---- HUB COMMAND */
    14  	var cmdHub = &cobra.Command{
    15  		Use:   "hub [action]",
    16  		Short: "Manage Hub",
    17  		Long: `
    18  Hub management
    19  
    20  List/update parsers/scenarios/postoverflows/collections from [Synsec Hub](https://hub.synsec.net).
    21  Hub is manage by ccscli, to get latest hub files from [Synsec Hub](https://hub.synsec.net), you need to update.
    22  		`,
    23  		Example: `
    24  ccscli hub list   # List all installed configurations
    25  ccscli hub update # Download list of available configurations from the hub
    26  		`,
    27  		Args: cobra.ExactArgs(0),
    28  		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    29  			if csConfig.Cscli == nil {
    30  				return fmt.Errorf("you must configure cli before interacting with hub")
    31  			}
    32  
    33  			return nil
    34  		},
    35  	}
    36  	cmdHub.PersistentFlags().StringVarP(&cwhub.HubBranch, "branch", "b", "", "Use given branch from hub")
    37  
    38  	var cmdHubList = &cobra.Command{
    39  		Use:   "list [-a]",
    40  		Short: "List installed configs",
    41  		Args:  cobra.ExactArgs(0),
    42  		Run: func(cmd *cobra.Command, args []string) {
    43  
    44  			if err := csConfig.LoadHub(); err != nil {
    45  				log.Fatalf(err.Error())
    46  			}
    47  			if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
    48  				log.Fatalf("Failed to get Hub index : %v", err)
    49  				log.Infoln("Run 'sudo ccscli hub update' to get the hub index")
    50  			}
    51  			//use LocalSync to get warnings about tainted / outdated items
    52  			_, warn := cwhub.LocalSync(csConfig.Hub)
    53  			for _, v := range warn {
    54  				log.Info(v)
    55  			}
    56  			cwhub.DisplaySummary()
    57  			log.Printf("PARSERS:")
    58  			ListItem(cwhub.PARSERS, args)
    59  			log.Printf("SCENARIOS:")
    60  			ListItem(cwhub.SCENARIOS, args)
    61  			log.Printf("COLLECTIONS:")
    62  			ListItem(cwhub.COLLECTIONS, args)
    63  			log.Printf("POSTOVERFLOWS:")
    64  			ListItem(cwhub.PARSERS_OVFLW, args)
    65  		},
    66  	}
    67  	cmdHubList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List as well disabled items")
    68  	cmdHub.AddCommand(cmdHubList)
    69  
    70  	var cmdHubUpdate = &cobra.Command{
    71  		Use:   "update",
    72  		Short: "Fetch available configs from hub",
    73  		Long: `
    74  Fetches the [.index.json](https://bitbucket.org/Aishee/hub/blob/master/.index.json) file from hub, containing the list of available configs.
    75  `,
    76  		Args: cobra.ExactArgs(0),
    77  		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    78  			if csConfig.Cscli == nil {
    79  				return fmt.Errorf("you must configure cli before interacting with hub")
    80  			}
    81  
    82  			if err := setHubBranch(); err != nil {
    83  				return fmt.Errorf("error while setting hub branch: %s", err)
    84  			}
    85  			return nil
    86  		},
    87  		Run: func(cmd *cobra.Command, args []string) {
    88  			if err := csConfig.LoadHub(); err != nil {
    89  				log.Fatalf(err.Error())
    90  			}
    91  			if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
    92  				log.Fatalf("Failed to get Hub index : %v", err)
    93  			}
    94  			//use LocalSync to get warnings about tainted / outdated items
    95  			_, warn := cwhub.LocalSync(csConfig.Hub)
    96  			for _, v := range warn {
    97  				log.Info(v)
    98  			}
    99  		},
   100  	}
   101  	cmdHub.AddCommand(cmdHubUpdate)
   102  
   103  	var cmdHubUpgrade = &cobra.Command{
   104  		Use:   "upgrade",
   105  		Short: "Upgrade all configs installed from hub",
   106  		Long: `
   107  Upgrade all configs installed from Synsec Hub. Run 'sudo ccscli hub update' if you want the latest versions available.
   108  `,
   109  		Args: cobra.ExactArgs(0),
   110  		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
   111  			if csConfig.Cscli == nil {
   112  				return fmt.Errorf("you must configure cli before interacting with hub")
   113  			}
   114  
   115  			if err := setHubBranch(); err != nil {
   116  				return fmt.Errorf("error while setting hub branch: %s", err)
   117  			}
   118  			return nil
   119  		},
   120  		Run: func(cmd *cobra.Command, args []string) {
   121  			if err := csConfig.LoadHub(); err != nil {
   122  				log.Fatalf(err.Error())
   123  			}
   124  			if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
   125  				log.Fatalf("Failed to get Hub index : %v", err)
   126  				log.Infoln("Run 'sudo ccscli hub update' to get the hub index")
   127  			}
   128  
   129  			log.Infof("Upgrading collections")
   130  			UpgradeConfig(cwhub.COLLECTIONS, "", forceAction)
   131  			log.Infof("Upgrading parsers")
   132  			UpgradeConfig(cwhub.PARSERS, "", forceAction)
   133  			log.Infof("Upgrading scenarios")
   134  			UpgradeConfig(cwhub.SCENARIOS, "", forceAction)
   135  			log.Infof("Upgrading postoverflows")
   136  			UpgradeConfig(cwhub.PARSERS_OVFLW, "", forceAction)
   137  		},
   138  	}
   139  	cmdHubUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
   140  	cmdHub.AddCommand(cmdHubUpgrade)
   141  	return cmdHub
   142  }