forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
144 lines (128 loc) · 4.32 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cmd
import (
"fmt"
"os"
"os/exec"
"runtime/debug"
"github.com/loft-sh/devpod/cmd/agent"
"github.com/loft-sh/devpod/cmd/context"
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/cmd/helper"
"github.com/loft-sh/devpod/cmd/ide"
"github.com/loft-sh/devpod/cmd/machine"
"github.com/loft-sh/devpod/cmd/pro"
"github.com/loft-sh/devpod/cmd/provider"
"github.com/loft-sh/devpod/cmd/use"
"github.com/loft-sh/devpod/pkg/client/clientimplementation"
"github.com/loft-sh/devpod/pkg/config"
"github.com/loft-sh/devpod/pkg/telemetry"
log2 "github.com/loft-sh/log"
"github.com/loft-sh/log/terminal"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
)
var (
globalFlags *flags.GlobalFlags
)
// NewRootCmd returns a new root command
func NewRootCmd() *cobra.Command {
return &cobra.Command{
Use: "devpod",
Short: "DevPod",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cobraCmd *cobra.Command, args []string) error {
telemetry.Collector.SetCLIData(cobraCmd, globalFlags)
if globalFlags.LogOutput == "json" {
log2.Default.SetFormat(log2.JSONFormat)
} else if globalFlags.LogOutput == "raw" {
log2.Default.SetFormat(log2.RawFormat)
} else if globalFlags.LogOutput != "plain" {
return fmt.Errorf("unrecognized log format %s, needs to be either plain or json", globalFlags.LogOutput)
}
if globalFlags.Silent {
log2.Default.SetLevel(logrus.FatalLevel)
} else if globalFlags.Debug {
log2.Default.SetLevel(logrus.DebugLevel)
} else if os.Getenv(clientimplementation.DevPodDebug) == "true" {
log2.Default.SetLevel(logrus.DebugLevel)
}
if globalFlags.DevPodHome != "" {
_ = os.Setenv(config.DEVPOD_HOME, globalFlags.DevPodHome)
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if globalFlags.DevPodHome != "" {
_ = os.Unsetenv(config.DEVPOD_HOME)
}
return nil
},
}
}
// 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() {
defer func() {
// recover from panic in order to log it via telemetry
if err := recover(); err != nil {
retErr := fmt.Errorf("panic: %v %s", err, debug.Stack())
telemetry.Collector.RecordEndEvent(retErr)
log2.Default.Fatal(retErr)
}
}()
// build the root command
rootCmd := BuildRoot()
// execute command
err := rootCmd.Execute()
telemetry.Collector.RecordEndEvent(err)
if err != nil {
//nolint:all
if sshExitErr, ok := err.(*ssh.ExitError); ok {
os.Exit(sshExitErr.ExitStatus())
}
//nolint:all
if execExitErr, ok := err.(*exec.ExitError); ok {
os.Exit(execExitErr.ExitCode())
}
if globalFlags.Debug {
log2.Default.Fatalf("%+v", err)
} else {
if rootCmd.Annotations == nil || rootCmd.Annotations[agent.AgentExecutedAnnotation] != "true" {
if terminal.IsTerminalIn {
log2.Default.Error("Try using the --debug flag to see a more verbose output")
} else if os.Getenv(telemetry.UIEnvVar) == "true" {
log2.Default.Error("Try enabling Debug mode under Settings to see a more verbose output")
}
}
log2.Default.Fatal(err)
}
}
}
// BuildRoot creates a new root command from the
func BuildRoot() *cobra.Command {
rootCmd := NewRootCmd()
persistentFlags := rootCmd.PersistentFlags()
globalFlags = flags.SetGlobalFlags(persistentFlags)
rootCmd.AddCommand(agent.NewAgentCmd(globalFlags))
rootCmd.AddCommand(provider.NewProviderCmd(globalFlags))
rootCmd.AddCommand(use.NewUseCmd(globalFlags))
rootCmd.AddCommand(helper.NewHelperCmd(globalFlags))
rootCmd.AddCommand(ide.NewIDECmd(globalFlags))
rootCmd.AddCommand(machine.NewMachineCmd(globalFlags))
rootCmd.AddCommand(context.NewContextCmd(globalFlags))
rootCmd.AddCommand(pro.NewProCmd(globalFlags))
rootCmd.AddCommand(NewUpCmd(globalFlags))
rootCmd.AddCommand(NewDeleteCmd(globalFlags))
rootCmd.AddCommand(NewSSHCmd(globalFlags))
rootCmd.AddCommand(NewVersionCmd())
rootCmd.AddCommand(NewStopCmd(globalFlags))
rootCmd.AddCommand(NewListCmd(globalFlags))
rootCmd.AddCommand(NewStatusCmd(globalFlags))
rootCmd.AddCommand(NewBuildCmd(globalFlags))
rootCmd.AddCommand(NewLogsDaemonCmd(globalFlags))
rootCmd.AddCommand(NewExportCmd(globalFlags))
rootCmd.AddCommand(NewImportCmd(globalFlags))
return rootCmd
}