|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/GPULab-AI/gpulab-cli/internal/api" |
| 12 | + "github.com/GPULab-AI/gpulab-cli/internal/output" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + credRegistry string |
| 18 | + credUsername string |
| 19 | + credPassword string |
| 20 | + credPasswordStdin bool |
| 21 | + credDeleteForce bool |
| 22 | +) |
| 23 | + |
| 24 | +func init() { |
| 25 | + rootCmd.AddCommand(credentialsCmd) |
| 26 | + credentialsCmd.AddCommand(credentialsListCmd) |
| 27 | + credentialsCmd.AddCommand(credentialsAddCmd) |
| 28 | + credentialsCmd.AddCommand(credentialsDeleteCmd) |
| 29 | + |
| 30 | + credentialsAddCmd.Flags().StringVar(&credRegistry, "registry", "docker.io", "Registry host (e.g. docker.io, quay.io, ghcr.io)") |
| 31 | + credentialsAddCmd.Flags().StringVar(&credUsername, "username", "", "Registry username") |
| 32 | + credentialsAddCmd.Flags().StringVar(&credPassword, "password", "", "Registry password or access token") |
| 33 | + credentialsAddCmd.Flags().BoolVar(&credPasswordStdin, "password-stdin", false, "Read the password from stdin (keeps it out of shell history)") |
| 34 | + credentialsAddCmd.MarkFlagRequired("username") |
| 35 | + |
| 36 | + credentialsDeleteCmd.Flags().BoolVar(&credDeleteForce, "force", false, "Skip confirmation") |
| 37 | +} |
| 38 | + |
| 39 | +var credentialsCmd = &cobra.Command{ |
| 40 | + Use: "credentials", |
| 41 | + Aliases: []string{"creds"}, |
| 42 | + Short: "Manage Docker registry credentials (for private images)", |
| 43 | + Long: `Store, list, and delete Docker registry credentials used to pull private images. |
| 44 | +
|
| 45 | +Reference a credential from a template with 'gpulab templates create --credentials <id>', |
| 46 | +or set one inline with --registry-username / --registry-password on create/edit. |
| 47 | +
|
| 48 | +Running 'gpulab credentials' with no subcommand lists every credential.`, |
| 49 | + Example: ` gpulab credentials add --registry docker.io --username adhik --password *** |
| 50 | + gpulab credentials add --username adhik --password-stdin < token.txt |
| 51 | + gpulab credentials # list credentials |
| 52 | + gpulab credentials rm 42`, |
| 53 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 54 | + return credentialsListCmd.RunE(cmd, args) |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +var credentialsListCmd = &cobra.Command{ |
| 59 | + Use: "list", |
| 60 | + Aliases: []string{"ls"}, |
| 61 | + Short: "List stored registry credentials", |
| 62 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | + client := requireAuth() |
| 64 | + creds, err := client.ListCredentials() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + switch getOutputFormat() { |
| 70 | + case output.FormatJSON: |
| 71 | + output.PrintJSON(creds) |
| 72 | + case output.FormatQuiet: |
| 73 | + ids := make([]string, len(creds)) |
| 74 | + for i, c := range creds { |
| 75 | + ids[i] = strconv.Itoa(c.ID) |
| 76 | + } |
| 77 | + output.PrintQuiet(ids) |
| 78 | + default: |
| 79 | + if len(creds) == 0 { |
| 80 | + fmt.Println("No credentials found. Add one with 'gpulab credentials add'.") |
| 81 | + return nil |
| 82 | + } |
| 83 | + headers := []string{"ID", "REGISTRY", "USERNAME", "CREATED"} |
| 84 | + rows := make([][]string, len(creds)) |
| 85 | + for i, c := range creds { |
| 86 | + rows[i] = []string{strconv.Itoa(c.ID), c.Registry, c.Username, c.CreatedAt} |
| 87 | + } |
| 88 | + output.PrintTable(headers, rows) |
| 89 | + } |
| 90 | + return nil |
| 91 | + }, |
| 92 | +} |
| 93 | + |
| 94 | +var credentialsAddCmd = &cobra.Command{ |
| 95 | + Use: "add", |
| 96 | + Short: "Add a Docker registry credential", |
| 97 | + Long: `Store a Docker registry credential for pulling private images. |
| 98 | +
|
| 99 | +The password may be passed with --password or piped in via --password-stdin |
| 100 | +(recommended, so it stays out of your shell history).`, |
| 101 | + Example: ` gpulab credentials add --username adhik --password dckr_pat_xxx |
| 102 | + echo "$DOCKER_TOKEN" | gpulab credentials add --username adhik --password-stdin |
| 103 | + gpulab credentials add --registry ghcr.io --username adhik --password ghp_xxx`, |
| 104 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 105 | + client := requireAuth() |
| 106 | + |
| 107 | + password := credPassword |
| 108 | + if credPasswordStdin { |
| 109 | + data, err := io.ReadAll(bufio.NewReader(os.Stdin)) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to read password from stdin: %w", err) |
| 112 | + } |
| 113 | + password = strings.TrimRight(string(data), "\r\n") |
| 114 | + } |
| 115 | + if password == "" { |
| 116 | + return fmt.Errorf("a password is required; pass --password or --password-stdin") |
| 117 | + } |
| 118 | + |
| 119 | + cred, err := client.CreateCredential(&api.CreateCredentialRequest{ |
| 120 | + Registry: credRegistry, |
| 121 | + Username: credUsername, |
| 122 | + Password: password, |
| 123 | + }) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if flagJSON { |
| 129 | + output.PrintJSON(cred) |
| 130 | + return nil |
| 131 | + } |
| 132 | + output.PrintSuccess(fmt.Sprintf("Credential created: %d", cred.ID)) |
| 133 | + fmt.Printf("Registry: %s\n", cred.Registry) |
| 134 | + fmt.Printf("Username: %s\n", cred.Username) |
| 135 | + fmt.Printf("\nReference it on a template with --credentials %d\n", cred.ID) |
| 136 | + return nil |
| 137 | + }, |
| 138 | +} |
| 139 | + |
| 140 | +var credentialsDeleteCmd = &cobra.Command{ |
| 141 | + Use: "delete [ID]", |
| 142 | + Aliases: []string{"rm", "remove"}, |
| 143 | + Short: "Delete a registry credential", |
| 144 | + Args: cobra.ExactArgs(1), |
| 145 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 146 | + client := requireAuth() |
| 147 | + |
| 148 | + id, err := strconv.Atoi(args[0]) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("credential id must be a number, got %q", args[0]) |
| 151 | + } |
| 152 | + |
| 153 | + if !credDeleteForce && !flagJSON && !flagQuiet { |
| 154 | + fmt.Printf("Delete credential %d? [y/N] ", id) |
| 155 | + var confirm string |
| 156 | + fmt.Scanln(&confirm) |
| 157 | + if strings.ToLower(confirm) != "y" { |
| 158 | + fmt.Println("Cancelled.") |
| 159 | + return nil |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if err := client.DeleteCredential(id); err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + if flagJSON { |
| 168 | + output.PrintJSON(map[string]interface{}{"status": "success", "action": "deleted", "id": id}) |
| 169 | + return nil |
| 170 | + } |
| 171 | + output.PrintSuccess(fmt.Sprintf("Credential deleted: %d", id)) |
| 172 | + return nil |
| 173 | + }, |
| 174 | +} |
0 commit comments