forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathagent.go
More file actions
76 lines (66 loc) · 2.07 KB
/
agent.go
File metadata and controls
76 lines (66 loc) · 2.07 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
package agent
import (
"os"
"github.com/sirupsen/logrus"
"github.com/skevetter/devpod/cmd/agent/container"
"github.com/skevetter/devpod/cmd/agent/workspace"
"github.com/skevetter/devpod/cmd/flags"
"github.com/skevetter/devpod/pkg/config"
"github.com/skevetter/devpod/pkg/envfile"
"github.com/skevetter/log"
"github.com/spf13/cobra"
)
var AgentExecutedAnnotation = "loft.sh/agent-executed"
// NewAgentCmd returns a new root command.
func NewAgentCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
agentCmd := &cobra.Command{
Use: "agent",
Short: "DevPod Agent",
PersistentPreRunE: func(cobraCmd *cobra.Command, args []string) error {
return AgentPersistentPreRunE(cobraCmd, args, globalFlags)
},
Hidden: true,
}
agentCmd.AddCommand(workspace.NewWorkspaceCmd(globalFlags))
agentCmd.AddCommand(container.NewContainerCmd(globalFlags))
agentCmd.AddCommand(NewDaemonCmd(globalFlags))
agentCmd.AddCommand(NewContainerTunnelCmd(globalFlags))
agentCmd.AddCommand(NewGitCredentialsCmd(globalFlags))
agentCmd.AddCommand(NewGitSSHSignatureCmd(globalFlags))
agentCmd.AddCommand(NewGitSSHSignatureHelperCmd(globalFlags))
agentCmd.AddCommand(NewDockerCredentialsCmd(globalFlags))
return agentCmd
}
func AgentPersistentPreRunE(
cobraCmd *cobra.Command,
args []string,
globalFlags *flags.GlobalFlags,
) error {
// get top level parent
parent := cobraCmd
for parent.Parent() != nil {
parent = parent.Parent()
}
if parent.Annotations == nil {
parent.Annotations = map[string]string{}
}
parent.Annotations[AgentExecutedAnnotation] = "true"
if globalFlags.LogOutput == "json" {
log.Default.SetFormat(log.JSONFormat)
} else {
log.Default.MakeRaw()
}
if globalFlags.Silent {
log.Default.SetLevel(logrus.FatalLevel)
} else if globalFlags.Debug {
log.Default.SetLevel(logrus.DebugLevel)
} else if os.Getenv(config.EnvDebug) == config.BoolTrue {
log.Default.SetLevel(logrus.DebugLevel)
}
if globalFlags.DevPodHome != "" {
_ = os.Setenv(config.EnvHome, globalFlags.DevPodHome)
}
// apply environment
envfile.Apply(log.Default.ErrorStreamOnly())
return nil
}