-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
license_put.go
126 lines (98 loc) · 2.45 KB
/
license_put.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
package command
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/pkg/errors"
)
type LicensePutCommand struct {
Meta
testStdin io.Reader
}
func (c *LicensePutCommand) Help() string {
helpText := `
Usage: nomad license put [options]
Puts a new license in Servers and Clients
General Options:
` + generalOptionsUsage() + `
Install a new license from a file:
$ nomad license put <path>
Install a new license from stdin:
$ nomad license put -
`
return strings.TrimSpace(helpText)
}
func (c *LicensePutCommand) Synopsis() string {
return "Install a new Nomad Enterprise License"
}
func (c *LicensePutCommand) Name() string { return "license put" }
func (c *LicensePutCommand) Run(args []string) int {
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
return 1
}
args = flags.Args()
data, err := c.dataFromArgs(args)
if err != nil {
c.Ui.Error(errors.Wrap(err, "Error parsing arguments").Error())
return 1
}
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
_, err = client.Operator().LicensePut(data, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error putting license: %v", err))
return 1
}
c.Ui.Output("Successfully applied license")
return 0
}
func (c *LicensePutCommand) dataFromArgs(args []string) (string, error) {
switch len(args) {
case 0:
return "", fmt.Errorf("Missing LICENSE argument")
case 1:
return LoadDataSource(args[0], c.testStdin)
default:
return "", fmt.Errorf("Too many arguments, exptected 1, got %d", len(args))
}
}
func loadFromFile(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("Failed to read file: %v", err)
}
return string(data), nil
}
func loadFromStdin(testStdin io.Reader) (string, error) {
var stdin io.Reader = os.Stdin
if testStdin != nil {
stdin = testStdin
}
var b bytes.Buffer
if _, err := io.Copy(&b, stdin); err != nil {
return "", fmt.Errorf("Failed to read stdin: %v", err)
}
return b.String(), nil
}
func LoadDataSource(file string, testStdin io.Reader) (string, error) {
// Handle empty quoted shell parameters
if len(file) == 0 {
return "", nil
}
if file == "-" {
if len(file) > 1 {
return file, nil
}
return loadFromStdin(testStdin)
}
return loadFromFile(file)
}