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

Use a configure command to set auth #149

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions cmd/bk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func main() {

func mainRun() int {
ctx := context.Background()
viper := viper.New()
viper.SetConfigFile(config.ConfigFile())
viper.AutomaticEnv()
v := viper.New()
v.SetConfigFile(config.ConfigFile())
v.AutomaticEnv()
// attempt to read in config file but it might not exist
_ = viper.ReadInConfig()
_ = v.ReadInConfig()

rootCmd, err := root.NewCmdRoot(viper, build.Version)
rootCmd, err := root.NewCmdRoot(v, build.Version)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create root command: %s\n", err)
return 1
Expand Down
18 changes: 0 additions & 18 deletions pkg/cmd/auth/auth.go

This file was deleted.

13 changes: 6 additions & 7 deletions pkg/cmd/auth/init/init.go → pkg/cmd/configure/configure.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package init
package configure

import (
"errors"
Expand All @@ -9,13 +9,13 @@ import (
"github.com/spf13/viper"
)

func NewCmdInit(viper *viper.Viper) *cobra.Command {
func NewCmdConfigure(v *viper.Viper) *cobra.Command {
var force bool

cmd := &cobra.Command{
Use: "init",
Use: "configure",
Args: cobra.ExactArgs(0),
Short: "Initialise authentication token",
Short: "Configure Buildkite API token",
RunE: func(cmd *cobra.Command, args []string) error {
// if the token already exists and --force is not used
if !force && viper.IsSet(config.APIToken) {
Expand All @@ -32,12 +32,11 @@ func NewCmdInit(viper *viper.Viper) *cobra.Command {
return err
}

viper.Set(config.APIToken, token)
return viper.WriteConfig()
v.Set(config.APIToken, token)
return v.WriteConfig()
},
}

cmd.Flags().BoolVar(&force, "force", false, "Force setting a new token")

return cmd
}
4 changes: 2 additions & 2 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package root

import (
"github.com/MakeNowJust/heredoc"
authCmd "github.com/buildkite/cli/v3/pkg/cmd/auth"
configureCmd "github.com/buildkite/cli/v3/pkg/cmd/configure"
versionCmd "github.com/buildkite/cli/v3/pkg/cmd/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -22,7 +22,7 @@ func NewCmdRoot(viper *viper.Viper, version string) (*cobra.Command, error) {
}

cmd.AddCommand(versionCmd.NewCmdVersion())
cmd.AddCommand(authCmd.NewCmdAuth(viper))
cmd.AddCommand(configureCmd.NewCmdConfigure(viper))

return cmd, nil
}