()
| 27 | ) |
| 28 | |
| 29 | func newAPITokenCreateCmd() *cobra.Command { |
| 30 | var ( |
| 31 | description, name, projectName string |
| 32 | expiresIn time.Duration |
| 33 | ) |
| 34 | |
| 35 | cmd := &cobra.Command{ |
| 36 | Use: "create", |
| 37 | Short: "Create an API token", |
| 38 | RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | var duration *time.Duration |
| 40 | if expiresIn != 0 { |
| 41 | duration = &expiresIn |
| 42 | } |
| 43 | |
| 44 | res, err := action.NewAPITokenCreate(ActionOpts).Run(context.Background(), name, description, projectName, duration) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("creating API token: %w", err) |
| 47 | } |
| 48 | |
| 49 | if flagOutputFormat == "token" { |
| 50 | fmt.Print(res.JWT) |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | return output.EncodeOutput(flagOutputFormat, res, apiTokenItemTableOutput) |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | cmd.Flags().StringVar(&description, "description", "", "API token description") |
| 59 | cmd.Flags().DurationVar(&expiresIn, "expiration", 0, "optional API token expiration, in hours i.e 1h, 24h, 178h (week), ...") |
| 60 | cmd.Flags().StringVar(&name, "name", "", "token name") |
| 61 | // Override default output flag |
| 62 | cmd.InheritedFlags().StringVarP(&flagOutputFormat, "output", "o", "table", "output format, valid options are table, json, token") |
| 63 | err := cmd.MarkFlagRequired("name") |
| 64 | cobra.CheckErr(err) |
| 65 | cmd.Flags().StringVar(&projectName, "project", "", "project name used to scope the token, if not set the token will be created at the organization level") |
| 66 | |
| 67 | return cmd |
| 68 | } |
| 69 | |
| 70 | func apiTokenItemTableOutput(token *action.APITokenItem) error { |
| 71 | return apiTokenListTableOutput([]*action.APITokenItem{token}) |
no test coverage detected