-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfAddIngressKey.go
108 lines (85 loc) · 3 KB
/
selfAddIngressKey.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
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/inpher/sb/internal/commands"
"github.com/inpher/sb/internal/helpers"
"github.com/inpher/sb/internal/models"
)
// SelfAddIngressKey describes the selfAddIngresKey command
type SelfAddIngressKey struct{}
func init() {
commands.RegisterCommand("self ingress-key add", func() (c commands.Command, r models.Right, h helpers.Helper, args map[string]commands.Argument) {
return new(SelfAddIngressKey), models.Public, helpers.Helper{
Header: "add a new public ingress key (you -> sb) to your account",
Usage: "self ingress-key add [--public-key 'KEY']",
Description: "add a new public ingress key (you -> sb) to your account",
Aliases: []string{"selfAddIngressKey"},
}, map[string]commands.Argument{
"public-key": {
Required: false,
Description: "Your new public SSH key to deposit on sb (you will need to '\"double escape it\"'); if not present, you'll be prompted for it",
},
}
})
}
// Checks checks whether or not the user can execute this method
func (c *SelfAddIngressKey) Checks(ct *commands.Context) error {
// No specific rights needed but a sb account
return nil
}
// Execute executes the command
func (c *SelfAddIngressKey) Execute(ct *commands.Context) (repl models.ReplicationData, cmdError error, err error) {
str, keys, _ := ct.User.DisplayPubKeys("ingress")
var pk *helpers.PublicKey
if ct.FormattedArguments["public-key"] == "" {
// Interactive mode
// We start by displaying the current keys (output of selfListIngressKeys)
fmt.Printf("Here is the list of your current ingress public SSH keys (you -> sb):\n%s\n", str)
// Then we request the new key to add
for pk == nil {
fmt.Print("Please paste the public key you want to add: ")
// Scan stdin to get the key to delete
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
ct.FormattedArguments["public-key"] = scanner.Text()
pk, err = helpers.CheckStringPK(ct.FormattedArguments["public-key"], keys)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
}
} else {
// Non-interactive mode
// We try to validate the provided public-key
pk, err = helpers.CheckStringPK(ct.FormattedArguments["public-key"], keys)
if err != nil {
return
}
}
repl = models.ReplicationData{
"account": ct.User.User.Username,
"public-key": pk.String(),
}
err = c.Replicate(repl)
// We finally display the account's keys (output of selfListIngressKeys)
str, _, _ = ct.User.DisplayPubKeys("ingress")
fmt.Printf("Here is the list of your current ingress public SSH keys (you -> sb):\n%s\n", str)
return
}
func (c *SelfAddIngressKey) PostExecute(repl models.ReplicationData) (err error) {
return
}
func (c *SelfAddIngressKey) Replicate(repl models.ReplicationData) (err error) {
user, err := models.LoadUser(repl["account"])
if err != nil {
return
}
// We add the key to the account
err = user.AddIngressKey(repl["public-key"])
if err != nil {
return
}
fmt.Println("Your key was successfully added.")
return
}