-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
shell.go
134 lines (116 loc) · 3.36 KB
/
shell.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
package cmd
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/util"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"go.uber.org/zap"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra"
)
// ShellOptions is the option of shell command
type ShellOptions struct {
common.Option
TmpDir string
TmpConfigFileName string
}
var shellOptions ShellOptions
func init() {
rootCmd.AddCommand(shellCmd)
}
const (
defaultRcFile = `
if [ -f /etc/bashrc ]; then
source /etc/bashrc
fi
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
if type -t __start_jcli >/dev/null; then true; else
source <(jcli completion)
fi
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
`
zshRcFile = `
if [ -f /etc/zshrc ]; then
source /etc/zshrc
fi
if [ -f ~/.zshrc ]; then
source ~/.zshrc
fi
`
)
var shellCmd = &cobra.Command{
Use: "shell [<name>]",
Short: i18n.T("Create a sub shell so that changes to a specific Jenkins remain local to the shell."),
Long: i18n.T("Create a sub shell so that changes to a specific Jenkins remain local to the shell."),
Aliases: []string{"sh"},
PreRunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
jenkinsName := args[0]
setCurrentJenkins(jenkinsName)
}
if shellOptions.TmpDir, err = ioutil.TempDir("", ".jcli-shell-"); err == nil {
shellOptions.TmpConfigFileName = filepath.Join(shellOptions.TmpDir, "/config")
var data []byte
config := getConfig()
if data, err = yaml.Marshal(&config); err == nil {
err = ioutil.WriteFile(shellOptions.TmpConfigFileName, data, 0644)
}
}
return
},
RunE: func(cmd *cobra.Command, _ []string) (err error) {
fullShell := os.Getenv("SHELL")
shell := filepath.Base(fullShell)
if fullShell == "" && runtime.GOOS == "windows" {
// SHELL is set by git-bash but not cygwin :-(
shell = "cmd.exe"
}
prompt := createNewBashPrompt(os.Getenv("PS1"))
rcFile := defaultRcFile + "\nexport PS1=" + prompt + "\nexport JCLI_CONFIG=\"" + shellOptions.TmpConfigFileName + "\"\n"
tmpRCFileName := shellOptions.TmpDir + "/.bashrc"
err = ioutil.WriteFile(tmpRCFileName, []byte(rcFile), 0760)
if err != nil {
return err
}
logger.Debug("temporary shell profile loaded", zap.String("path", tmpRCFileName))
e := util.ExecCommand(shellOptions.ExecContext, shell, "-rcfile", tmpRCFileName, "-i")
if shell == "zsh" {
env := os.Environ()
env = append(env, fmt.Sprintf("ZDOTDIR=%s", shellOptions.TmpDir))
e = util.ExecCommand(shellOptions.ExecContext, shell, "-i")
e.Env = env
} else if shell == "cmd.exe" {
env := os.Environ()
env = append(env, fmt.Sprintf("JCLI_CONFIG=%s", shellOptions.TmpConfigFileName))
e = util.ExecCommand(shellOptions.ExecContext, shell)
e.Env = env
}
e.Stdout = cmd.OutOrStdout()
e.Stderr = cmd.OutOrStderr()
e.Stdin = os.Stdin
err = e.Run()
return
},
PostRunE: func(cmd *cobra.Command, args []string) (err error) {
err = os.RemoveAll(shellOptions.TmpDir)
return
},
}
func createNewBashPrompt(prompt string) string {
if prompt == "" {
return "'[\\u@\\h \\W jcli> ]\\$ '"
}
if prompt[0] == '"' {
return prompt[0:1] + "jcli> " + prompt[1:]
}
if prompt[0] == '\'' {
return prompt[0:1] + "jcli> " + prompt[1:]
}
return "'jcli> " + prompt + "'"
}