-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot.go
More file actions
78 lines (63 loc) · 1.62 KB
/
root.go
File metadata and controls
78 lines (63 loc) · 1.62 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
package cmd
import (
"encoding/json"
"net/http"
"os"
"time"
"github.com/alecthomas/kong"
)
type Config struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt time.Time `json:"expires_at"`
// maybe APIBaseURL, etc.
}
type AppContext struct {
Config *Config
HTTPClient *http.Client
}
type Globals struct {
ConfigPath string `name:"config" help:"Path to config file" default:"${config_path}"`
}
type CLI struct {
Globals
// Define your CLI structure here: Top Level Commands
Auth AuthCmd `cmd:"" help:"Authentication commands"`
Job JobCmd `cmd:"" help:"Job management commands"`
// Config ConfigCmd `cmd:"" help:"Configuration commands"`
Help HelpCmd `cmd:"" help:"Show help information"`
// Config ConfigCmd `cmd:"" help: "Display Cluster Configuration"`
}
func loadConfig(path string) (*Config, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg Config
if err := json.Unmarshal(b, &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
func Main() {
var cli CLI
// Read command-line arguments
appCtx := &AppContext{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
kctx := kong.Parse(&cli,
kong.Name("mist"),
kong.Description("MIST CLI - Manage your MIST jobs and configurations"),
kong.UsageOnError(),
kong.Vars{"config_path": defaultConfigPath()},
kong.Bind(appCtx),
)
if cfg, err := loadConfig(cli.ConfigPath); err == nil {
appCtx.Config = cfg
} else if !os.IsNotExist(err) {
// config file is present but broken
kctx.FatalIfErrorf(err)
}
err := kctx.Run()
kctx.FatalIfErrorf(err)
}