/
add.go
40 lines (34 loc) · 873 Bytes
/
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
package keys
import (
"fmt"
"io/ioutil"
"os"
"github.com/Scalingo/cli/config"
"gopkg.in/errgo.v1"
)
func Add(name string, path string) error {
stat, err := os.Stat(path)
if err != nil {
return errgo.Notef(err, "fail to stat path to the key")
}
if stat.Mode().IsDir() {
return fmt.Errorf("%s: is a directory", path)
}
if stat.Size() > 10*1024*1024 {
return fmt.Errorf("%s: is too large (%v bytes)", path, stat.Size())
}
keyContent, err := ioutil.ReadFile(path)
if err != nil {
return errgo.Notef(err, "fail to read the key file")
}
c, err := config.ScalingoAuthClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
}
_, err = c.KeysAdd(name, string(keyContent))
if err != nil {
return errgo.Notef(err, "fail to add the key to Scalingo account")
}
fmt.Printf("Key '%s' has been added.\n", name)
return nil
}