Skip to content

Commit c61ed06

Browse files
authored
[pkgcfg] show shims and env variables in info command. Add markdown flag (#341)
## Summary Improved package configuration info by including created shims and environment variables. Also added a new `--markdown` flag to the info command which produces the output in markdown format. We can use this to automate doc generation. @Lagoja let me know if this is useful. It didn't take me to long, so not a bit deal if you prefer not to merge in. I was hoping this might make it transparent enough that we might be able to turn it on. ## How was it tested? Default (non-markdown): ```bash > devbox info mariadb mariadb-server-10.6.10 mariadb NOTES: * This package creates shims and stores them in .devbox/conf/mariadb/bin * Use mysql_install_db to initialize data directory * Use mysqld to start the server This configuration creates the following helper shims: * .devbox/conf/mariadb/bin/mysqld * .devbox/conf/mariadb/bin/mysql * .devbox/conf/mariadb/bin/mysql_install_db * .devbox/conf/mariadb/bin/mysqladmin This configuration sets the following environment variables: * MYSQL_BASEDIR=/Users/mike/dev/jetpack-repos/devbox/.devbox/nix/profile/default * MYSQL_HOME=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/run * MYSQL_DATADIR=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/data * MYSQL_UNIX_PORT=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/run/mysql.sock * MYSQL_PID_FILE=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/run/mysql.pid To show this information, run `devbox info mariadb` ``` # Markdown version ```bash > devbox info mariadb --markdown ``` ## mariadb-server-10.6.10 ### mariadb NOTES: * This package creates shims and stores them in .devbox/conf/mariadb/bin * Use mysql_install_db to initialize data directory * Use mysqld to start the server ### This configuration creates the following helper shims: * .devbox/conf/mariadb/bin/mysql_install_db * .devbox/conf/mariadb/bin/mysqladmin * .devbox/conf/mariadb/bin/mysqld * .devbox/conf/mariadb/bin/mysql ### This configuration sets the following environment variables: * MYSQL_BASEDIR=/Users/mike/dev/jetpack-repos/devbox/.devbox/nix/profile/default * MYSQL_HOME=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/run * MYSQL_DATADIR=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/data * MYSQL_UNIX_PORT=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/run/mysql.sock * MYSQL_PID_FILE=/Users/mike/dev/jetpack-repos/devbox/.devbox/conf/mariadb/run/mysql.pid To show this information, run `devbox info mariadb`
1 parent 5b1f0f3 commit c61ed06

File tree

4 files changed

+148
-47
lines changed

4 files changed

+148
-47
lines changed

boxcli/info.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
)
1414

1515
type infoCmdFlags struct {
16-
config configFlags
16+
config configFlags
17+
markdown bool
1718
}
1819

1920
func InfoCmd() *cobra.Command {
@@ -30,6 +31,7 @@ func InfoCmd() *cobra.Command {
3031
}
3132

3233
flags.config.register(command)
34+
command.Flags().BoolVar(&flags.markdown, "markdown", false, "Output in markdown format")
3335
return command
3436
}
3537

@@ -39,5 +41,5 @@ func infoCmdFunc(_ *cobra.Command, pkg string, flags infoCmdFlags) error {
3941
return errors.WithStack(err)
4042
}
4143

42-
return box.Info(pkg)
44+
return box.Info(pkg, flags.markdown)
4345
}

