/
casutil.go
90 lines (79 loc) · 2.04 KB
/
casutil.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
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
root = &cobra.Command{
Use: "casutil",
Short: "Utility for Interacting with the Google CAS v1 API",
}
create = &cobra.Command{
Use: "create",
Short: "create CAs and CA pools",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
showConfig()
},
}
list = &cobra.Command{
Use: "list",
Short: "List pools or CAs in a pool",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
showConfig()
},
}
delete = &cobra.Command{
Use: "delete",
Short: "Delete pools or CAs in a pool",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
showConfig()
},
}
enable = &cobra.Command{
Use: "enable",
Short: "Enable CAs",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
showConfig()
},
}
)
func main() {
root.Execute()
}
func init() {
cobra.OnInitialize(flagsFromEnv)
root.PersistentFlags().StringP("project", "p", "", "The GCP project in which CAS operations are performed")
root.PersistentFlags().StringP("location", "l", "", "The GCP location")
createCA.Flags().String("pool", "", "The pool to create the CA in")
listCA.Flags().String("pool", "", "The pool to list CAs in")
deleteCA.Flags().String("pool", "", "The pool to list CAs in")
enableCA.Flags().String("pool", "", "The pool to enable CAs in")
listCerts.Flags().String("pool", "", "The pool to list CAs in")
create.AddCommand(createPool, createCA)
list.AddCommand(listPool, listCA, listCerts)
delete.AddCommand(deletePool, deleteCA)
enable.AddCommand(enableCA)
root.AddCommand(create, list, delete, enable)
}
func fatalIf(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
func flagsFromEnv() {
viper.SetEnvPrefix("casutil")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
}
func showConfig() {
fmt.Fprintf(os.Stderr,
"Using project: %s\n"+
"Using location: %s\n",
viper.GetString("project"),
viper.GetString("location"),
)
}