-
Notifications
You must be signed in to change notification settings - Fork 49
/
shell.go
182 lines (165 loc) 路 4.57 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package shell
import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"github.com/mitchellh/go-ps"
"github.com/willdonnelly/passwd"
"github.com/cashapp/hermit/envars"
"github.com/cashapp/hermit/errors"
"github.com/cashapp/hermit/ui"
"github.com/cashapp/hermit/util"
)
// ActivationConfig for shells.
type ActivationConfig struct {
Root string
Prompt string
Env envars.Envars
}
// Shell abstracts shell specific functionality
type Shell interface {
Name() string
// ActivationHooksInstallation returns the path and shell fragment for injecting activation code to the shell initialisation.
ActivationHooksInstallation() (path, script string, err error)
// ActivationHooksCode returns the shell fragment for activation/deactivation hooks
ActivationHooksCode() (script string, err error)
// ActivationScript for this shell.
ActivationScript(w io.Writer, config ActivationConfig) error
// ApplyEnvars writes the shell fragment required to apply the given envars.
//
// Envars with empty values should be deleted.
ApplyEnvars(w io.Writer, env envars.Envars) error
// DeactivationScript for this shell.
DeactivationScript(w io.Writer) error
}
var (
shells = map[string]Shell{
"zsh": &Zsh{},
"bash": &Bash{},
}
)
// InstallHooks for the given Shell.
func InstallHooks(l *ui.UI, shell Shell) error {
if shell == nil {
return nil
}
fileName, script, err := shell.ActivationHooksInstallation()
if err != nil {
return errors.WithStack(err)
}
patcher := util.NewFilePatcher(hookStartMarker, hookEndMarker)
changed, err := patcher.Patch(fileName, script)
if err != nil {
return errors.WithStack(err)
}
if changed {
l.Infof("Hermit hooks updated to %s", fileName)
} else {
l.Infof("Hermit hooks were already up-to-date in %s", fileName)
}
return nil
}
// PrintHooks for the given shell.
//
// "sha256sums" is the union of all known SHA256 sums for per-environment scripts.
func PrintHooks(shell Shell, sha256sums []string) error {
if shell == nil {
return nil
}
code, err := shell.ActivationHooksCode()
if err != nil {
return errors.WithStack(err)
}
fmt.Fprint(os.Stdout, code)
return nil
}
func Resolve(name string) (Shell, error) {
shell, ok := shells[name]
if !ok {
return nil, errors.Errorf("unknown shell type: %q", name)
}
return shell, nil
}
// Detect the user's shell.
func Detect() (Shell, error) {
// First look for shell in parent processes.
pid := os.Getppid()
for {
process, err := ps.FindProcess(pid)
if err != nil || process == nil {
break
}
name := filepath.Base(process.Executable())
shell, ok := shells[name]
if ok {
return shell, nil
}
pid = process.PPid()
if pid == 0 {
break
}
}
// Next, check for SHELL environment variable.
envShell := filepath.Base(os.Getenv("SHELL"))
if shell, ok := shells[envShell]; ok {
return shell, nil
}
// Next, try to pull the shell from the user's password entry.
u, err := user.Current()
if err != nil {
return nil, errors.Wrap(err, "could not retrieve current user")
}
pw, err := passwd.Parse()
if err != nil {
return nil, errors.Wrap(err, "couldn't locate/parse /etc/passwd database")
}
entry, ok := pw[u.Username]
if !ok {
return nil, errors.Errorf("/etc/passwd file entry for %q is missing", u.Username)
}
if entry.Shell == "" {
return nil, errors.Errorf("/etc/passwd file entry for %q does not contain a shell field", u.Username)
}
shell, ok := shells[filepath.Base(entry.Shell)]
if ok {
return shell, nil
}
return nil, errors.Errorf("unsupported shell %q :(", entry.Shell)
}
// Changes encapsulates changes that need to be applied to the active environment after a change
type Changes struct {
Env envars.Envars
Add envars.Ops
Remove envars.Ops
}
// NewChanges returns a new empty change set
func NewChanges(env envars.Envars) *Changes {
return &Changes{Env: env}
}
// Merge combines two change sets into one
func (c Changes) Merge(o *Changes) *Changes {
if o != nil {
c.Add = append(c.Add, o.Add...)
c.Remove = append(c.Remove, o.Remove...)
}
return &c
}
// ActivateHermit prints out the hermit activation script for the given shell.
func ActivateHermit(w io.Writer, shell Shell, config ActivationConfig) error {
if err := shell.ActivationScript(w, config); err != nil {
return errors.WithStack(err)
}
return nil
}
// DeactivateHermit prints out the hermit deactivation script for the given shell.
func DeactivateHermit(w io.Writer, shell Shell, env envars.Envars) error {
if err := shell.DeactivationScript(w); err != nil {
return errors.WithStack(err)
}
if err := shell.ApplyEnvars(w, env); err != nil {
return errors.WithStack(err)
}
return nil
}