-
Notifications
You must be signed in to change notification settings - Fork 351
/
enter.go
86 lines (74 loc) · 3.13 KB
/
enter.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
package cmd
import (
"github.com/covexo/devspace/pkg/devspace/config/configutil"
"github.com/covexo/devspace/pkg/devspace/kubectl"
"github.com/covexo/devspace/pkg/devspace/services"
"github.com/covexo/devspace/pkg/util/log"
"github.com/spf13/cobra"
)
// EnterCmd is a struct that defines a command call for "enter"
type EnterCmd struct {
flags *EnterCmdFlags
}
// EnterCmdFlags are the flags available for the enter-command
type EnterCmdFlags struct {
service string
namespace string
labelSelector string
container string
switchContext bool
config string
configOverwrite string
}
func init() {
cmd := &EnterCmd{
flags: &EnterCmdFlags{},
}
cobraCmd := &cobra.Command{
Use: "enter",
Short: "Start a new terminal session",
Long: `
#######################################################
################## devspace enter #####################
#######################################################
Execute a command or start a new terminal in your
devspace:
devspace enter
devspace enter bash
devspace enter -s my-service
devspace enter -c my-container
devspace enter bash -n my-namespace
devspace enter bash -l release=test
#######################################################`,
Run: cmd.Run,
}
rootCmd.AddCommand(cobraCmd)
cobraCmd.Flags().StringVarP(&cmd.flags.service, "service", "s", "", "Service name (in config) to select pod/container for terminal")
cobraCmd.Flags().StringVarP(&cmd.flags.container, "container", "c", "", "Container name within pod where to execute command")
cobraCmd.Flags().StringVarP(&cmd.flags.labelSelector, "label-selector", "l", "", "Comma separated key=value selector list (e.g. release=test)")
cobraCmd.Flags().StringVarP(&cmd.flags.namespace, "namespace", "n", "", "Namespace where to select pods")
cobraCmd.Flags().BoolVar(&cmd.flags.switchContext, "switch-context", true, "Switch kubectl context to the devspace context")
cobraCmd.Flags().StringVar(&cmd.flags.config, "config", configutil.ConfigPath, "The devspace config file to load (default: '.devspace/config.yaml'")
cobraCmd.Flags().StringVar(&cmd.flags.configOverwrite, "config-overwrite", configutil.OverwriteConfigPath, "The devspace config overwrite file to load (default: '.devspace/overwrite.yaml'")
}
// Run executes the command logic
func (cmd *EnterCmd) Run(cobraCmd *cobra.Command, args []string) {
if configutil.ConfigPath != cmd.flags.config {
configutil.ConfigPath = cmd.flags.config
// Don't use overwrite config if we use a different config
configutil.OverwriteConfigPath = ""
}
if configutil.OverwriteConfigPath != cmd.flags.configOverwrite {
configutil.OverwriteConfigPath = cmd.flags.configOverwrite
}
log.StartFileLogging()
log.Infof("Loading config %s with overwrite config %s", configutil.ConfigPath, configutil.OverwriteConfigPath)
kubectl, err := kubectl.NewClientWithContextSwitch(cmd.flags.switchContext)
if err != nil {
log.Fatalf("Unable to create new kubectl client: %v", err)
}
err = services.StartTerminal(kubectl, cmd.flags.service, cmd.flags.container, cmd.flags.labelSelector, cmd.flags.namespace, args, make(chan error), log.GetInstance())
if err != nil {
log.Fatal(err)
}
}