Skip to content

Commit 8a88637

Browse files
authored
[pkg-config] Implement basic mariadb config (#263)
## Summary * Adds golang template interpolation to the config and any files it creates * Creates a basic v0.0.1 config for mariafb (can be greatly improved) ## How was it tested? ```bash devbox add mariadb devbox shell mysqld mysql # success! ```
1 parent a30c2f2 commit 8a88637

File tree

10 files changed

+80
-32
lines changed

10 files changed

+80
-32
lines changed

devbox.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ const (
3131
// configFilename is name of the JSON file that defines a devbox environment.
3232
configFilename = "devbox.json"
3333

34-
// profileDir contains the contents of the profile generated via `nix-env --profile profileDir <command>`
35-
// Instead of using directory, prefer using the devbox.profileDir() function that ensures the directory exists.
36-
// TODO savil. Rename to profilePath. This is the symlink of the profile, and not a directory.
37-
profileDir = ".devbox/nix/profile/default"
38-
3934
// shellHistoryFile keeps the history of commands invoked inside devbox shell
4035
shellHistoryFile = ".devbox/shell_history"
4136
)
@@ -236,7 +231,7 @@ func (d *Devbox) Shell() error {
236231
}
237232

238233
if featureflag.Get(featureflag.PKGConfig).Enabled() {
239-
env, err := pkgcfg.Env(plan.DevPackages)
234+
env, err := pkgcfg.Env(plan.DevPackages, d.srcDir)
240235
if err != nil {
241236
return err
242237
}
@@ -343,7 +338,7 @@ func (d *Devbox) generateBuildFiles() error {
343338
}
344339

345340
func (d *Devbox) profileDir() (string, error) {
346-
absPath := filepath.Join(d.srcDir, profileDir)
341+
absPath := filepath.Join(d.srcDir, nix.ProfilePath)
347342
if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
348343
return "", errors.WithStack(err)
349344
}

nix/nix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/pkg/errors"
1515
)
1616

17+
// ProfilePath contains the contents of the profile generated via `nix-env --profile ProfilePath <command>`
18+
// Instead of using directory, prefer using the devbox.ProfilePath() function that ensures the directory exists.
19+
const ProfilePath = ".devbox/nix/profile/default"
20+
1721
func PkgExists(pkg string) bool {
1822
_, found := PkgInfo(pkg)
1923
return found

pkgcfg/package-configuration/dummy-config/echo.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkgcfg/package-configuration/dummy-config/go_1_19.json

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "mariadb",
3+
"version": "0.0.1",
4+
"env": {
5+
"MYSQL_BASEDIR": "{{ .DevboxProfileDefault }}",
6+
"MYSQL_HOME": "{{ .DevboxRoot }}/conf/mariadb/run",
7+
"MYSQL_DATADIR": "{{ .DevboxRoot }}/conf/mariadb/data",
8+
"MYSQL_UNIX_PORT": "{{ .DevboxRoot }}/conf/mariadb/run/mysql.sock",
9+
"MYSQL_PID_FILE": "{{ .DevboxRoot }}/conf/mariadb/run/mysql.pid"
10+
},
11+
"create_files": {
12+
".devbox/conf/mariadb/data": "",
13+
".devbox/conf/mariadb/run": "",
14+
".devbox/conf/mariadb/bin/mysql": "mariadb/mysql",
15+
".devbox/conf/mariadb/bin/mysql_install_db": "mariadb/mysql_install_db",
16+
".devbox/conf/mariadb/bin/mysqladmin": "mariadb/mysqladmin",
17+
".devbox/conf/mariadb/bin/mysqld": "mariadb/mysqld"
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ .DevboxProfileDefault }}/bin/mysql --socket=$MYSQL_UNIX_PORT "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ .DevboxProfileDefault }}/bin/mysql_install_db --auth-root-authentication-method=normal --datadir=$MYSQL_DATADIR --basedir=$MYSQL_BASEDIR --pid-file=$MYSQL_PID_FILE "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ .DevboxProfileDefault }}/bin/mysqld --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE --socket=$MYSQL_UNIX_PORT "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ .DevboxProfileDefault }}/bin/mysqld --datadir=$MYSQL_DATADIR --pid-file=$MYSQL_PID_FILE --socket=$MYSQL_UNIX_PORT "$@" 2> $MYSQL_HOME/mysql.log & MYSQL_PID=$!

pkgcfg/pkgcfg.go

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package pkgcfg
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"html/template"
57
"os"
68
"path/filepath"
79

810
"github.com/pkg/errors"
911
"go.jetpack.io/devbox/debug"
12+
"go.jetpack.io/devbox/nix"
1013
)
1114

1215
const localPkgConfigPath = "DEVBOX_LOCAL_PKG_CONFIG"
@@ -19,34 +22,57 @@ type config struct {
1922
localConfigPath string `json:"-"`
2023
}
2124

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)
2427
if err != nil {
2528
return err
2629
}
30+
debug.Log("Creating files for package %q create files", pkg)
2731
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)
2947
content, err := os.ReadFile(filepath.Join(cfg.localConfigPath, contentPath))
3048
if err != nil {
3149
return errors.WithStack(err)
3250
}
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)
3561
}
36-
if err := os.WriteFile(filePath, content, 0744); err != nil {
62+
if err := os.WriteFile(filePath, buf.Bytes(), 0744); err != nil {
3763
return errors.WithStack(err)
3864
}
39-
if err := createSymlink(basePath, filePath); err != nil {
65+
if err := createSymlink(rootDir, filePath); err != nil {
4066
return err
4167
}
4268
}
4369
return nil
4470
}
4571

46-
func Env(pkgs []string) (map[string]string, error) {
72+
func Env(pkgs []string, rootDir string) (map[string]string, error) {
4773
env := map[string]string{}
4874
for _, pkg := range pkgs {
49-
cfg, err := get(pkg)
75+
cfg, err := get(pkg, rootDir)
5076
if err != nil {
5177
return nil, err
5278
}
@@ -57,15 +83,15 @@ func Env(pkgs []string) (map[string]string, error) {
5783
return env, nil
5884
}
5985

60-
func get(pkg string) (*config, error) {
86+
func get(pkg, rootDir string) (*config, error) {
6187
if configPath := os.Getenv(localPkgConfigPath); configPath != "" {
6288
debug.Log("Using local package config at %q", configPath)
63-
return getLocalConfig(configPath, pkg)
89+
return getLocalConfig(configPath, pkg, rootDir)
6490
}
6591
return &config{}, nil
6692
}
6793

68-
func getLocalConfig(configPath, pkg string) (*config, error) {
94+
func getLocalConfig(configPath, pkg, rootDir string) (*config, error) {
6995
pkgConfigPath := filepath.Join(configPath, pkg+".json")
7096
if _, err := os.Stat(pkgConfigPath); errors.Is(err, os.ErrNotExist) {
7197
// We don't need config for all packages and that's fine
@@ -77,11 +103,23 @@ func getLocalConfig(configPath, pkg string) (*config, error) {
77103
return nil, errors.WithStack(err)
78104
}
79105
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 {
81118
return nil, errors.WithStack(err)
82119
}
83120
return cfg, nil
84121
}
122+
85123
func createDir(path string) error {
86124
if path == "" {
87125
return nil

0 commit comments

Comments
 (0)