-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (84 loc) · 2.47 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
package main
import (
"fmt"
"os"
"github.com/brevdev/nvcf/cmd"
"github.com/brevdev/nvcf/cmd/auth"
"github.com/brevdev/nvcf/cmd/function"
"github.com/brevdev/nvcf/cmd/gpu"
"github.com/brevdev/nvcf/cmd/preflight"
"github.com/brevdev/nvcf/config"
"github.com/brevdev/nvcf/output"
"github.com/spf13/cobra"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
rootCmd := &cobra.Command{
Use: "nvcf",
Short: "NVIDIA Cloud Functions CLI",
Long: `A command-line interface for managing and interacting with NVIDIA Cloud Functions.
Environment variables:
NVCF_BETA - Set to true to enable beta features
NVCF_SHOW_DOCS_CMD - Set to true to show the docs command
`,
SilenceErrors: true,
PersistentPreRunE: preRunAuthCheck,
Run: func(cmd *cobra.Command, args []string) {
output.PrintASCIIArt(cmd)
err := cmd.Usage()
if err != nil {
return
}
},
DisableAutoGenTag: true,
}
// Add global flags
rootCmd.PersistentFlags().Bool("json", false, "Output results in JSON format")
rootCmd.PersistentFlags().Bool("no-color", false, "Disable color output")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Suppress non-error output")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output and show underlying API calls")
// Add commands
rootCmd.AddCommand(function.FunctionCmd())
rootCmd.AddCommand(gpu.GpuCmd())
// rootCmd.AddCommand(cmd.InvokeCmd())
// rootCmd.AddCommand(cmd.AssetCmd())
rootCmd.AddCommand(auth.AuthCmd())
// rootCmd.AddCommand(cmd.QueueCmd())
// rootCmd.AddCommand(cmd.ClusterGroupCmd())
// rootCmd.AddCommand(cmd.ConfigCmd())
rootCmd.AddCommand(preflight.PreflightCmd())
rootCmd.AddCommand(cmd.DocsCmd())
// // Enable command auto-completion
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.AddCommand(cmd.CompletionCmd())
return rootCmd.Execute()
}
var shouldApplyAuthCheck = map[string]bool{
"function": true,
"gpu": true,
}
func preRunAuthCheck(cmd *cobra.Command, args []string) error {
config.Init()
topLevelCmd := getTopLevelCmd(cmd)
if shouldApplyAuthCheck[topLevelCmd.Name()] {
if !config.IsAuthenticated() {
return fmt.Errorf("you are not authenticated. Please run 'nvcf auth login' first")
}
}
return nil
}
func getTopLevelCmd(cmd *cobra.Command) *cobra.Command {
if cmd == nil || cmd.Parent() == nil {
return cmd
}
parent := cmd.Parent()
if parent.Parent() == nil {
return cmd
}
return getTopLevelCmd(parent)
}