Skip to content

Commit 20f429f

Browse files
authored
devbox: docs and some cleanup (#2645)
Add package docs and tweak a few things to follow some Go idioms. - Use regular casing for constants. - Rename `Init` to `InitConfig`. Although the signature isn't the same, `Init` is very close the special package init function. - Put Open directly under the Devbox struct.
1 parent 822511a commit 20f429f

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

boxcli/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func InitCmd() *cobra.Command {
2121
func runInitCmd(cmd *cobra.Command, args []string) error {
2222
path := pathArg(args)
2323

24-
_, err := devbox.Init(path)
24+
_, err := devbox.InitConfig(path)
2525
if err != nil {
2626
return errors.WithStack(err)
2727
}

config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import (
88
"go.jetpack.io/devbox/cuecfg"
99
)
1010

11+
// Config defines a devbox environment as JSON.
1112
type Config struct {
13+
// Packages is the slice of Nix packages that devbox makes available in
14+
// its environment.
1215
Packages []string `cue:"[...string]" json:"packages,omitempty"`
1316
}
1417

18+
// ReadConfig reads a devbox config file.
1519
func ReadConfig(path string) (*Config, error) {
1620
cfg := &Config{}
1721
err := cuecfg.ReadFile(path, cfg)
@@ -21,6 +25,7 @@ func ReadConfig(path string) (*Config, error) {
2125
return cfg, nil
2226
}
2327

28+
// WriteConfig saves a devbox config file.
2429
func WriteConfig(path string, cfg *Config) error {
2530
return cuecfg.WriteFile(path, cfg)
2631
}

devbox.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
22
// Use of this source code is governed by the license in the LICENSE file.
33

4+
// Package devbox creates isolated development environments.
45
package devbox
56

67
import (
@@ -14,20 +15,26 @@ import (
1415
"go.jetpack.io/devbox/planner"
1516
)
1617

18+
// configFilename is name of the JSON file that defines a devbox environment.
19+
const configFilename = "devbox.json"
20+
21+
// InitConfig creates a default devbox config file if one doesn't already
22+
// exist.
23+
func InitConfig(dir string) (created bool, err error) {
24+
cfgPath := filepath.Join(dir, configFilename)
25+
return cuecfg.InitFile(cfgPath, &Config{})
26+
}
27+
28+
// Devbox provides an isolated development environment that contains a set of
29+
// Nix packages.
1730
type Devbox struct {
1831
cfg *Config
1932
srcDir string
2033
}
2134

22-
const CONFIG_FILENAME = "devbox.json"
23-
24-
func Init(dir string) (bool, error) {
25-
cfgPath := filepath.Join(dir, CONFIG_FILENAME)
26-
return cuecfg.InitFile(cfgPath, &Config{})
27-
}
28-
35+
// Open opens a devbox by reading the config file in dir.
2936
func Open(dir string) (*Devbox, error) {
30-
cfgPath := filepath.Join(dir, CONFIG_FILENAME)
37+
cfgPath := filepath.Join(dir, configFilename)
3138

3239
cfg, err := ReadConfig(cfgPath)
3340
if err != nil {
@@ -41,30 +48,32 @@ func Open(dir string) (*Devbox, error) {
4148
return box, nil
4249
}
4350

51+
// Add adds a Nix package to the config so that it's available in the devbox
52+
// environment. It validates that the Nix package exists, but doesn't install
53+
// it. Adding a duplicate package is a no-op.
4454
func (d *Devbox) Add(pkgs ...string) error {
4555
// Check packages exist before adding.
4656
for _, pkg := range pkgs {
4757
ok := nix.PkgExists(pkg)
4858
if !ok {
49-
return errors.Errorf("Package %s not found.", pkg)
59+
return errors.Errorf("package %s not found", pkg)
5060
}
5161
}
5262
// Merge and remove duplicates:
5363
merged := append(d.cfg.Packages, pkgs...)
5464
d.cfg.Packages = lo.FindUniques(merged)
55-
56-
// Save config.
5765
return d.saveCfg()
5866
}
5967

68+
// Remove removes Nix packages from the config so that it no longer exists in
69+
// the devbox environment.
6070
func (d *Devbox) Remove(pkgs ...string) error {
6171
// Remove packages from config.
6272
d.cfg.Packages = lo.Without(d.cfg.Packages, pkgs...)
63-
64-
// Save config.
6573
return d.saveCfg()
6674
}
6775

76+
// Build creates a Docker image containing a shell with the devbox environment.
6877
func (d *Devbox) Build(opts ...docker.BuildOptions) error {
6978
defaultFlags := &docker.BuildFlags{
7079
Name: "devbox",
@@ -79,18 +88,24 @@ func (d *Devbox) Build(opts ...docker.BuildOptions) error {
7988
return docker.Build(d.srcDir, opts...)
8089
}
8190

91+
// Plan creates a plan of the actions that devbox will take to generate its
92+
// environment.
8293
func (d *Devbox) Plan() *planner.BuildPlan {
8394
basePlan := &planner.BuildPlan{
8495
Packages: d.cfg.Packages,
8596
}
8697
return planner.MergePlans(basePlan, planner.Plan(d.srcDir))
8798
}
8899

100+
// Generate creates the directory of Nix files and the Dockerfile that define
101+
// the devbox environment.
89102
func (d *Devbox) Generate() error {
90103
plan := d.Plan()
91104
return generate(d.srcDir, plan)
92105
}
93106

107+
// Shell generates the devbox environment and launches nix-shell as a child
108+
// process.
94109
func (d *Devbox) Shell() error {
95110
err := d.Generate()
96111
if err != nil {
@@ -100,7 +115,8 @@ func (d *Devbox) Shell() error {
100115
return nix.Shell(nixDir)
101116
}
102117

118+
// saveCfg writes the config file to the devbox directory.
103119
func (d *Devbox) saveCfg() error {
104-
cfgPath := filepath.Join(d.srcDir, CONFIG_FILENAME)
120+
cfgPath := filepath.Join(d.srcDir, configFilename)
105121
return cuecfg.WriteFile(cfgPath, d.cfg)
106122
}

0 commit comments

Comments
 (0)