Skip to content

Commit f1568b8

Browse files
authored
[pkg-config] Read config from github (#264)
## Summary Stacked on #263 This is a quick (and inefficient) implementation that fetches configuration from github. We can improve by caching and fetching multiple files at once. ## How was it tested? DEVBOX_FEATURE_PKG_CONFIG=1 ./dist/devbox shell
1 parent 8a88637 commit f1568b8

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

pkgcfg/pkgcfg.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package pkgcfg
33
import (
44
"bytes"
55
"encoding/json"
6-
"html/template"
6+
"io"
7+
"net/http"
8+
"net/url"
79
"os"
810
"path/filepath"
11+
"text/template"
912

1013
"github.com/pkg/errors"
1114
"go.jetpack.io/devbox/debug"
@@ -44,7 +47,7 @@ func CreateFiles(pkg, rootDir string) error {
4447
}
4548

4649
debug.Log("Creating file %q", filePath)
47-
content, err := os.ReadFile(filepath.Join(cfg.localConfigPath, contentPath))
50+
content, err := getFile(cfg, contentPath)
4851
if err != nil {
4952
return errors.WithStack(err)
5053
}
@@ -88,7 +91,29 @@ func get(pkg, rootDir string) (*config, error) {
8891
debug.Log("Using local package config at %q", configPath)
8992
return getLocalConfig(configPath, pkg, rootDir)
9093
}
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))
92117
}
93118

94119
func getLocalConfig(configPath, pkg, rootDir string) (*config, error) {
@@ -103,7 +128,11 @@ func getLocalConfig(configPath, pkg, rootDir string) (*config, error) {
103128
return nil, errors.WithStack(err)
104129
}
105130
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)
107136
if err != nil {
108137
return nil, errors.WithStack(err)
109138
}
@@ -150,3 +179,19 @@ func createSymlink(root, filePath string) error {
150179
}
151180
return nil
152181
}
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

Comments
 (0)