forked from redhat-developer/odo
/
terminal.go
114 lines (92 loc) · 2.76 KB
/
terminal.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
package utils
import (
"fmt"
"os"
"github.com/openshift/odo/pkg/odo/genericclioptions"
util2 "github.com/openshift/odo/pkg/util"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
const (
terminalCommandName = "terminal"
ps1 = `
__odo_ps1() {
# Get application context
APP=$(odo application get -q --skip-connection-check)
if [ "$APP" = "" ]; then
APP="<no application>"
fi
# Get current context
COMPONENT=$(odo component get -q --skip-connection-check)
if [ "$COMPONENT" = "" ]; then
COMPONENT="<no component>"
fi
if [ -n "$COMPONENT" ] || [ -n "$APP" ]; then
echo "[${APP}/${COMPONENT}]"
fi
}
`
// Bash output
bashPS1Output = ps1 + `
PS1='$(__odo_ps1)'$PS1
`
// Zsh output
zshPS1Output = ps1 + `
setopt prompt_subst
PROMPT='$(__odo_ps1)'$PROMPT
`
)
var (
terminalExample = ktemplates.Examples(` # Bash terminal PS1 support
source <(%[1]s bash)
# Zsh terminal PS1 support
source <(%[1]s zsh)
`)
terminalLongDesc = ktemplates.LongDesc(`Add odo terminal support to your development environment.
This will append your PS1 environment variable with odo component and application information.`)
supportedShells = map[string]string{"bash": bashPS1Output, "zsh": zshPS1Output}
)
// TerminalOptions encapsulates the options for the command
type TerminalOptions struct {
shellType string
}
// NewTerminalOptions creates a new TerminalOptions instance
func NewTerminalOptions() *TerminalOptions {
return &TerminalOptions{}
}
// Complete completes TerminalOptions after they've been created
func (o *TerminalOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.shellType = args[0]
return
}
// Validate validates the TerminalOptions based on completed values
func (o *TerminalOptions) Validate() (err error) {
if _, ok := supportedShells[o.shellType]; !ok {
return fmt.Errorf("unknown shell type %s, supported shells: %v", o.shellType, getSupportedShells())
}
return
}
// Run contains the logic for the command
func (o *TerminalOptions) Run() (err error) {
// shell type is already validated so retrieval will work
_, err = os.Stdout.Write([]byte(supportedShells[o.shellType]))
return
}
// NewCmdTerminal implements the utils terminal odo command
func NewCmdTerminal(name, fullName string) *cobra.Command {
o := NewTerminalOptions()
terminalCmd := &cobra.Command{
Use: name,
Short: "Add Odo terminal support to your development environment",
Long: terminalLongDesc,
Example: fmt.Sprintf(terminalExample, fullName),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
return terminalCmd
}
func getSupportedShells() []string {
return util2.GetSortedKeys(supportedShells)
}