-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdInstall.go
79 lines (64 loc) · 1.64 KB
/
cmdInstall.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
goprompt "github.com/NonLogicalDev/shell.async-goprompt"
"github.com/kballard/go-shellquote"
"github.com/spf13/cobra"
)
var cmdInstallHelpLong = `
# To install run the following:
$ goprompt install zsh >> .zshrc
# [FILE] options:
* zsh
* zsh.plugin
`
const defaultContent = `
# To try for just this session run the following:
$ eval "$({{goprompt}} install zsh)"
# To install run the following:
$ {{goprompt}} install zsh >> .zshrc
`
var (
cmdInstall = &cobra.Command{
Use: "install [FILE]",
Short: "print out the shell script to setup prompt",
Long: trim(cmdInstallHelpLong),
}
)
func init() {
cmdInstall.RunE = cmdInstallRun
}
func replacePlaceholders(content string) string {
goPromptExec := os.Args[0]
if strings.Contains(goPromptExec, "/") {
if fullPath, err := filepath.Abs(goPromptExec); err == nil {
goPromptExec = fullPath
}
}
goPromptExec = shellquote.Join(goPromptExec)
content = strings.ReplaceAll(content, "{{goprompt}}", goPromptExec)
content = strings.ReplaceAll(content, "${GOPROMPT}", goPromptExec)
return content
}
func cmdInstallRun(command *cobra.Command, args []string) error {
argFile := ""
if len(args) > 0 {
argFile = args[0]
}
var content string
switch argFile {
case "zsh":
f, _ := goprompt.ZSHPluginFiles.ReadFile("plugin/zsh/prompt_install.zsh")
content = replacePlaceholders(string(f))
case "zsh.plugin":
f, _ := goprompt.ZSHPluginFiles.ReadFile("plugin/zsh/prompt_asynczle_setup.zsh")
content = replacePlaceholders(string(f))
default:
content = replacePlaceholders(defaultContent)
}
fmt.Println(content)
return nil
}