Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix token flag ignored in context creation #173

Merged
merged 1 commit into from
Aug 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions cmd/context_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package cmd
import (
"bufio"
"fmt"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
"os"
"strings"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

// addCmd represents the add command
Expand All @@ -34,28 +35,32 @@ var addCmd = &cobra.Command{
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name, token := args[0], ""
r := bufio.NewReader(os.Stdin)
for {
fmt.Printf("Token: ")
t, err := r.ReadString('\n')
FatalOnError(err)
t = strings.TrimSpace(t)
if t == "" {
continue
}
name := args[0]
token, err := cmd.Flags().GetString("token")
FatalOnError(err)

token = t
break
}
if token == "" {
r := bufio.NewReader(os.Stdin)
for {
fmt.Printf("Token: ")
t, err := r.ReadString('\n')
FatalOnError(err)
t = strings.TrimSpace(t)
if t == "" {
continue
}

token = t
break
}
}
// test connection
opts := []hcloud.ClientOption{
hcloud.WithToken(token),
}

AppConf.Client = hcloud.NewClient(opts...)
_, err := AppConf.Client.Server.All(AppConf.Context)
_, err = AppConf.Client.Server.All(AppConf.Context)

FatalOnError(err)

Expand All @@ -72,14 +77,4 @@ func init() {
contextCmd.AddCommand(addCmd)

addCmd.Flags().StringP("token", "t", "", "token of the context")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}