From cda7beddbfc1916c78d0e787f9d6e2adbe69be1b Mon Sep 17 00:00:00 2001 From: "Thibault \"bui\" Koechlin" Date: Wed, 27 May 2020 18:09:23 +0200 Subject: [PATCH] add support in cscli to switch branches of hub (#43) --- cmd/crowdsec-cli/main.go | 5 ++++- pkg/cwhub/hubMgmt.go | 11 ++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 189848be6..8c9095f03 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -117,7 +117,10 @@ API interaction: rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info.") rootCmd.PersistentFlags().BoolVar(&wrn_lvl, "warning", false, "Set logging to warning.") rootCmd.PersistentFlags().BoolVar(&err_lvl, "error", false, "Set logging to error.") - + rootCmd.PersistentFlags().StringVar(&cwhub.HubBranch, "branch", "master", "Override hub branch on github") + if err := rootCmd.PersistentFlags().MarkHidden("branch"); err != nil { + log.Fatalf("failed to make branch hidden : %s", err) + } cobra.OnInitialize(initConfig) /*don't sort flags so we can enforce order*/ rootCmd.Flags().SortFlags = false diff --git a/pkg/cwhub/hubMgmt.go b/pkg/cwhub/hubMgmt.go index daa9ea7b4..b9957d3bc 100644 --- a/pkg/cwhub/hubMgmt.go +++ b/pkg/cwhub/hubMgmt.go @@ -35,8 +35,9 @@ var Installdir = "/etc/crowdsec/" var Hubdir = "/etc/crowdsec/cscli/hub/" var Cfgdir = "/etc/crowdsec/cscli/" -var RawFileURLTemplate = "https://raw.githubusercontent.com/crowdsecurity/hub/master/%s" -var HUB_INDEX_FILE = ".index.json" +var RawFileURLTemplate = "https://raw.githubusercontent.com/crowdsecurity/hub/%s/%s" +var HubIndexFile = ".index.json" +var HubBranch = "master" type ItemVersion struct { Digest string @@ -409,7 +410,7 @@ func UpdateHubIdx() error { } func DownloadHubIdx() ([]byte, error) { - req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, HUB_INDEX_FILE), nil) + req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, HubBranch, HubIndexFile), nil) if err != nil { log.Errorf("failed request : %s", err) return nil, err @@ -421,7 +422,7 @@ func DownloadHubIdx() ([]byte, error) { } if resp.StatusCode != 200 { log.Errorf("got code %d while requesting %s, abort", resp.StatusCode, - fmt.Sprintf(RawFileURLTemplate, HUB_INDEX_FILE)) + fmt.Sprintf(RawFileURLTemplate, HubBranch, HubIndexFile)) return nil, fmt.Errorf("bad http code") } defer resp.Body.Close() @@ -687,7 +688,7 @@ func DownloadItem(target Item, tdir string, overwrite bool, dataFolder string) ( } //log.Infof("Downloading %s to %s", target.Name, tdir) - req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, target.RemotePath), nil) + req, err := http.NewRequest("GET", fmt.Sprintf(RawFileURLTemplate, HubBranch, target.RemotePath), nil) if err != nil { log.Errorf("%s : request creation failed : %s", target.Name, err) return target, err