Skip to content

Commit

Permalink
basicauth separate, better pos arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
praktiskt committed Nov 4, 2020
1 parent fac048d commit 0601c29
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 25 deletions.
21 changes: 15 additions & 6 deletions cmd/addCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,33 @@ import (
"github.com/spf13/viper"
)

var headers []string
var (
headers []string
basicAuthUsername string
basicAuthPassword string
)

// addClusterCmd represents the addCluster command
var addClusterCmd = &cobra.Command{
Use: "add-cluster <url:port>",
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())
Expand All @@ -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", "", "")
}
5 changes: 3 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions cmd/describeJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ var describeJobCmd = &cobra.Command{
Use: "job <jid>",
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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions cmd/submitJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/useCluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ var useClusterCmd = &cobra.Command{
Use: "use-cluster <cluster url>",
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")
Expand Down
17 changes: 14 additions & 3 deletions config/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -68,3 +74,8 @@ func GetHeaders() []string {
current, _ := GetCurrent()
return current.Headers
}

func GetBasicAuth() BasicAuth {
current, _ := GetCurrent()
return current.BasicAuth
}
9 changes: 9 additions & 0 deletions tools/applyHeaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ": ")
Expand All @@ -15,5 +23,6 @@ func ApplyHeadersToRequest(request *gorequest.SuperAgent) *gorequest.SuperAgent
}
request = request.AppendHeader(parts[0], parts[1])
}
request = ApplyBasicAuthToRequest(request)
return request
}

0 comments on commit 0601c29

Please sign in to comment.