From 0601c293bf989601846051ec9d27517ce6d8d55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Furug=C3=A5rd?= Date: Wed, 4 Nov 2020 20:55:43 +0100 Subject: [PATCH] basicauth separate, better pos arguments --- cmd/addCluster.go | 21 +++++++++++++++------ cmd/config.go | 5 +++-- cmd/describe.go | 5 +++-- cmd/describeJob.go | 4 +--- cmd/get.go | 6 +++--- cmd/submitJob.go | 4 +--- cmd/useCluster.go | 4 +--- config/new.go | 17 ++++++++++++++--- tools/applyHeaders.go | 9 +++++++++ 9 files changed, 50 insertions(+), 25 deletions(-) diff --git a/cmd/addCluster.go b/cmd/addCluster.go index b2e795e..718ecd6 100644 --- a/cmd/addCluster.go +++ b/cmd/addCluster.go @@ -24,7 +24,11 @@ import ( "github.com/spf13/viper" ) -var headers []string +var ( + headers []string + basicAuthUsername string + basicAuthPassword string +) // addClusterCmd represents the addCluster command var addClusterCmd = &cobra.Command{ @@ -32,18 +36,21 @@ var addClusterCmd = &cobra.Command{ Short: "Add a new cluster to your flinkctl config", Example: `flinkctl config add-cluster https://localhost:123 flinkctl config add-cluster https://localhost:567 --headers="Authorization: Basic Zm9v,Content-Type: application/json"`, + Args: cobra.ExactValidArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return fmt.Errorf("add-cluster requires exactly 1 positional argument, not %v", len(args)) - } - u, err := url.Parse(args[0]) if err != nil { return err } currentConfig := config.Get() - newConfig := config.ClusterConfig{URL: u.String(), Headers: headers} + newConfig := config.ClusterConfig{ + URL: u.String(), + Headers: headers, + BasicAuth: config.BasicAuth{ + Username: basicAuthUsername, + Password: basicAuthPassword}} + if len(currentConfig.Clusters) == 0 { viper.Set("clusters", newConfig) viper.Set("current-cluster", u.String()) @@ -63,4 +70,6 @@ flinkctl config add-cluster https://localhost:567 --headers="Authorization: Basi func init() { configCmd.AddCommand(addClusterCmd) addClusterCmd.Flags().StringSliceVar(&headers, "headers", []string{}, "additional headers to pass when calling this cluster") + addClusterCmd.Flags().StringVar(&basicAuthUsername, "basic-auth-username", "", "") + addClusterCmd.Flags().StringVar(&basicAuthPassword, "basic-auth-password", "", "") } diff --git a/cmd/config.go b/cmd/config.go index 2fe9e9e..a63173c 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -21,8 +21,9 @@ import ( // configCmd represents the config command var configCmd = &cobra.Command{ - Use: "config", - Short: "Alter your flinkctl configuration file", + Use: "config", + Short: "Alter your flinkctl configuration file", + ValidArgs: []string{"add-cluster", "use-cluster"}, } func init() { diff --git a/cmd/describe.go b/cmd/describe.go index 5d16bd4..73b290d 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -20,8 +20,9 @@ import ( ) var describeCmd = &cobra.Command{ - Use: "describe", - Short: "Describe a resource in your cluster, or the cluster itself.", + Use: "describe", + Short: "Describe a resource in your cluster, or the cluster itself.", + ValidArgs: []string{"cluster", "job"}, } func init() { diff --git a/cmd/describeJob.go b/cmd/describeJob.go index 8957bbf..492bdf8 100644 --- a/cmd/describeJob.go +++ b/cmd/describeJob.go @@ -25,10 +25,8 @@ var describeJobCmd = &cobra.Command{ Use: "job ", Short: "Describe a job in your cluster.", PreRun: func(cmd *cobra.Command, args []string) { InitCluster() }, + Args: cobra.ExactValidArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("bad args, got: %v", args) - } for _, jid := range args { if len(jid) != 32 { return fmt.Errorf("%v is not a valid jid", jid) diff --git a/cmd/get.go b/cmd/get.go index 0f58539..ae0b8bb 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -20,9 +20,9 @@ import ( ) var getCmd = &cobra.Command{ - Use: "get", - Short: "Get a resource in your Flink cluster", - PreRun: func(cmd *cobra.Command, args []string) { InitCluster() }, + Use: "get", + Short: "Get a resource in your Flink cluster", + ValidArgs: []string{"jars", "jobs"}, } func init() { diff --git a/cmd/submitJob.go b/cmd/submitJob.go index 2d82c5b..67d6090 100644 --- a/cmd/submitJob.go +++ b/cmd/submitJob.go @@ -57,10 +57,8 @@ var submitJobCmd = &cobra.Command{ Short: "Submit a packaged Flink job to your cluster.", Example: "flinkctl submit job ~/path/to/flinkjob.jar", PreRun: func(cmd *cobra.Command, args []string) { InitCluster() }, + Args: cobra.ExactValidArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("must specify at least one job to submit") - } u := cl.Jars.UploadURL.String() for _, file := range args { diff --git a/cmd/useCluster.go b/cmd/useCluster.go index d7c0a7e..a714d38 100644 --- a/cmd/useCluster.go +++ b/cmd/useCluster.go @@ -28,10 +28,8 @@ var useClusterCmd = &cobra.Command{ Use: "use-cluster ", Short: "Change the currently in-use config for flinkctl", Example: `flinkctl config use-cluster https://localhost:123`, + Args: cobra.ExactValidArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return fmt.Errorf("use-cluster requires exactly one argument") - } url := args[0] if !config.ConfigExists(url) { return fmt.Errorf("no such cluster exists in your config") diff --git a/config/new.go b/config/new.go index 2fb07ff..92c0837 100644 --- a/config/new.go +++ b/config/new.go @@ -15,8 +15,14 @@ type FlinkctlConfig struct { } type ClusterConfig struct { - URL string `yaml:"url"` - Headers []string `yaml:"headers"` + URL string `yaml:"url"` + BasicAuth BasicAuth + Headers []string `yaml:"headers"` +} + +type BasicAuth struct { + Username string + Password string } func Get() *FlinkctlConfig { @@ -36,7 +42,7 @@ func GetCurrent() (*ClusterConfig, error) { return &conf, nil } } - return &ClusterConfig{}, fmt.Errorf("no such cluster: %v", currentCluster) + return &ClusterConfig{}, fmt.Errorf("ERROR: no such cluster: %v", currentCluster) } func ConfigExists(url string) bool { @@ -68,3 +74,8 @@ func GetHeaders() []string { current, _ := GetCurrent() return current.Headers } + +func GetBasicAuth() BasicAuth { + current, _ := GetCurrent() + return current.BasicAuth +} diff --git a/tools/applyHeaders.go b/tools/applyHeaders.go index e7c3612..10b0af8 100644 --- a/tools/applyHeaders.go +++ b/tools/applyHeaders.go @@ -7,6 +7,14 @@ import ( "github.com/parnurzeal/gorequest" ) +func ApplyBasicAuthToRequest(request *gorequest.SuperAgent) *gorequest.SuperAgent { + conf := config.GetBasicAuth() + if len(conf.Username) != 0 && len(conf.Password) != 0 { + request = request.SetBasicAuth(conf.Username, conf.Password) + } + return request +} + func ApplyHeadersToRequest(request *gorequest.SuperAgent) *gorequest.SuperAgent { for _, header := range config.GetHeaders() { parts := strings.Split(header, ": ") @@ -15,5 +23,6 @@ func ApplyHeadersToRequest(request *gorequest.SuperAgent) *gorequest.SuperAgent } request = request.AppendHeader(parts[0], parts[1]) } + request = ApplyBasicAuthToRequest(request) return request }