@@ -3,18 +3,50 @@ package actions
3
3
import (
4
4
"os"
5
5
"path/filepath"
6
+ "regexp"
6
7
"strings"
7
8
8
9
"github.com/perrito666/goworkon/environment"
9
10
"github.com/perrito666/goworkon/goinstalls"
10
11
"github.com/perrito666/goworkon/goswitch"
12
+ "github.com/perrito666/goworkon/paths"
11
13
"github.com/pkg/errors"
12
14
)
13
15
14
- func configGet (basePath , name string ) (environment.Config , error ) {
16
+ func globalBins () ([]string , error ) {
17
+ basePath , err := paths .XdgDataConfig ()
18
+ if err != nil {
19
+ return nil , errors .Wrap (err , "retrieving config files" )
20
+ }
15
21
cfgs , err := environment .LoadConfig (basePath )
16
22
if err != nil {
17
- return environment.Config {}, errors .WithStack (err )
23
+ return nil , errors .Wrap (err , "loading configs" )
24
+ }
25
+ globalBin := []string {}
26
+ for _ , cfg := range cfgs {
27
+ if cfg .GlobalBin {
28
+ globalBin = append (globalBin , paths .GoPathBin (cfg .GoPath ))
29
+ }
30
+ }
31
+ return globalBin , nil
32
+
33
+ }
34
+
35
+ var notFoundRe = regexp .MustCompile ("evironment .* not found" )
36
+
37
+ func isNotFound (err error ) bool {
38
+ err = errors .Cause (err )
39
+ return notFoundRe .MatchString (err .Error ())
40
+ }
41
+
42
+ func configGet (name string ) (environment.Config , error ) {
43
+ basePath , err := paths .XdgDataConfig ()
44
+ if err != nil {
45
+ return environment.Config {}, errors .Wrapf (err , "retrieving config for %q" , name )
46
+ }
47
+ cfgs , err := environment .LoadConfig (basePath )
48
+ if err != nil {
49
+ return environment.Config {}, errors .Wrapf (err , "loading config for %q" , name )
18
50
}
19
51
env , ok := cfgs [name ]
20
52
if ! ok {
@@ -25,46 +57,52 @@ func configGet(basePath, name string) (environment.Config, error) {
25
57
26
58
// Switch changes the environment to the specified one
27
59
// if it exists, otherwise its a noop and returns error.
28
- func Switch (basePath , installName string ) error {
29
- cfgs , err := environment . LoadConfig ( basePath )
60
+ func Switch (installName string ) error {
61
+ basePath , err := paths . XdgData ( )
30
62
if err != nil {
31
- return errors .WithStack (err )
63
+ return errors .Wrapf (err , "retrieving config for %q" , installName )
32
64
}
33
- env , err := configGet (basePath , installName )
65
+
66
+ env , err := configGet (installName )
34
67
if err != nil {
35
- return errors .WithStack (err )
68
+ return errors .Wrapf (err , "loading config to switch to %q" , installName )
36
69
}
37
70
38
71
settings , err := environment .LoadSettings (basePath )
39
72
if err != nil {
40
- return errors .Wrap (err , "loading settings" )
73
+ return errors .Wrapf (err , "loading settings to switch to %q" , installName )
41
74
}
42
75
43
- extraBins := []string {}
44
- for _ , cfg := range cfgs {
45
- if cfg .GlobalBin {
46
- extraBins = append (extraBins , filepath .Join (cfg .GoPath , "bin" ))
47
- }
76
+ extraBins , err := globalBins ()
77
+ if err != nil {
78
+ return errors .Wrap (err , "determining global bin paths" )
48
79
}
49
- goswitch .Switch (basePath , env , installName == settings .Default , extraBins )
50
-
51
- return nil
80
+ return errors .Wrapf (goswitch .Switch (env , installName == settings .Default , extraBins ),
81
+ "switching to environment %q" , installName )
52
82
}
53
83
54
84
// Reset will try toreturn the env to its original state.
55
85
func Reset () error {
56
86
return goswitch .Reset ()
57
87
}
58
88
59
- func ensureVersionInstalled (goVersion , basePath , goroot string ) (goinstalls.Version , error ) {
60
- installFolder := filepath .Join (basePath , "installs" )
61
- goFolder := filepath .Join (installFolder , goVersion )
89
+ func ensureVersionInstalled (goVersion , goroot string ) (goinstalls.Version , error ) {
90
+ goFolder , err := paths .XdgDataGoInstallsBinForVerson (goVersion )
91
+ if err != nil {
92
+ return goinstalls.Version {}, errors .Wrapf (err , "determining if %q exists" , goVersion )
93
+ }
94
+ // if the bin folder is not there, the version is not properly
95
+ // installed.
62
96
if _ , err := os .Stat (goFolder ); err == nil {
63
97
return goinstalls .VersionFromString (goVersion )
64
98
}
65
99
versions , err := goinstalls .OnlineAvailableVersions ()
66
100
if err != nil {
67
- return goinstalls.Version {}, errors .WithStack (err )
101
+ return goinstalls.Version {}, errors .Wrap (err , "retrieving available online versions" )
102
+ }
103
+ installFolder , err := paths .XdgDataGoInstalls ()
104
+ if err != nil {
105
+ return goinstalls.Version {}, errors .Wrapf (err , "determining go installs folder to install %q" , goVersion )
68
106
}
69
107
for k , v := range versions {
70
108
if k .CommonVersionString () == goVersion {
@@ -73,7 +111,7 @@ func ensureVersionInstalled(goVersion, basePath, goroot string) (goinstalls.Vers
73
111
return k , nil
74
112
}
75
113
err = goinstalls .InstallVersion (k , v , installFolder , goroot )
76
- return k , errors .WithStack (err )
114
+ return k , errors .Wrapf (err , "installing go %q" , goVersion )
77
115
}
78
116
}
79
117
return goinstalls.Version {}, errors .Errorf ("go version %q not found" , goVersion )
@@ -83,25 +121,26 @@ func ensureVersionInstalled(goVersion, basePath, goroot string) (goinstalls.Vers
83
121
// Create creates the an environment with the passed name
84
122
// in the passed go version, if it exists its a noop and
85
123
// returns an error.
86
- func Create (installName , goVersion , basePath , goPath string , settings environment.Settings ) error {
87
- cfgs , err := environment .LoadConfig (basePath )
88
- if err != nil {
89
- return errors .WithStack (err )
90
- }
91
- if _ , ok := cfgs [installName ]; ok {
92
- return nil
124
+ func Create (installName , goVersion , goPath string , settings environment.Settings ) error {
125
+ _ , err := configGet (installName )
126
+ if err != nil && ! isNotFound (err ) {
127
+ return errors .Wrapf (err , "determining if environment %q exists" , installName )
93
128
}
94
- v , err := ensureVersionInstalled (goVersion , basePath , settings .Goroot )
129
+ v , err := ensureVersionInstalled (goVersion , settings .Goroot )
95
130
if err != nil {
96
- return errors .WithStack (err )
131
+ return errors .Wrapf (err , "installing go %q to create %q environment" , goVersion , installName )
97
132
}
98
133
c := environment.Config {
99
134
Name : installName ,
100
135
GoVersion : v .String (),
101
136
GoPath : goPath ,
102
137
}
103
- if err := c .Save (basePath ); err != nil {
104
- return errors .WithStack (err )
138
+ configPath , err := paths .XdgDataConfig ()
139
+ if err != nil {
140
+ return errors .Wrapf (err , "getting config folder to save %q config" , installName )
141
+ }
142
+ if err := c .Save (configPath ); err != nil {
143
+ return errors .Wrapf (err , "saving %q config" , installName )
105
144
}
106
145
return nil
107
146
}
@@ -119,25 +158,29 @@ func extractEnvironment(attribute string) (string, string, error) {
119
158
}
120
159
121
160
// Set sets <attribute> to <value> in the correct setting or returns an error.
122
- func Set (attribute , value , baseFolder string ) error {
161
+ func Set (attribute , value string ) error {
123
162
environmentName , attribute , err := extractEnvironment (attribute )
124
163
if err != nil {
125
164
return errors .Errorf ("setting %q to %q" , attribute , value )
126
165
}
127
166
if environmentName != "" {
128
- cfg , err := configGet (baseFolder , environmentName )
167
+ cfg , err := configGet (environmentName )
129
168
if err != nil {
130
- return errors .WithStack (err )
169
+ return errors .Wrapf (err , "finding config for %q" , environmentName )
131
170
}
132
- return errors .Wrapf (cfg .Set (attribute , value , baseFolder ), "stting %q to %q" , attribute , value )
171
+ return errors .Wrapf (cfg .Set (attribute , value ), "setting %q to %q" , attribute , value )
133
172
134
173
return errors .New ("setting environment attributes is not implemented" )
135
174
}
136
175
137
- settings , err := environment .LoadSettings (baseFolder )
176
+ settingsFolder , err := paths .XdgData ()
177
+ if err != nil {
178
+ return errors .Wrapf (err , "determining settings folder to set %q" , attribute )
179
+ }
180
+ settings , err := environment .LoadSettings (settingsFolder )
138
181
if err != nil {
139
182
return errors .Wrap (err , "loading settings" )
140
183
}
141
184
142
- return errors .Wrapf (settings .Set (attribute , value , baseFolder ), "setting %q to %q" , attribute , value )
185
+ return errors .Wrapf (settings .Set (attribute , value ), "setting %q to %q" , attribute , value )
143
186
}
0 commit comments