-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (73 loc) · 2.16 KB
/
main.go
File metadata and controls
86 lines (73 loc) · 2.16 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
// Copyright 2025 AgbCloud CLI Contributors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/agbcloud/agbcloud-cli/cmd"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "agb",
Short: "AgbCloud CLI",
Long: "Command line interface for AgbCloud services",
DisableAutoGenTag: true,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
func init() {
// Add command groups
rootCmd.AddGroup(&cobra.Group{ID: "core", Title: "Core Commands"})
rootCmd.AddGroup(&cobra.Group{ID: "management", Title: "Management Commands"})
// Add commands
rootCmd.AddCommand(cmd.VersionCmd)
rootCmd.AddCommand(cmd.LoginCmd)
rootCmd.AddCommand(cmd.LogoutCmd)
rootCmd.AddCommand(cmd.ImageCmd)
// Global flags
rootCmd.CompletionOptions.HiddenDefaultCmd = true
rootCmd.PersistentFlags().BoolP("help", "", false, "help for agb")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose output")
rootCmd.Flags().BoolP("version", "", false, "Display the version of AgbCloud CLI")
// Handle version flag and verbose flag
rootCmd.PersistentPreRun = func(command *cobra.Command, args []string) {
// Set up logging based on verbose flag
verbose, _ := command.Flags().GetBool("verbose")
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
// Set log format to be more CLI-friendly
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
DisableColors: false,
})
}
// Handle version flag
rootCmd.PreRun = func(command *cobra.Command, args []string) {
versionFlag, _ := command.Flags().GetBool("version")
if versionFlag {
err := cmd.VersionCmd.RunE(command, []string{})
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
}
}
func main() {
// Load environment variables
_ = godotenv.Load()
// Execute root command
err := rootCmd.Execute()
if err != nil {
// Exit with error code without logging the error again
// Error messages are already handled by individual commands
os.Exit(1)
}
}