Skip to content

Commit

Permalink
add token flag to list
Browse files Browse the repository at this point in the history
  • Loading branch information
justin1121 committed Apr 12, 2023
1 parent d6c1394 commit 4e68d47
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cmd/cape/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var listCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().StringP("token", "t", "", "authorization token to use")
listCmd.PersistentFlags().IntP("limit", "", 50, "Limit the number of functions returned.")
listCmd.PersistentFlags().IntP("offset", "", 0, "Number of functions to skip before listing.")
}
Expand Down Expand Up @@ -77,11 +78,17 @@ func list(cmd *cobra.Command, args []string) error {
return UserError{Msg: "list does not take any arguments", Err: fmt.Errorf("invalid number of input arguments")}
}

t, err := authToken()
if err != nil {
return err
token, _ := cmd.Flags().GetString("token")
if token == "" {
t, err := getAuthToken()
if err != nil {
return err
}

token = t
}
auth := entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: t}

auth := entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: token}
err = doList(u, insecure, auth, limit, offset, format)
if err != nil {
if errors.Is(err, ErrUnauthorized) {
Expand All @@ -93,7 +100,7 @@ func list(cmd *cobra.Command, args []string) error {
return nil
}

func doList(url string, insecure bool, auth entities.FunctionAuth, limit int, offset int, format string) error { //nolint:gocognit
func doList(url string, insecure bool, auth entities.FunctionAuth, limit int, offset int, format string) error {
endpoint := fmt.Sprintf("%s/v1/functions?limit=%d&offset=%d", url, limit, offset)

req, err := http.NewRequest("GET", endpoint, nil)
Expand Down

0 comments on commit 4e68d47

Please sign in to comment.