@@ -19,6 +19,7 @@ package helper
19
19
*/
20
20
21
21
import (
22
+ "embed"
22
23
"fmt"
23
24
"os"
24
25
"path"
@@ -27,98 +28,99 @@ import (
27
28
"github.com/synfinatic/aws-sso-cli/internal/utils"
28
29
)
29
30
30
- import _ "embed"
31
+ //go:embed bash_profile.sh zshrc.sh aws-sso.fish
32
+ var embedFiles embed.FS
31
33
32
- //go:embed bash_profile.sh
33
- var BASH_PROFILE string
34
-
35
- //go:embed zshrc.sh
36
- var ZSH_SCRIPT string
37
-
38
- //go:embed aws-sso.fish
39
- var FISH_SCRIPT string
34
+ type fileMap struct {
35
+ Key string
36
+ Path string
37
+ }
40
38
41
39
// map of shells to their file we edit by default
42
- var SHELL_SCRIPTS = map [string ]string {
43
- "bash" : "~/.bash_profile" ,
44
- "zsh" : "~/.zshrc" ,
45
- "fish" : getFishScript (),
40
+ var SHELL_SCRIPTS = map [string ]fileMap {
41
+ "bash" : {
42
+ Key : "bash_profile.sh" ,
43
+ Path : "~/.bash_profile" ,
44
+ },
45
+ "zsh" : {
46
+ Key : "zshrc.sh" ,
47
+ Path : "~/.zshrc" ,
48
+ },
49
+ "fish" : {
50
+ Key : "aws-sso.fish" ,
51
+ Path : getFishScript (),
52
+ },
46
53
}
47
54
48
55
// ConfigFiles returns a list of all the config files we might edit
49
56
func ConfigFiles () []string {
50
57
ret := []string {}
51
58
52
59
for _ , v := range SHELL_SCRIPTS {
53
- ret = append (ret , utils .GetHomePath (v ))
60
+ ret = append (ret , utils .GetHomePath (v . Path ))
54
61
}
55
62
return ret
56
63
}
57
64
58
- // InstallHelper installs any helper code into our shell startup script(s)
59
- func InstallHelper (shell , script string ) error {
65
+ // getScript takes a shell and returns the contents & path to the shell script
66
+ func getScript (shell string ) ([] byte , string , error ) {
60
67
var err error
68
+ var bytes []byte
69
+ var shellFile fileMap
61
70
var ok bool
62
- var shellFile string
63
71
64
72
if shell == "" {
65
73
if shell , err = detectShell (); err != nil {
66
- return err
74
+ return bytes , "" , err
67
75
}
68
76
}
77
+ log .Debugf ("using %s as our shell" , shell )
69
78
70
- if script == "" {
71
- if shellFile , ok = SHELL_SCRIPTS [shell ]; ! ok {
72
- return fmt .Errorf ("unsupported shell: %s" , shell )
73
- }
74
- script = utils .GetHomePath (shellFile )
79
+ if shellFile , ok = SHELL_SCRIPTS [shell ]; ! ok {
80
+ return bytes , "" , fmt .Errorf ("unsupported shell: %s" , shell )
75
81
}
76
82
77
- switch shell {
78
- case "bash" :
79
- err = installConfigFile (script , BASH_PROFILE )
80
- case "zsh" :
81
- err = installConfigFile (script , ZSH_SCRIPT )
82
- case "fish" :
83
- err = installConfigFile (script , FISH_SCRIPT )
84
- default :
85
- err = fmt .Errorf ("unsupported shell: %s" , shell )
83
+ path := utils .GetHomePath (shellFile .Path )
84
+ bytes , err = embedFiles .ReadFile (shellFile .Key )
85
+ if err != nil {
86
+ return bytes , "" , err
86
87
}
87
-
88
- return err
88
+ return bytes , path , nil
89
89
}
90
90
91
- // UninstallHelper removes any helper code from our shell startup script(s)
92
- func UninstallHelper (shell , script string ) error {
93
- var err error
94
- var ok bool
95
- var shellFile string
96
-
97
- if shell == "" {
98
- if shell , err = detectShell (); err != nil {
99
- return err
100
- }
91
+ // InstallHelper installs any helper code into our shell startup script(s)
92
+ func InstallHelper (shell string , path string ) error {
93
+ c , defaultPath , err := getScript (shell )
94
+ if err != nil {
95
+ return err
101
96
}
102
97
103
- if script == "" {
104
- if shellFile , ok = SHELL_SCRIPTS [shell ]; ! ok {
105
- return fmt .Errorf ("unsupported shell: %s" , shell )
106
- }
107
- script = utils .GetHomePath (shellFile )
98
+ if path == "" {
99
+ err = installConfigFile (defaultPath , c )
100
+ } else {
101
+ err = installConfigFile (path , c )
108
102
}
109
103
110
- switch shell {
111
- case "bash" :
112
- err = uninstallConfigFile (script , BASH_PROFILE )
113
- default :
114
- err = fmt .Errorf ("unsupported shell: %s" , shell )
104
+ return err
105
+ }
106
+
107
+ // UninstallHelper removes any helper code from our shell startup script(s)
108
+ func UninstallHelper (shell string , path string ) error {
109
+ c , defaultPath , err := getScript (shell )
110
+ if err != nil {
111
+ return err
115
112
}
116
113
114
+ if path == "" {
115
+ err = uninstallConfigFile (defaultPath , c )
116
+ } else {
117
+ err = uninstallConfigFile (path , c )
118
+ }
117
119
return err
118
120
}
119
121
120
122
// installConfigFile adds our blob to the given file
121
- func installConfigFile (path , contents string ) error {
123
+ func installConfigFile (path string , contents [] byte ) error {
122
124
var err error
123
125
var exec string
124
126
var fe * utils.FileEdit
@@ -131,7 +133,7 @@ func installConfigFile(path, contents string) error {
131
133
"Executable" : exec ,
132
134
}
133
135
134
- if fe , err = utils .NewFileEdit (BASH_PROFILE , args ); err != nil {
136
+ if fe , err = utils .NewFileEdit (string ( contents ) , args ); err != nil {
135
137
return err
136
138
}
137
139
@@ -143,7 +145,7 @@ func installConfigFile(path, contents string) error {
143
145
}
144
146
145
147
// uninstallConfigFile removes our blob from the given file
146
- func uninstallConfigFile (path , contents string ) error {
148
+ func uninstallConfigFile (path string , contents [] byte ) error {
147
149
var err error
148
150
var fe * utils.FileEdit
149
151
@@ -168,6 +170,7 @@ func detectShell() (string, error) {
168
170
}
169
171
170
172
_ , shell := path .Split (shellPath )
173
+ log .Debugf ("detected configured shell as: %s" , shell )
171
174
return shell , nil
172
175
}
173
176
0 commit comments