-
Notifications
You must be signed in to change notification settings - Fork 4
/
root.go
130 lines (116 loc) · 4.6 KB
/
root.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
package cmd
import (
"context"
"fmt"
"os"
"github.com/loft-sh/api/v4/pkg/product"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/connect"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/create"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/credits"
cmddefaults "github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/defaults"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/delete"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/devpod"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/generate"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/get"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/importcmd"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/list"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/reset"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/set"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/share"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/sleep"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/use"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/vars"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd/wakeup"
"github.com/loft-sh/loftctl/v4/cmd/loftctl/flags"
"github.com/loft-sh/loftctl/v4/pkg/client"
"github.com/loft-sh/loftctl/v4/pkg/defaults"
"github.com/loft-sh/loftctl/v4/pkg/upgrade"
"github.com/loft-sh/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// NewRootCmd returns a new root command
func NewRootCmd(streamLogger *log.StreamLogger) *cobra.Command {
return &cobra.Command{
Use: "loft",
SilenceUsage: true,
SilenceErrors: true,
Short: product.Replace("Welcome to Loft!"),
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
if globalFlags.Silent {
streamLogger.SetLevel(logrus.FatalLevel)
}
if globalFlags.Debug {
streamLogger.SetLevel(logrus.DebugLevel)
}
if (globalFlags.Config == "" || globalFlags.Config == client.DefaultCacheConfig) && os.Getenv("LOFT_CONFIG") != "" {
globalFlags.Config = os.Getenv("LOFT_CONFIG")
}
if globalFlags.LogOutput == "json" {
streamLogger.SetFormat(log.JSONFormat)
} else if globalFlags.LogOutput == "raw" {
streamLogger.SetFormat(log.RawFormat)
} else if globalFlags.LogOutput != "plain" {
return fmt.Errorf("unrecognized log format %s, needs to be either plain or json", globalFlags.LogOutput)
}
return nil
},
Long: product.Replace(`Loft CLI`) + " - www.loft.sh",
}
}
var globalFlags *flags.GlobalFlags
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
log := log.Default
rootCmd := BuildRoot(log)
// Set version for --version flag
rootCmd.Version = upgrade.GetVersion()
// Execute command
err := rootCmd.ExecuteContext(context.Background())
if err != nil {
if globalFlags.Debug {
log.Fatalf("%+v", err)
} else {
log.Fatal(err)
}
}
}
// BuildRoot creates a new root command from the
func BuildRoot(log *log.StreamLogger) *cobra.Command {
rootCmd := NewRootCmd(log)
persistentFlags := rootCmd.PersistentFlags()
globalFlags = flags.SetGlobalFlags(persistentFlags)
defaults, err := defaults.NewFromPath(defaults.ConfigFolder, defaults.ConfigFile)
if err != nil {
log.Debugf("Error loading defaults: %v", err)
}
// add top level commands
rootCmd.AddCommand(NewStartCmd(globalFlags))
rootCmd.AddCommand(NewLoginCmd(globalFlags))
rootCmd.AddCommand(NewLogoutCmd(globalFlags))
rootCmd.AddCommand(NewUiCmd(globalFlags))
rootCmd.AddCommand(NewTokenCmd(globalFlags))
rootCmd.AddCommand(NewBackupCmd(globalFlags))
rootCmd.AddCommand(NewCompletionCmd(rootCmd, globalFlags))
rootCmd.AddCommand(NewUpgradeCmd())
// add subcommands
rootCmd.AddCommand(list.NewListCmd(globalFlags))
rootCmd.AddCommand(use.NewUseCmd(globalFlags, defaults))
rootCmd.AddCommand(create.NewCreateCmd(globalFlags, defaults))
rootCmd.AddCommand(delete.NewDeleteCmd(globalFlags, defaults))
rootCmd.AddCommand(generate.NewGenerateCmd(globalFlags))
rootCmd.AddCommand(get.NewGetCmd(globalFlags, defaults))
rootCmd.AddCommand(vars.NewVarsCmd(globalFlags))
rootCmd.AddCommand(share.NewShareCmd(globalFlags, defaults))
rootCmd.AddCommand(set.NewSetCmd(globalFlags, defaults))
rootCmd.AddCommand(reset.NewResetCmd(globalFlags))
rootCmd.AddCommand(sleep.NewSleepCmd(globalFlags, defaults))
rootCmd.AddCommand(wakeup.NewWakeUpCmd(globalFlags, defaults))
rootCmd.AddCommand(importcmd.NewImportCmd(globalFlags))
rootCmd.AddCommand(connect.NewConnectCmd(globalFlags))
rootCmd.AddCommand(cmddefaults.NewDefaultsCmd(globalFlags, defaults))
rootCmd.AddCommand(devpod.NewDevPodCmd(globalFlags))
rootCmd.AddCommand(credits.NewCreditsCmd())
return rootCmd
}