-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
165 lines (157 loc) · 3.51 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
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
const (
DEFAULT_CONFIG_FILE = "config.hcl"
)
var (
// Version of the build. This is injected at build-time.
buildString = "unknown"
defaultConfigFilePath string
)
func init() {
path, err := getFilePathInHomeDir(DEFAULT_CONFIG_FILE)
if err != nil {
log.Fatalf("Failed to get default config file path: %v", err)
}
defaultConfigFilePath = path
}
func main() {
app := &cli.App{
Name: "nomctx",
Usage: "Faster way to switch across multiple Nomad clusters and namespaces",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: defaultConfigFilePath,
Usage: "Path to a config file to load.",
},
},
Before: func(c *cli.Context) error {
cfg, err := initConfig(c.String("config"))
if err != nil {
return fmt.Errorf("error initialising config: %w", err)
}
// Set the config in the app metadata.
c.App.Metadata["cfg"] = cfg
return nil
},
// Default.
Action: func(c *cli.Context) error {
if c.Args().Len() == 0 {
// No command provided.
return handleSwitchCluster(c)
}
return cli.ShowAppHelp(c)
},
Commands: []*cli.Command{
{
Name: "list-clusters",
Usage: "List all clusters",
Action: handleListClusters,
},
{
Name: "list-namespaces",
Usage: "List all namespaces",
Action: handleListNamespaces,
},
{
Name: "set-cluster",
Usage: "Set the current cluster context",
ArgsUsage: "CLUSTER_NAME",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "persist",
Usage: "Persist the environment variables to a .env file",
},
},
Action: handleSetCluster,
},
{
Name: "set-namespace",
Usage: "Set namespace",
ArgsUsage: "NAMESPACE",
Action: handleSetNamespace,
},
{
Name: "switch-cluster",
Usage: "Switch cluster",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "persist",
Usage: "Persist the environment variables to a .env file",
},
},
Action: handleSwitchCluster,
},
{
Name: "switch-namespace",
Usage: "Switch namespace",
Action: handleSwitchNamespace,
},
{
Name: "current-context",
Usage: "Display the current context",
Action: handleCurrentCtx,
},
{
Name: "login",
Usage: "Login to a cluster",
ArgsUsage: "CLUSTER",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cluster",
Usage: "Name of the cluster to login to",
},
&cli.BoolFlag{
Name: "persist",
Usage: "Persist the environment variables to a .env file",
},
},
Action: handleLogin,
},
{
Name: "add-cluster",
Usage: "Add a new cluster to the config",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cluster",
Required: true,
Usage: "Name of the cluster",
},
&cli.StringFlag{
Name: "addr",
Required: true,
Usage: "Address of the cluster",
},
&cli.StringFlag{
Name: "token",
Usage: "Token for the cluster",
},
&cli.StringFlag{
Name: "namespace",
Usage: "Namespace for the cluster",
},
&cli.StringFlag{
Name: "region",
Usage: "Region of the cluster",
},
&cli.StringFlag{
Name: "auth-method",
Usage: "Auth method for the cluster",
},
},
Action: handleAddCluster,
},
},
Version: buildString,
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}