@@ -3,9 +3,12 @@ package pkgcfg
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
- "html/template"
6
+ "io"
7
+ "net/http"
8
+ "net/url"
7
9
"os"
8
10
"path/filepath"
11
+ "text/template"
9
12
10
13
"github.com/pkg/errors"
11
14
"go.jetpack.io/devbox/debug"
@@ -44,7 +47,7 @@ func CreateFiles(pkg, rootDir string) error {
44
47
}
45
48
46
49
debug .Log ("Creating file %q" , filePath )
47
- content , err := os . ReadFile ( filepath . Join ( cfg . localConfigPath , contentPath ) )
50
+ content , err := getFile ( cfg , contentPath )
48
51
if err != nil {
49
52
return errors .WithStack (err )
50
53
}
@@ -88,7 +91,29 @@ func get(pkg, rootDir string) (*config, error) {
88
91
debug .Log ("Using local package config at %q" , configPath )
89
92
return getLocalConfig (configPath , pkg , rootDir )
90
93
}
91
- return & config {}, nil
94
+ return getConfig (pkg , rootDir )
95
+ }
96
+
97
+ var baseConfigURL = "https://raw.githubusercontent.com/jetpack-io/devbox/main/pkgcfg/package-configuration"
98
+
99
+ func getConfig (pkg , rootDir string ) (* config , error ) {
100
+ confURL , err := url .JoinPath (baseConfigURL , pkg + ".json" )
101
+ if err != nil {
102
+ return nil , errors .WithStack (err )
103
+ }
104
+ resp , err := http .Get (confURL )
105
+ if err != nil {
106
+ return nil , errors .WithStack (err )
107
+ }
108
+ defer resp .Body .Close ()
109
+ if resp .StatusCode == http .StatusNotFound {
110
+ return & config {}, nil
111
+ }
112
+ content , err := io .ReadAll (resp .Body )
113
+ if err != nil {
114
+ return nil , errors .WithStack (err )
115
+ }
116
+ return buildConfig (& config {}, pkg , rootDir , string (content ))
92
117
}
93
118
94
119
func getLocalConfig (configPath , pkg , rootDir string ) (* config , error ) {
@@ -103,7 +128,11 @@ func getLocalConfig(configPath, pkg, rootDir string) (*config, error) {
103
128
return nil , errors .WithStack (err )
104
129
}
105
130
cfg := & config {localConfigPath : configPath }
106
- t , err := template .New (pkg + "-template" ).Parse (string (content ))
131
+ return buildConfig (cfg , pkg , rootDir , string (content ))
132
+ }
133
+
134
+ func buildConfig (cfg * config , pkg , rootDir , content string ) (* config , error ) {
135
+ t , err := template .New (pkg + "-template" ).Parse (content )
107
136
if err != nil {
108
137
return nil , errors .WithStack (err )
109
138
}
@@ -150,3 +179,19 @@ func createSymlink(root, filePath string) error {
150
179
}
151
180
return nil
152
181
}
182
+
183
+ func getFile (cfg * config , contentPath string ) ([]byte , error ) {
184
+ if cfg .localConfigPath != "" {
185
+ return os .ReadFile (filepath .Join (cfg .localConfigPath , contentPath ))
186
+ }
187
+ confURL , err := url .JoinPath (baseConfigURL , contentPath )
188
+ if err != nil {
189
+ return nil , errors .WithStack (err )
190
+ }
191
+ resp , err := http .Get (confURL )
192
+ if err != nil {
193
+ return nil , errors .WithStack (err )
194
+ }
195
+ defer resp .Body .Close ()
196
+ return io .ReadAll (resp .Body )
197
+ }
0 commit comments