(ch *cmdutil.Helper)
| 9 | ) |
| 10 | |
| 11 | func ListCmd(ch *cmdutil.Helper) *cobra.Command { |
| 12 | var pageSize uint32 |
| 13 | var pageToken string |
| 14 | var refreshTokensOnly bool |
| 15 | |
| 16 | listCmd := &cobra.Command{ |
| 17 | Use: "list", |
| 18 | Args: cobra.NoArgs, |
| 19 | Short: "List personal access tokens", |
| 20 | RunE: func(cmd *cobra.Command, args []string) error { |
| 21 | client, err := ch.Client() |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | var refresh *bool |
| 27 | // check if flag was explicitly set |
| 28 | if cmd.Flags().Changed("refresh") { |
| 29 | refresh = &refreshTokensOnly |
| 30 | } |
| 31 | |
| 32 | res, err := client.ListUserAuthTokens(cmd.Context(), &adminv1.ListUserAuthTokensRequest{ |
| 33 | UserId: "current", |
| 34 | PageSize: pageSize, |
| 35 | PageToken: pageToken, |
| 36 | Refresh: refresh, |
| 37 | }) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | if len(res.Tokens) == 0 { |
| 43 | if refreshTokensOnly { |
| 44 | ch.PrintfWarn("No refresh tokens found\n") |
| 45 | } else { |
| 46 | ch.PrintfWarn("No tokens found\n") |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // If the result set is smaller than the page size, sort by creation time (newest first). |
| 52 | if pageToken == "" && uint32(len(res.Tokens)) < pageSize { |
| 53 | sort.Slice(res.Tokens, func(i, j int) bool { |
| 54 | return res.Tokens[i].CreatedOn.AsTime().After(res.Tokens[j].CreatedOn.AsTime()) |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | ch.PrintUserTokens(res.Tokens) |
| 59 | |
| 60 | if res.NextPageToken != "" { |
| 61 | cmd.Println() |
| 62 | cmd.Printf("Next page token: %s\n", res.NextPageToken) |
| 63 | } |
| 64 | |
| 65 | return nil |
| 66 | }, |
| 67 | } |
| 68 |
no test coverage detected