Skip to content

Commit

Permalink
fix(cli): handle CLI config file not found error (#1345) (#1347)
Browse files Browse the repository at this point in the history
Signed-off-by: Sunghoon Kang <[email protected]>
Co-authored-by: Sunghoon Kang <[email protected]>
  • Loading branch information
krancour and Sunghoon Kang authored Jan 2, 2024
1 parent 882617b commit 42f2d38
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 16 deletions.
7 changes: 5 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ func main() {
ctx := context.Background()
cfg, err := config.LoadCLIConfig()
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrap(err, "load config"))
os.Exit(1)
if !config.IsConfigNotFoundErr(err) {
fmt.Fprintln(os.Stderr, errors.Wrap(err, "load config"))
os.Exit(1)
}
cfg = config.NewDefaultCLIConfig()
}
cmd, err := NewRootCommand(option.NewOption(cfg), &rootState{})
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func GetClientFromConfig(ctx context.Context, opt *option.Option) (
}
cfg, err := config.LoadCLIConfig()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "load cli config")
}
if cfg.APIAddress == "" || cfg.BearerToken == "" {
return nil, errors.New(
"seems like you are not logged in; please use `kargo login` to authenticate",
)
}
skipTLSVerify := opt.InsecureTLS || cfg.InsecureSkipTLSVerify
if cfg, err =
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kargo config set project my-project
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadCLIConfig()
if err != nil {
return errors.Wrap(err, "load cli config")
return errors.Wrap(err, "ensure cli config")
}

key := strings.ToLower(args[0])
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/config/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kargo config unset project my-project
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadCLIConfig()
if err != nil {
return errors.Wrap(err, "load cli config")
return errors.Wrap(err, "ensure cli config")
}

key := strings.ToLower(args[0])
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/create/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ kargo create project my-project

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}
resp, err := kargoSvcCli.CreateProject(ctx,
connect.NewRequest(&kargosvcapi.CreateProjectRequest{
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/delete/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ kargo delete project my-project
ctx := cmd.Context()
kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}

var resErr error
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/delete/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ kargo delete stage --project=my-project my-stage
ctx := cmd.Context()
kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}

project := opt.Project
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/delete/warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ kargo delete warehouse --project=my-project my-warehouse
ctx := cmd.Context()
kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}

project := opt.Project
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/get/freight.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ kargo get freight --project=my-project my-freight

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}
resp, err := kargoSvcCli.QueryFreight(ctx, connect.NewRequest(&v1alpha1.QueryFreightRequest{
Project: project,
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/get/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ kargo get projects -o json

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}
resp, err := kargoSvcCli.ListProjects(ctx, connect.NewRequest(&v1alpha1.ListProjectsRequest{}))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/get/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ kargo get promotions --project=my-project some-promotion

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}
resp, err := kargoSvcCli.ListPromotions(ctx, connect.NewRequest(req))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/get/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ kargo get stages --project=my-project my-stage

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}
resp, err := kargoSvcCli.ListStages(ctx, connect.NewRequest(&v1alpha1.ListStagesRequest{
Project: project,
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/get/warehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kargo get warehouses --project=my-project my-warehouse

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}
resp, err := kargoSvcCli.ListWarehouses(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cmd/update/freight-alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newUpdateFreightAliasCommand(opt *option.Option) *cobra.Command {

kargoSvcCli, err := client.GetClientFromConfig(ctx, opt)
if err != nil {
return errors.New("get client from config")
return errors.Wrap(err, "get client from config")
}

if _, err = kargoSvcCli.UpdateFreightAlias(
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type CLIConfig struct {
Project string `json:"project,omitempty"`
}

// NewDefaultCLIConfig returns a new default CLI configuration.
func NewDefaultCLIConfig() CLIConfig {
return CLIConfig{}
}

// LoadCLIConfig loads Kargo CLI configuration from a file in the Kargo home
// directory.
func LoadCLIConfig() (CLIConfig, error) {
Expand Down
11 changes: 10 additions & 1 deletion internal/cli/config/errors.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package config

import "fmt"
import (
"fmt"

"github.com/pkg/errors"
)

type ErrConfigNotFound struct {
Path string
}

func IsConfigNotFoundErr(target error) bool {
var err *ErrConfigNotFound
return errors.As(target, &err)
}

func NewConfigNotFoundErr(path string) error {
return &ErrConfigNotFound{Path: path}
}
Expand Down

0 comments on commit 42f2d38

Please sign in to comment.