forked from intelsdi-x/snap
/
main.go
152 lines (133 loc) · 3.43 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"sort"
"strings"
"time"
"golang.org/x/crypto/ssh/terminal"
"github.com/codegangsta/cli"
"github.com/intelsdi-x/snap/mgmt/rest/client"
)
var (
gitversion string
pClient *client.Client
timeFormat = time.RFC1123
err error
)
type usageError struct {
s string
ctx *cli.Context
}
func (ue usageError) Error() string {
return ue.s
}
func (ue usageError) help() {
cli.ShowCommandHelp(ue.ctx, ue.ctx.Command.Name)
}
func newUsageError(s string, ctx *cli.Context) usageError {
return usageError{s, ctx}
}
func main() {
app := cli.NewApp()
app.Name = "snapctl"
app.Version = gitversion
app.Usage = "The open telemetry framework"
app.Flags = []cli.Flag{flURL, flSecure, flAPIVer, flPassword, flConfig}
app.Commands = append(commands, tribeCommands...)
sort.Sort(ByCommand(app.Commands))
app.Before = beforeAction
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
if ue, ok := err.(usageError); ok {
ue.help()
}
os.Exit(1)
}
}
// Run before every command
func beforeAction(ctx *cli.Context) error {
username, password := checkForAuth(ctx)
pClient, err = client.New(ctx.String("url"), ctx.String("api-version"), ctx.Bool("insecure"))
if err != nil {
return fmt.Errorf("%v", err)
}
pClient.Password = password
pClient.Username = username
if err = checkTribeCommand(ctx); err != nil {
return fmt.Errorf("%v", err)
}
return nil
}
// Checks if a tribe command was issued when tribe mode was not
// enabled on the specified snapd instance.
func checkTribeCommand(ctx *cli.Context) error {
tribe := false
for _, a := range os.Args {
for _, command := range tribeCommands {
if strings.Contains(a, command.Name) {
tribe = true
break
}
}
if tribe {
break
}
}
if !tribe {
return nil
}
resp := pClient.ListAgreements()
if resp.Err != nil {
if resp.Err.Error() == "Invalid credentials" {
return resp.Err
}
return fmt.Errorf("Tribe mode must be enabled in snapd to use tribe command")
}
return nil
}
// Checks for authentication flags and returns a username/password
// from the specified settings
func checkForAuth(ctx *cli.Context) (username, password string) {
if ctx.IsSet("password") {
username = "snap" // for now since username is unused but needs to exist for basicAuth
// Prompt for password
fmt.Print("Password:")
pass, err := terminal.ReadPassword(0)
if err != nil {
password = ""
} else {
password = string(pass)
}
// Go to next line after password prompt
fmt.Println()
return
}
//Get config file path in the order:
if ctx.IsSet("config") {
cfg := &config{}
if err := cfg.loadConfig(ctx.String("config")); err != nil {
fmt.Println(err)
}
if cfg.RestAPI.Password != nil {
password = *cfg.RestAPI.Password
} else {
fmt.Println("Error config password field 'rest-auth-pwd' is empty")
}
}
return
}