forked from a2call/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
67 lines (62 loc) · 1.72 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package keys
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"golang.org/x/crypto/ssh"
"github.com/Sirupsen/logrus"
"github.com/daticahealth/cli/commands/deploykeys"
"github.com/daticahealth/cli/config"
"github.com/daticahealth/cli/models"
"github.com/mitchellh/go-homedir"
)
func CmdAdd(name, path string, ik IKeys, id deploykeys.IDeployKeys) error {
if strings.ContainsAny(name, config.InvalidChars) {
return fmt.Errorf("Invalid key name. Names must not contain the following characters: %s", config.InvalidChars)
}
homePath, err := homedir.Expand(path)
if err != nil {
return err
}
fullPath, err := filepath.Abs(homePath)
if err != nil {
return err
}
keyBytes, err := ioutil.ReadFile(fullPath)
if err != nil {
return err
}
k, err := id.ParsePublicKey(keyBytes)
if err != nil {
return err
}
key := ssh.MarshalAuthorizedKey(k)
if err != nil {
return err
}
err = ik.Add(name, string(key))
if err != nil {
return err
}
logrus.Printf("Key '%s' added to your account.", name)
logrus.Println("If you use an ssh-agent, make sure you add this key to your ssh-agent in order to push code")
return nil
}
// Add adds a new public key to the authenticated user's account
func (k *SKeys) Add(name, publicKey string) error {
body, err := json.Marshal(models.UserKey{
Key: publicKey,
Name: name,
})
if err != nil {
return err
}
headers := k.Settings.HTTPManager.GetHeaders(k.Settings.SessionToken, k.Settings.Version, k.Settings.Pod, k.Settings.UsersID)
resp, status, err := k.Settings.HTTPManager.Post(body, fmt.Sprintf("%s%s/keys", k.Settings.AuthHost, k.Settings.AuthHostVersion), headers)
if err != nil {
return err
}
return k.Settings.HTTPManager.ConvertResp(resp, status, nil)
}