/
entity-create.go
79 lines (65 loc) · 2.02 KB
/
entity-create.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
package ctl
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/NetAuth/NetAuth/pkg/client"
)
var (
newEntityID string
newNumber int
newSecret string
entityCreateCmd = &cobra.Command{
Use: "create <ID>",
Short: "Create a new entity with the specified ID",
Long: entityCreateLongDocs,
Args: cobra.ExactArgs(1),
Run: entityCreateRun,
}
entityCreateLongDocs = `
Create an entity with the specified ID. Though there are no strict
requirements on the ID beyond it being a single word that is globally
unique, it is strongly encouraged to make it exclusively of lower case
letters and numbers. For the best compatibility, it is recommended to
start with a letter only.
Additional fields can be specified on the command line such as the
number to assign or the initial secret to set. If left blank the
number will be chosen as the next unassigned number, and the secret
will be prompted for. To create an entity with an unset secret,
specify the empty string as the initial secret.
The caller must posess the CREATE_ENTITY capability or be a
GLOBAL_ROOT operator for this command to succeed.`
entityCreateExample = `$ netauth entity create demo
Initial Secret for demo:
New entity created successfully`
)
func init() {
entityCmd.AddCommand(entityCreateCmd)
entityCreateCmd.Flags().IntVar(&newNumber, "number", -1, "Number to assign.")
entityCreateCmd.Flags().StringVar(&newSecret, "initial-secret", "", "Initial secret.")
}
func entityCreateRun(cmd *cobra.Command, args []string) {
// Grab a client
c, err := client.New()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Get the authorization token
t, err := getToken(c, viper.GetString("entity"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
newEntityID = args[0]
if newSecret == "" {
newSecret = getSecret(fmt.Sprintf("Initial Secret for %s: ", newEntityID))
}
result, err := c.NewEntity(newEntityID, int32(newNumber), newSecret, t)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(result.GetMsg())
}