@@ -12,36 +12,11 @@ import (
12
12
const localPkgConfigPath = "DEVBOX_LOCAL_PKG_CONFIG"
13
13
14
14
type config struct {
15
- Name string `json:"name"`
16
- Version string `json:"version"`
17
- CreateFiles map [string ]string `json:"create_files"`
18
- Env map [string ]string `json:"env"`
19
- }
20
-
21
- func get (pkg string ) (* config , error ) {
22
- if configPath := os .Getenv (localPkgConfigPath ); configPath != "" {
23
- debug .Log ("Using local package config at %q" , configPath )
24
- return getLocalConfig (configPath , pkg )
25
- }
26
- return & config {}, nil
27
- }
28
-
29
- func getLocalConfig (configPath , pkg string ) (* config , error ) {
30
- configPath = filepath .Join (configPath , pkg + ".json" )
31
- if _ , err := os .Stat (configPath ); errors .Is (err , os .ErrNotExist ) {
32
- // We don't need config for all packages and that's fine
33
- return & config {}, nil
34
- }
35
- debug .Log ("Reading local package config at %q" , configPath )
36
- content , err := os .ReadFile (configPath )
37
- if err != nil {
38
- return nil , errors .WithStack (err )
39
- }
40
- cfg := & config {}
41
- if err = json .Unmarshal (content , cfg ); err != nil {
42
- return nil , errors .WithStack (err )
43
- }
44
- return cfg , nil
15
+ Name string `json:"name"`
16
+ Version string `json:"version"`
17
+ CreateFiles map [string ]string `json:"create_files"`
18
+ Env map [string ]string `json:"env"`
19
+ localConfigPath string `json:"-"`
45
20
}
46
21
47
22
func CreateFiles (pkg , basePath string ) error {
@@ -51,16 +26,19 @@ func CreateFiles(pkg, basePath string) error {
51
26
}
52
27
for name , contentPath := range cfg .CreateFiles {
53
28
filePath := filepath .Join (basePath , name )
54
- if _ , err := os .Stat (filePath ); err == nil {
55
- continue
56
- }
57
- content , err := os .ReadFile (filepath .Join (basePath , contentPath ))
29
+ content , err := os .ReadFile (filepath .Join (cfg .localConfigPath , contentPath ))
58
30
if err != nil {
59
31
return errors .WithStack (err )
60
32
}
33
+ if err = createDir (filepath .Dir (filePath )); err != nil {
34
+ return err
35
+ }
61
36
if err := os .WriteFile (filePath , content , 0744 ); err != nil {
62
37
return errors .WithStack (err )
63
38
}
39
+ if err := createSymlink (basePath , filePath ); err != nil {
40
+ return err
41
+ }
64
42
}
65
43
return nil
66
44
}
@@ -78,3 +56,59 @@ func Env(pkgs []string) (map[string]string, error) {
78
56
}
79
57
return env , nil
80
58
}
59
+
60
+ func get (pkg string ) (* config , error ) {
61
+ if configPath := os .Getenv (localPkgConfigPath ); configPath != "" {
62
+ debug .Log ("Using local package config at %q" , configPath )
63
+ return getLocalConfig (configPath , pkg )
64
+ }
65
+ return & config {}, nil
66
+ }
67
+
68
+ func getLocalConfig (configPath , pkg string ) (* config , error ) {
69
+ pkgConfigPath := filepath .Join (configPath , pkg + ".json" )
70
+ if _ , err := os .Stat (pkgConfigPath ); errors .Is (err , os .ErrNotExist ) {
71
+ // We don't need config for all packages and that's fine
72
+ return & config {}, nil
73
+ }
74
+ debug .Log ("Reading local package config at %q" , pkgConfigPath )
75
+ content , err := os .ReadFile (pkgConfigPath )
76
+ if err != nil {
77
+ return nil , errors .WithStack (err )
78
+ }
79
+ cfg := & config {localConfigPath : configPath }
80
+ if err = json .Unmarshal (content , cfg ); err != nil {
81
+ return nil , errors .WithStack (err )
82
+ }
83
+ return cfg , nil
84
+ }
85
+ func createDir (path string ) error {
86
+ if path == "" {
87
+ return nil
88
+ }
89
+ if err := os .MkdirAll (path , 0755 ); err != nil {
90
+ return errors .WithStack (err )
91
+ }
92
+ return nil
93
+ }
94
+
95
+ func createSymlink (root , filePath string ) error {
96
+ name := filepath .Base (filePath )
97
+ newname := filepath .Join (root , ".devbox/conf/bin" , name )
98
+
99
+ // Create bin path just in case it doesn't exist
100
+ if err := os .MkdirAll (filepath .Join (root , ".devbox/conf/bin" ), 0755 ); err != nil {
101
+ return errors .WithStack (err )
102
+ }
103
+
104
+ if _ , err := os .Stat (newname ); err == nil {
105
+ if err = os .Remove (newname ); err != nil {
106
+ return errors .WithStack (err )
107
+ }
108
+ }
109
+
110
+ if err := os .Symlink (filePath , newname ); err != nil {
111
+ return errors .WithStack (err )
112
+ }
113
+ return nil
114
+ }
0 commit comments