|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/MakeNowJust/heredoc/v2" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + lab "github.com/zaquestion/lab/internal/gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + expiresAt time.Time |
| 16 | +) |
| 17 | + |
| 18 | +var tokenCreateCmd = &cobra.Command{ |
| 19 | + Use: "create", |
| 20 | + Short: "create a new Personal Access Token", |
| 21 | + Args: cobra.MaximumNArgs(1), |
| 22 | + Example: heredoc.Doc(` |
| 23 | + lab token create`), |
| 24 | + PersistentPreRun: labPersistentPreRun, |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + // The values of name and scopes must be specified, they are not optional. |
| 27 | + name, _ := cmd.Flags().GetString("name") |
| 28 | + if name == "" { |
| 29 | + log.Fatal("The name of the token must be specified.") |
| 30 | + } |
| 31 | + |
| 32 | + scopes, _ := cmd.Flags().GetStringSlice("scopes") |
| 33 | + if len(scopes) == 0 { |
| 34 | + log.Fatal("Scopes must be specified. See --help for available options.") |
| 35 | + } |
| 36 | + |
| 37 | + // expiresat is optional |
| 38 | + expiresat, _ := cmd.Flags().GetString("expiresat") |
| 39 | + if expiresat != "" { |
| 40 | + s := strings.Split(expiresat, "-") |
| 41 | + if len(s) != 3 { |
| 42 | + log.Fatal("Incorrect date specified, must be YYYY-MM-DD format") |
| 43 | + } |
| 44 | + |
| 45 | + year, err := strconv.Atoi(s[0]) |
| 46 | + if err != nil { |
| 47 | + log.Fatal("Invalid year specified") |
| 48 | + } |
| 49 | + month, err := strconv.Atoi(s[1]) |
| 50 | + if err != nil { |
| 51 | + log.Fatal("Invalid month specified") |
| 52 | + } |
| 53 | + day, err := strconv.Atoi(s[2]) |
| 54 | + if err != nil { |
| 55 | + log.Fatal("Invalid day specified") |
| 56 | + } |
| 57 | + |
| 58 | + loc, _ := time.LoadLocation("UTC") |
| 59 | + expiresAt = time.Date(year, time.Month(month), day, 0, 0, 0, 0, loc) |
| 60 | + |
| 61 | + yearNow, monthNow, dayNow := time.Now().UTC().Date() |
| 62 | + yearFromNow := time.Date(yearNow, monthNow, dayNow, 0, 0, 0, 0, loc).AddDate(1, 0, 0) |
| 63 | + if expiresAt.After(yearFromNow) { |
| 64 | + log.Fatalf("Expires date can only be a maximum of one year from now (%s)", yearFromNow.String()) |
| 65 | + } |
| 66 | + } else { |
| 67 | + loc, _ := time.LoadLocation("UTC") |
| 68 | + yearNow, monthNow, dayNow := time.Now().UTC().Date() |
| 69 | + expiresAt = time.Date(yearNow, monthNow, dayNow, 0, 0, 0, 0, loc).AddDate(0, 0, 30) |
| 70 | + } |
| 71 | + |
| 72 | + pat, err := lab.CreatePAT(name, expiresAt, scopes) |
| 73 | + if err != nil { |
| 74 | + log.Fatal(err) |
| 75 | + } |
| 76 | + fmt.Printf("%s created set to expire on %s", pat, expiresat) |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +func init() { |
| 81 | + tokenCreateCmd.Flags().StringP("name", "n", "", "name of token") |
| 82 | + tokenCreateCmd.Flags().StringSliceP("scopes", "s", []string{}, "Comma separated scopes for this token. (Available scopes are: api, read_api, read_user, read_repository, write_repository, read_registry, write_registry, sudo, admin_mode.") |
| 83 | + tokenCreateCmd.Flags().StringP("expiresat", "e", "", "YYYY-MM-DD formatted date the token will expire on (Default: 30 days from now, Maximum: One year from today.)") |
| 84 | + tokenCmd.AddCommand(tokenCreateCmd) |
| 85 | +} |
0 commit comments