devbox.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ func (d *Devbox) Add(pkgs ...string) error {
119119
}
120120
if featureflag.PKGConfig.Enabled() {
121121
for _, pkg := range pkgs {
122-
if err := pkgcfg.PrintReadme(pkg, d.configDir, d.writer, IsDevboxShellEnabled()); err != nil {
122+
if err := pkgcfg.PrintReadme(
123+
pkg,
124+
d.configDir,
125+
d.writer,
126+
IsDevboxShellEnabled(),
127+
false, /*markdown*/
128+
); err != nil {
123129
return err
124130
}
125131
}
@@ -367,16 +373,27 @@ func (d *Devbox) PrintShellEnv() error {
367373
return nil
368374
}
369375

370-
func (d *Devbox) Info(pkg string) error {
376+
func (d *Devbox) Info(pkg string, markdown bool) error {
371377
info, hasInfo := nix.PkgInfo(d.cfg.Nixpkgs.Commit, pkg)
372378
if !hasInfo {
373379
_, err := fmt.Fprintf(d.writer, "Package %s not found\n", pkg)
374380
return errors.WithStack(err)
375381
}
376-
if _, err := fmt.Fprintf(d.writer, "%s\n", info); err != nil {
382+
if _, err := fmt.Fprintf(
383+
d.writer,
384+
"%s%s\n",
385+
lo.Ternary(markdown, "## ", ""),
386+
info,
387+
); err != nil {
377388
return errors.WithStack(err)
378389
}
379-
return pkgcfg.PrintReadme(pkg, d.configDir, d.writer, false /*showSourceEnv*/)
390+
return pkgcfg.PrintReadme(
391+
pkg,
392+
d.configDir,
393+
d.writer,
394+
false, /*showSourceEnv*/
395+
markdown,
396+
)
380397
}
381398

382399
// saveCfg writes the config file to the devbox directory.

pkgcfg/info.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package pkgcfg
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/pkg/errors"
8+
"github.com/samber/lo"
9+
)
10+
11+
func PrintReadme(
12+
pkg, rootDir string,
13+
w io.Writer,
14+
showSourceEnv, markdown bool,
15+
) error {
16+
cfg, err := getConfig(pkg, rootDir)
17+
18+
if err != nil {
19+
return err
20+
}
21+
22+
_, _ = fmt.Fprintln(w, "")
23+
24+
if err = printReadme(cfg, w, markdown); err != nil {
25+
return err
26+
}
27+
28+
if err = printCreateFiles(cfg, w, markdown); err != nil {
29+
return err
30+
}
31+
32+
if err = printEnv(cfg, w, markdown); err != nil {
33+
return err
34+
}
35+
36+
if err = printInfoInstructions(pkg, w); err != nil {
37+
return err
38+
}
39+
40+
if showSourceEnv {
41+
err = printSourceEnvMessage(pkg, rootDir, w)
42+
}
43+
return err
44+
}
45+
46+
func printReadme(cfg *config, w io.Writer, markdown bool) error {
47+
if cfg.Readme == "" {
48+
return nil
49+
}
50+
_, err := fmt.Fprintf(
51+
w,
52+
"%s%s NOTES:\n%s\n\n",
53+
lo.Ternary(markdown, "### ", ""),
54+
cfg.Name,
55+
cfg.Readme,
56+
)
57+
return errors.WithStack(err)
58+
}
59+
60+
func printCreateFiles(cfg *config, w io.Writer, markdown bool) error {
61+
if len(cfg.CreateFiles) == 0 {
62+
return nil
63+
}
64+
65+
shims := ""
66+
for name, src := range cfg.CreateFiles {
67+
if src != "" {
68+
shims += fmt.Sprintf("* %s\n", name)
69+
}
70+
}
71+
72+
_, err := fmt.Fprintf(
73+
w,
74+
"%sThis configuration creates the following helper shims:\n%s\n",
75+
lo.Ternary(markdown, "### ", ""),
76+
shims,
77+
)
78+
return errors.WithStack(err)
79+
}
80+
81+
func printEnv(cfg *config, w io.Writer, markdown bool) error {
82+
if len(cfg.Env) == 0 {
83+
return nil
84+
}
85+
86+
envVars := ""
87+
for name, value := range cfg.Env {
88+
envVars += fmt.Sprintf("* %s=%s\n", name, value)
89+
}
90+
91+
_, err := fmt.Fprintf(
92+
w,
93+
"%sThis configuration sets the following environment variables:\n%s\n",
94+
lo.Ternary(markdown, "### ", ""),
95+
envVars,
96+
)
97+
return errors.WithStack(err)
98+
}
99+
100+
func printInfoInstructions(pkg string, w io.Writer) error {
101+
_, err := fmt.Fprintf(
102+
w,
103+
"To show this information, run `devbox info %s`\n\n",
104+
pkg,
105+
)
106+
return errors.WithStack(err)
107+
}
108+
109+
func printSourceEnvMessage(pkg, rootDir string, w io.Writer) error {
110+
env, err := Env([]string{pkg}, rootDir)
111+
if err != nil {
112+
return err
113+
}
114+
if len(env) > 0 {
115+
_, err = fmt.Fprintf(
116+
w,
117+
"To ensure environment is set, run `source %s/%s/env`\n\n",
118+
confPath,
119+
pkg,
120+
)
121+
}
122+
return errors.WithStack(err)
123+
}

pkgcfg/pkgcfg.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"os"
98
"path/filepath"
109
"text/template"
@@ -159,43 +158,3 @@ func createSymlink(root, filePath string) error {
159158
}
160159
return nil
161160
}
162-
163-
func PrintReadme(pkg, rootDir string, w io.Writer, showSourceEnv bool) error {
164-
cfg, err := getConfig(pkg, rootDir)
165-
if err != nil {
166-
return err
167-
}
168-
if cfg.Readme == "" {
169-
return nil
170-
}
171-
_, err = fmt.Fprintf(
172-
w,
173-
"\n%s NOTES:\n\n%s\n\nto show these notes use `devbox info %s`\n\n",
174-
cfg.Name,
175-
cfg.Readme,
176-
cfg.Name,
177-
)
178-
if err != nil {
179-
return errors.WithStack(err)
180-
}
181-
if showSourceEnv {
182-
err = displaySourceEnvMessage(pkg, rootDir, w)
183-
}
184-
return err
185-
}
186-
187-
func displaySourceEnvMessage(pkg, rootDir string, w io.Writer) error {
188-
env, err := Env([]string{pkg}, rootDir)
189-
if err != nil {
190-
return err
191-
}
192-
if len(env) > 0 {
193-
_, err = fmt.Fprintf(
194-
w,
195-
"\nTo ensure environment is set, run `source %s/%s/env`\n\n",
196-
confPath,
197-
pkg,
198-
)
199-
}
200-
return errors.WithStack(err)
201-
}

0 commit comments

Comments
 (0)