@@ -5,11 +5,13 @@ import (
55 "io/ioutil"
66 "os"
77 "path"
8+ "regexp"
9+ "runtime"
810
9- "github.com/bcaldwell/devctl/postCommand"
1011 printer "github.com/bcaldwell/go-printer"
1112
1213 "github.com/bcaldwell/devctl/plugins"
14+ "github.com/bcaldwell/devctl/postCommand"
1315 "github.com/bcaldwell/devctl/utilities"
1416 "github.com/spf13/cobra"
1517)
@@ -19,14 +21,9 @@ import (
1921// setupCmd represents the setup command
2022var setupCmd = & cobra.Command {
2123 Use : "setup" ,
22- Short : "A brief description of your command" ,
23- Long : `A longer description that spans multiple lines and likely contains examples
24- and usage of using your command. For example:
25-
26- Cobra is a CLI library for Go that empowers applications.
27- This application is a tool to generate the needed files
28- to quickly create a Cobra application.` ,
29- Run : setup ,
24+ Short : "Sets up devctl dependencies and install shell extensions" ,
25+ Long : `` ,
26+ Run : setup ,
3027}
3128
3229func init () {
@@ -35,40 +32,77 @@ func init() {
3532
3633func setup (cmd * cobra.Command , args []string ) {
3734 printer .InfoLineTop ()
35+
36+ setupShellScript ()
37+
38+ for _ , i := range plugins .List {
39+ i .Setup ()
40+ }
41+
42+ printer .InfoLineBottom ()
43+ }
44+
45+ func setupShellScript () error {
3846 // create devctl.sh file in devctl home folder (HOME/.devctl)
3947 data , err := Asset ("devctl.sh" )
4048 utilities .Check (err , "Fetching devctl.sh file contents" )
4149
4250 fileName := path .Join (devctlHomeFolder , "devctl.sh" )
43- utilities .Check (err , "Creating file " + fileName )
51+ if utilities .Check (err , "Creating file " + fileName ) {
52+ return err
53+ }
4454 f , err := os .Create (fileName )
4555 defer f .Close ()
4656
4757 _ , err = f .Write (data )
48- utilities .Check (err , "Writing contents to " + fileName )
58+ if utilities .Check (err , "Writing contents to " + fileName ) {
59+ return err
60+ }
4961
5062 profileFile := detectProfile ()
63+ fmt .Println (profileFile )
5164 profileFile = path .Join (os .Getenv ("HOME" ), profileFile )
5265
5366 devctlSourceString := fmt .Sprintf ("[ -f %s ] && \\ . %s # This loads devctl shell super powers" , fileName , fileName )
5467
5568 fileData , err := ioutil .ReadFile (profileFile )
5669 if utilities .HandleError (err ) {
57- return
70+ return err
5871 }
5972 writeString := utilities .UniqueStringMerge (string (fileData ), devctlSourceString )
6073 err = ioutil .WriteFile (profileFile , []byte (writeString ), 0644 )
6174
6275 postCommand .RunCommand ("source " + fileName )
6376
6477 printer .InfoBar (printer .ColoredString ("{{green:%s}} Setup shell functions" ), printer .SuccessIcon )
65-
66- for _ , i := range plugins .List {
67- i .Setup ()
68- }
69- printer .InfoLineBottom ()
78+ return nil
7079}
7180
7281func detectProfile () string {
73- return ".zshrc"
82+ var re = regexp .MustCompile (`-.*\z` )
83+ shell := os .Getenv ("SHELL" )
84+ shell = path .Base (shell )
85+ // handle possible version suffix like `zsh-5.2`
86+ shell = re .ReplaceAllString (shell , "" )
87+
88+ switch shell {
89+ case "zsh" :
90+ return ".zshrc"
91+ case "bash" :
92+ if runtime .GOOS == "darwin" {
93+ return ".bash_profile"
94+ }
95+ return ".bashrc"
96+ case "csh" :
97+ return ".cshrc"
98+ case "fish" :
99+ return ".config/fish/config.fish"
100+ case "ksh" :
101+ return ".kshrc"
102+ case "sh" :
103+ return ".bash_profile"
104+ case "tcsh" :
105+ return ".tcshrc"
106+ }
107+ return ".bash_profile"
74108}
0 commit comments