/
setup.go
108 lines (88 loc) · 2.37 KB
/
setup.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"runtime"
printer "github.com/bcaldwell/go-printer"
"github.com/bcaldwell/devctl/plugins"
"github.com/bcaldwell/devctl/postCommand"
"github.com/bcaldwell/devctl/utilities"
"github.com/spf13/cobra"
)
//go:generate go-bindata -prefix "../" -o bindata.go -pkg cmd ../devctl.sh
// setupCmd represents the setup command
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Sets up devctl dependencies and install shell extensions",
Long: ``,
Run: setup,
}
func init() {
devctlCmd.AddCommand(setupCmd)
}
func setup(cmd *cobra.Command, args []string) {
printer.InfoLineTop()
setupShellScript()
for _, i := range plugins.List {
i.Setup()
}
printer.InfoLineBottom()
}
func setupShellScript() error {
// create devctl.sh file in devctl home folder (HOME/.devctl)
data, err := Asset("devctl.sh")
utilities.Check(err, "Fetching devctl.sh file contents")
fileName := path.Join(devctlHomeFolder, "devctl.sh")
if utilities.Check(err, "Creating file "+fileName) {
return err
}
f, err := os.Create(fileName)
defer f.Close()
_, err = f.Write(data)
if utilities.Check(err, "Writing contents to "+fileName) {
return err
}
profileFile := detectProfile()
fmt.Println(profileFile)
profileFile = path.Join(os.Getenv("HOME"), profileFile)
devctlSourceString := fmt.Sprintf("[ -f %s ] && \\. %s # This loads devctl shell super powers", fileName, fileName)
fileData, err := ioutil.ReadFile(profileFile)
if utilities.HandleError(err) {
return err
}
writeString := utilities.UniqueStringMerge(string(fileData), devctlSourceString)
err = ioutil.WriteFile(profileFile, []byte(writeString), 0644)
postCommand.RunCommand("source " + fileName)
printer.InfoBar(printer.ColoredString("{{green:%s}} Setup shell functions"), printer.SuccessIcon)
return nil
}
func detectProfile() string {
var re = regexp.MustCompile(`-.*\z`)
shell := os.Getenv("SHELL")
shell = path.Base(shell)
// handle possible version suffix like `zsh-5.2`
shell = re.ReplaceAllString(shell, "")
switch shell {
case "zsh":
return ".zshrc"
case "bash":
if runtime.GOOS == "darwin" {
return ".bash_profile"
}
return ".bashrc"
case "csh":
return ".cshrc"
case "fish":
return ".config/fish/config.fish"
case "ksh":
return ".kshrc"
case "sh":
return ".bash_profile"
case "tcsh":
return ".tcshrc"
}
return ".bash_profile"
}