1
1
package pkgcfg
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding/json"
6
+ "html/template"
5
7
"os"
6
8
"path/filepath"
7
9
8
10
"github.com/pkg/errors"
9
11
"go.jetpack.io/devbox/debug"
12
+ "go.jetpack.io/devbox/nix"
10
13
)
11
14
12
15
const localPkgConfigPath = "DEVBOX_LOCAL_PKG_CONFIG"
@@ -19,34 +22,57 @@ type config struct {
19
22
localConfigPath string `json:"-"`
20
23
}
21
24
22
- func CreateFiles (pkg , basePath string ) error {
23
- cfg , err := get (pkg )
25
+ func CreateFiles (pkg , rootDir string ) error {
26
+ cfg , err := get (pkg , rootDir )
24
27
if err != nil {
25
28
return err
26
29
}
30
+ debug .Log ("Creating files for package %q create files" , pkg )
27
31
for name , contentPath := range cfg .CreateFiles {
28
- filePath := filepath .Join (basePath , name )
32
+ filePath := filepath .Join (rootDir , name )
33
+
34
+ dirPath := filepath .Dir (filePath )
35
+ if contentPath == "" {
36
+ dirPath = filePath
37
+ }
38
+ if err = createDir (dirPath ); err != nil {
39
+ return errors .WithStack (err )
40
+ }
41
+
42
+ if contentPath == "" {
43
+ continue
44
+ }
45
+
46
+ debug .Log ("Creating file %q" , filePath )
29
47
content , err := os .ReadFile (filepath .Join (cfg .localConfigPath , contentPath ))
30
48
if err != nil {
31
49
return errors .WithStack (err )
32
50
}
33
- if err = createDir (filepath .Dir (filePath )); err != nil {
34
- return err
51
+ t , err := template .New (name + "-template" ).Parse (string (content ))
52
+ if err != nil {
53
+ return errors .WithStack (err )
54
+ }
55
+ var buf bytes.Buffer
56
+ if err = t .Execute (& buf , map [string ]string {
57
+ "DevboxRoot" : filepath .Join (rootDir , ".devbox" ),
58
+ "DevboxProfileDefault" : filepath .Join (rootDir , nix .ProfilePath ),
59
+ }); err != nil {
60
+ return errors .WithStack (err )
35
61
}
36
- if err := os .WriteFile (filePath , content , 0744 ); err != nil {
62
+ if err := os .WriteFile (filePath , buf . Bytes () , 0744 ); err != nil {
37
63
return errors .WithStack (err )
38
64
}
39
- if err := createSymlink (basePath , filePath ); err != nil {
65
+ if err := createSymlink (rootDir , filePath ); err != nil {
40
66
return err
41
67
}
42
68
}
43
69
return nil
44
70
}
45
71
46
- func Env (pkgs []string ) (map [string ]string , error ) {
72
+ func Env (pkgs []string , rootDir string ) (map [string ]string , error ) {
47
73
env := map [string ]string {}
48
74
for _ , pkg := range pkgs {
49
- cfg , err := get (pkg )
75
+ cfg , err := get (pkg , rootDir )
50
76
if err != nil {
51
77
return nil , err
52
78
}
@@ -57,15 +83,15 @@ func Env(pkgs []string) (map[string]string, error) {
57
83
return env , nil
58
84
}
59
85
60
- func get (pkg string ) (* config , error ) {
86
+ func get (pkg , rootDir string ) (* config , error ) {
61
87
if configPath := os .Getenv (localPkgConfigPath ); configPath != "" {
62
88
debug .Log ("Using local package config at %q" , configPath )
63
- return getLocalConfig (configPath , pkg )
89
+ return getLocalConfig (configPath , pkg , rootDir )
64
90
}
65
91
return & config {}, nil
66
92
}
67
93
68
- func getLocalConfig (configPath , pkg string ) (* config , error ) {
94
+ func getLocalConfig (configPath , pkg , rootDir string ) (* config , error ) {
69
95
pkgConfigPath := filepath .Join (configPath , pkg + ".json" )
70
96
if _ , err := os .Stat (pkgConfigPath ); errors .Is (err , os .ErrNotExist ) {
71
97
// We don't need config for all packages and that's fine
@@ -77,11 +103,23 @@ func getLocalConfig(configPath, pkg string) (*config, error) {
77
103
return nil , errors .WithStack (err )
78
104
}
79
105
cfg := & config {localConfigPath : configPath }
80
- if err = json .Unmarshal (content , cfg ); err != nil {
106
+ t , err := template .New (pkg + "-template" ).Parse (string (content ))
107
+ if err != nil {
108
+ return nil , errors .WithStack (err )
109
+ }
110
+ var buf bytes.Buffer
111
+ if err = t .Execute (& buf , map [string ]string {
112
+ "DevboxRoot" : filepath .Join (rootDir , ".devbox" ),
113
+ "DevboxProfileDefault" : filepath .Join (rootDir , nix .ProfilePath ),
114
+ }); err != nil {
115
+ return nil , errors .WithStack (err )
116
+ }
117
+ if err = json .Unmarshal (buf .Bytes (), cfg ); err != nil {
81
118
return nil , errors .WithStack (err )
82
119
}
83
120
return cfg , nil
84
121
}
122
+
85
123
func createDir (path string ) error {
86
124
if path == "" {
87
125
return nil
0 commit comments