-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
99 lines (80 loc) · 1.8 KB
/
common.go
File metadata and controls
99 lines (80 loc) · 1.8 KB
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
package commonCLI
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strings"
homedir "github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
)
// PrintJSON pretty print JSON string
func PrintJSON(str string) {
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, []byte(str), "", " ")
if error != nil {
log.Println("JSON parse error: ", error)
return
}
fmt.Println(string(prettyJSON.Bytes()))
return
}
// OutputJSON displays output of query for alerts in JSON format
func OutputJSON(input interface{}) {
b, err := json.Marshal(input)
if err != nil {
fmt.Println(err)
return
}
PrintJSON(string(b))
}
// ErrorCheck default error check
func ErrorCheck(e error) {
if e != nil {
log.Fatal(e)
}
}
// HomeDir set default config location
func HomeDir() string {
dir, _ := homedir.Dir()
dir += string(os.PathSeparator) + ".edgerc"
return dir
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
// inCLI detect if cli runs from akamai framework
func inCLI(appName string) string {
_, inCLI := os.LookupEnv("AKAMAI_CLI")
if inCLI {
appName = strings.Replace(appName, "-cli-", " ", -1)
}
return appName
}
//GetArgumentStr retrieves argument value for string
func GetArgumentStr(c *cli.Context, errMessage string) string {
id := getArgumentValue(c, errMessage)
return id
}
//GetArgumentInt retrieves argument value for int
func GetArgumentInt(c *cli.Context, errMessage string) int {
id := getArgumentValue(c, errMessage)
ok, val := isStringInt(id)
if !ok {
log.Fatal(errMessage)
}
return val
}
//getArgumentValue retrieves argument value
func getArgumentValue(c *cli.Context, errMessage string) string {
var id string
if c.NArg() == 0 {
log.Fatal(errMessage)
}
id = c.Args().Get(0)
return id
}