| 37 | } |
| 38 | |
| 39 | func (r *RootCmd) licenseAdd() *serpent.Command { |
| 40 | var ( |
| 41 | filename string |
| 42 | license string |
| 43 | debug bool |
| 44 | ) |
| 45 | cmd := &serpent.Command{ |
| 46 | Use: "add [-f file | -l license]", |
| 47 | Short: "Add license to Coder deployment", |
| 48 | Middleware: serpent.Chain( |
| 49 | serpent.RequireNArgs(0), |
| 50 | ), |
| 51 | Handler: func(inv *serpent.Invocation) error { |
| 52 | client, err := r.InitClient(inv) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | switch { |
| 58 | case filename != "" && license != "": |
| 59 | return xerrors.New("only one of (--file, --license) may be specified") |
| 60 | |
| 61 | case filename == "" && license == "": |
| 62 | license, err = cliui.Prompt(inv, cliui.PromptOptions{ |
| 63 | Text: "Paste license:", |
| 64 | Secret: true, |
| 65 | Validate: validJWT, |
| 66 | }) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | case filename != "" && license == "": |
| 72 | var r io.Reader |
| 73 | if filename == "-" { |
| 74 | r = inv.Stdin |
| 75 | } else { |
| 76 | f, err := os.Open(filename) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | defer f.Close() |
| 81 | r = f |
| 82 | } |
| 83 | lb, err := io.ReadAll(r) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | license = string(lb) |
| 88 | } |
| 89 | license = strings.Trim(license, " \n") |
| 90 | err = validJWT(license) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | |
| 95 | licResp, err := client.AddLicense( |
| 96 | inv.Context(), |