| 58 | } |
| 59 | |
| 60 | func runAdd(opts *AddOptions) error { |
| 61 | httpClient, err := opts.HTTPClient() |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | var keyReader io.Reader |
| 67 | if opts.KeyFile == "-" { |
| 68 | keyReader = opts.IO.In |
| 69 | defer opts.IO.In.Close() |
| 70 | } else { |
| 71 | f, err := os.Open(opts.KeyFile) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | defer f.Close() |
| 76 | keyReader = f |
| 77 | } |
| 78 | |
| 79 | cfg, err := opts.Config() |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | hostname, _ := cfg.Authentication().DefaultHost() |
| 85 | |
| 86 | var uploaded bool |
| 87 | |
| 88 | if opts.Type == shared.SigningKey { |
| 89 | uploaded, err = SSHSigningKeyUpload(httpClient, hostname, keyReader, opts.Title) |
| 90 | } else { |
| 91 | uploaded, err = SSHKeyUpload(httpClient, hostname, keyReader, opts.Title) |
| 92 | } |
| 93 | |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | cs := opts.IO.ColorScheme() |
| 99 | |
| 100 | if uploaded { |
| 101 | fmt.Fprintf(opts.IO.ErrOut, "%s Public key added to your account\n", cs.SuccessIcon()) |
| 102 | } else { |
| 103 | fmt.Fprintf(opts.IO.ErrOut, "%s Public key already exists on your account\n", cs.SuccessIcon()) |
| 104 | } |
| 105 | |
| 106 | return nil |
| 107 | } |