()
| 119 | } |
| 120 | |
| 121 | func authNewTokenCommand() *cobra.Command { |
| 122 | tokensCmd := &cobra.Command{ |
| 123 | Use: "tokens", |
| 124 | Short: "Manage devbox auth tokens", |
| 125 | } |
| 126 | |
| 127 | newCmd := &cobra.Command{ |
| 128 | Use: "new", |
| 129 | Short: "Create a new token", |
| 130 | Args: cobra.ExactArgs(0), |
| 131 | RunE: func(cmd *cobra.Command, args []string) error { |
| 132 | ctx := cmd.Context() |
| 133 | token, err := identity.GenSession(ctx) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | client := api.NewClient(ctx, build.JetpackAPIHost(), token) |
| 138 | pat, err := client.CreateToken(ctx) |
| 139 | if err != nil { |
| 140 | // This is a hack because errors are not returning with correct code. |
| 141 | // Once that is fixed, we can switch to use *connect.Error Code() instead. |
| 142 | if strings.Contains(err.Error(), "permission_denied") { |
| 143 | ux.Ferrorf( |
| 144 | cmd.ErrOrStderr(), |
| 145 | "You do not have permission to create a token. Please contact your"+ |
| 146 | " administrator.", |
| 147 | ) |
| 148 | return nil |
| 149 | } |
| 150 | return err |
| 151 | } |
| 152 | ux.Fsuccessf(cmd.OutOrStdout(), "Token created.\n\n") |
| 153 | table := tablewriter.NewWriter(cmd.OutOrStdout()) |
| 154 | // Row lines are configured through the renderer in the new API |
| 155 | if err := table.Bulk([][]string{ |
| 156 | {"Token ID", pat.GetToken().GetId()}, |
| 157 | {"Secret", pat.GetToken().GetSecret()}, |
| 158 | }); err != nil { |
| 159 | return err |
| 160 | } |
| 161 | return table.Render() |
| 162 | }, |
| 163 | } |
| 164 | |
| 165 | tokensCmd.AddCommand(newCmd) |
| 166 | |
| 167 | return tokensCmd |
| 168 | } |
no test coverage detected