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
|
package cmd
import (
"path/filepath"
)
// Shell is the interface that represents the interaction with the host shell.
type Shell interface {
// Hook is the string that gets evaluated into the host shell config and
// setups direnv as a prompt hook.
Hook() (string, error)
// Export outputs the ShellExport as an evaluatable string on the host shell
Export(e ShellExport) (string, error)
// Dump outputs and evaluatable string that sets the env in the host shell
Dump(env Env) (string, error)
}
// ShellExport represents environment variables to add and remove on the host
// shell.
type ShellExport map[string]*string
// Add represents the addition of a new environment variable
func (e ShellExport) Add(key, value string) {
e[key] = &value
}
// Remove represents the removal of a given `key` environment variable.
func (e ShellExport) Remove(key string) {
e[key] = nil
}
var supportedShellList = map[string]Shell{
"bash": Bash,
"elvish": Elvish,
"fish": Fish,
"gha": GitHubActions,
"gzenv": GzEnv,
"json": JSON,
"murex": Murex,
"tcsh": Tcsh,
"vim": Vim,
"zsh": Zsh,
"pwsh": Pwsh,
"systemd": Systemd,
}
// DetectShell returns a Shell instance from the given target.
//
// target is usually $0 and can also be prefixed by `-`
func DetectShell(target string) Shell {
target = filepath.Base(target)
// $0 starts with "-"
if target[0:1] == "-" {
target = target[1:]
}
detectedShell, isValid := supportedShellList[target]
if isValid {
return detectedShell
}
return nil
}
|