-
Notifications
You must be signed in to change notification settings - Fork 5
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
refactor: helper function for getToken for cache reset + error handling #133
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,17 @@ import ( | |||||||
"github.com/spf13/cobra" | ||||||||
) | ||||||||
|
||||||||
func getToken() (string, error) { | ||||||||
token := os.Getenv("DEPOT_TOKEN") | ||||||||
if token == "" { | ||||||||
token = config.GetApiToken() | ||||||||
} | ||||||||
if token == "" { | ||||||||
return "", fmt.Errorf("missing API token, please run `depot login`") | ||||||||
} | ||||||||
return token, nil | ||||||||
} | ||||||||
|
||||||||
func NewCmdResetCache() *cobra.Command { | ||||||||
var projectID string | ||||||||
var token string | ||||||||
|
@@ -24,25 +35,25 @@ func NewCmdResetCache() *cobra.Command { | |||||||
Use: "reset", | ||||||||
Short: "Reset the cache for a project", | ||||||||
Args: cli.RequiresMaxArgs(1), | ||||||||
|
||||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||||
var cwd string | ||||||||
if len(args) > 0 { | ||||||||
cwd, _ = filepath.Abs(args[0]) | ||||||||
cwd, err := filepath.Abs(args[0]) | ||||||||
if err != nil { | ||||||||
return fmt.Errorf("unable to determine absolute path: %w", err) | ||||||||
} | ||||||||
|
||||||||
projectID := helpers.ResolveProjectID(projectID, cwd) | ||||||||
if projectID == "" { | ||||||||
return errors.Errorf("unknown project ID (run `depot init` or use --project or $DEPOT_PROJECT_ID)") | ||||||||
} | ||||||||
if projectID == "" { | ||||||||
return errors.Errorf("unknown project ID (run `depot init` or use --project or $DEPOT_PROJECT_ID)") | ||||||||
} | ||||||||
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line is duplicated:
Suggested change
|
||||||||
|
||||||||
// TODO: make this a helper | ||||||||
if token == "" { | ||||||||
token = os.Getenv("DEPOT_TOKEN") | ||||||||
} | ||||||||
if token == "" { | ||||||||
token = config.GetApiToken() | ||||||||
} | ||||||||
if token == "" { | ||||||||
return fmt.Errorf("missing API token, please run `depot login`") | ||||||||
token, err := getToken() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the token = helpers.ResolveToken(context.Background(), token) |
||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
client := api.NewProjectsClient() | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic from before isn't really easy to follow, sorry about that. What's happening is that
cache reset
can take either 0 or 1 arguments. If no arguments are specified, then thecwd
variable will contain an empty string""
.Then if an empty string passed to
helpers.ResolveProjectID(projectID, cwd)
as thecwd
, that function will actually resolve the current working directory.So we do only want to try to resolve
args[0]
iflen(args) > 0
.Maybe to make it clearer, we could rename the
cwd
variable herecwdOverride
orprojectDir
or something that would make it more clear?We could also move the resolution of
cwd, _ := filepath.Abs(...)
into the helper function itself, I imagine every command has that part duplicated currently.