forked from a2call/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
134 lines (125 loc) · 5.13 KB
/
contract.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package keys
import (
"github.com/Sirupsen/logrus"
"github.com/daticahealth/cli/commands/deploykeys"
"github.com/daticahealth/cli/lib/auth"
"github.com/daticahealth/cli/lib/prompts"
"github.com/daticahealth/cli/models"
"github.com/jault3/mow.cli"
)
// Cmd for keys
var Cmd = models.Command{
Name: "keys",
ShortHelp: "Tasks for SSH keys",
LongHelp: "The <code>keys</code> command gives access to SSH key management for your user account. " +
"SSH keys can be used for authentication and pushing code to the Datica platform. " +
"Any SSH keys added to your user account should not be shared but be treated as private SSH keys. " +
"Any SSH key uploaded to your user account will be able to be used with all code services and environments that you have access to. " +
"The keys command can not be run directly but has subcommands.",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.CommandLong(AddSubCmd.Name, AddSubCmd.ShortHelp, AddSubCmd.LongHelp, AddSubCmd.CmdFunc(settings))
cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings))
cmd.CommandLong(RemoveSubCmd.Name, RemoveSubCmd.ShortHelp, RemoveSubCmd.LongHelp, RemoveSubCmd.CmdFunc(settings))
cmd.CommandLong(SetSubCmd.Name, SetSubCmd.ShortHelp, SetSubCmd.LongHelp, SetSubCmd.CmdFunc(settings))
}
},
}
var AddSubCmd = models.Command{
Name: "add",
ShortHelp: "Add a public key",
LongHelp: "<code>keys add</code> allows you to add a new SSH key to your user account. " +
"SSH keys added to your user account should be private and not shared with others. " +
"SSH keys can be used for authentication (as opposed to the traditional email and password) as well as pushing code to an environment's code services. " +
"Please note, you must specify the path to the public key file and not the private key. " +
"All SSH keys should be in either OpenSSH RSA format or PEM format. Here is a sample command\n\n" +
"<pre>\ndatica keys add my_prod_key ~/.ssh/prod_rsa.pub\n</pre>",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
name := cmd.StringArg("NAME", "", "The name for the new key, for your own purposes")
path := cmd.StringArg("PUBLIC_KEY_PATH", "", "Relative path to the public key file")
cmd.Action = func() {
if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
logrus.Fatal(err.Error())
}
err := CmdAdd(*name, *path, New(settings), deploykeys.New(settings))
if err != nil {
logrus.Fatal(err)
}
}
}
},
}
var ListSubCmd = models.Command{
Name: "list",
ShortHelp: "List your public keys",
LongHelp: "<code>keys list</code> lists all public keys by name that have been uploaded to your user account including the key's fingerprint in SHA256 format. " +
"Here is a sample command\n\n" +
"<pre>\ndatica keys list\n</pre>",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Action = func() {
if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
logrus.Fatal(err.Error())
}
err := CmdList(New(settings), deploykeys.New(settings))
if err != nil {
logrus.Fatal(err)
}
}
}
},
}
var RemoveSubCmd = models.Command{
Name: "rm",
ShortHelp: "Remove a public key",
LongHelp: "<code>keys rm</code> allows you to remove an SSH key previously uploaded to your account. " +
"The name of the key can be found by using the keys list command. Here is a sample command\n\n" +
"<pre>\ndatica keys rm my_prod_key\n</pre>",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
name := cmd.StringArg("NAME", "", "The name of the key to remove.")
cmd.Action = func() {
if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
logrus.Fatal(err.Error())
}
err := CmdRemove(*name, settings.PrivateKeyPath, New(settings), deploykeys.New(settings))
if err != nil {
logrus.Fatal(err)
}
}
}
},
}
var SetSubCmd = models.Command{
Name: "set",
ShortHelp: "Set your auth key",
LongHelp: "<code>keys set</code> allows the CLI to use an SSH key for authentication instead of the traditional email and password combination. " +
"This can be useful for automation or where shared workstations are involved. " +
"Please note that you must pass in the path to the private key and not the public key. " +
"The given key must already be added to your account by using the keys add command. " +
"Here is a sample command\n\n" +
"<pre>\ndatica keys set ~/.ssh/my_key\n</pre>",
CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
path := cmd.StringArg("PRIVATE_KEY_PATH", "", "Relative path to the private key file")
cmd.Action = func() {
err := CmdSet(*path, settings)
if err != nil {
logrus.Fatal(err)
}
}
}
},
}
type IKeys interface {
List() (*[]models.UserKey, error)
Add(name, publicKey string) error
Remove(name string) error
}
type SKeys struct {
Settings *models.Settings
}
func New(settings *models.Settings) IKeys {
return &SKeys{Settings: settings}
}