Skip to content

Commit 5ed2c80

Browse files
authored
[code-cleanup] Rename devbox.Writer to Stderr (#1507)
## Summary This renames the devbox `Writer` field to the more accurate `Stderr`. It changes a few functions that used to print stuff and now they return strings instead. Also renamed a few functions that where called `PrintX` to something more accurate because they don't actually print anything. How to review: * grep for `devopt.Opts` this will show all the places where we used to pass in stdout sometimes. Specifically @mohsenari please look at integrate.go. @ipince please look at telemetry.go. I think both of these were wrong. * Please take a look at functions that used to print stuff and now return strings (i.e. Info and PrintGlobalList). I tried to keep formatting identical so nothing should change. ## How was it tested? compiles
1 parent bc9de75 commit 5ed2c80

27 files changed

+155
-151
lines changed

devbox.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,36 @@ import (
1616

1717
// Devbox provides an isolated development environment.
1818
type Devbox interface {
19-
// Add adds Nix packages to the config so that they're available in the devbox
20-
// environment. It validates that the Nix packages exist, and install them.
21-
// Adding duplicate packages is a no-op.
2219
Add(ctx context.Context, platforms, excludePlatforms []string, pkgs ...string) error
2320
Config() *devconfig.Config
24-
ProjectDir() string
25-
// Generate creates the directory of Nix files and the Dockerfile that define
26-
// the devbox environment.
27-
Generate(ctx context.Context) error
28-
GenerateDevcontainer(ctx context.Context, generateOpts devopt.GenerateOpts) error
29-
GenerateDockerfile(ctx context.Context, generateOpts devopt.GenerateOpts) error
30-
GenerateEnvrcFile(ctx context.Context, force bool, envFlags devopt.EnvFlags) error
31-
Info(ctx context.Context, pkg string, markdown bool) error
21+
EnvVars(ctx context.Context) ([]string, error)
22+
Info(ctx context.Context, pkg string, markdown bool) (string, error)
3223
Install(ctx context.Context) error
3324
IsEnvEnabled() bool
3425
ListScripts() []string
35-
PrintEnv(ctx context.Context, includeHooks bool) (string, error)
36-
PrintEnvVars(ctx context.Context) ([]string, error)
37-
PrintGlobalList() error
26+
NixEnv(ctx context.Context, includeHooks bool) (string, error)
27+
PackageNames() []string
28+
ProjectDir() string
3829
Pull(ctx context.Context, opts devopt.PullboxOpts) error
3930
Push(ctx context.Context, opts devopt.PullboxOpts) error
40-
// Remove removes Nix packages from the config so that it no longer exists in
41-
// the devbox environment.
4231
Remove(ctx context.Context, pkgs ...string) error
43-
RestartServices(ctx context.Context, services ...string) error
4432
RunScript(ctx context.Context, scriptName string, scriptArgs []string) error
45-
Services() (services.Services, error)
46-
// Shell generates the devbox environment and launches nix-shell as a child process.
4733
Shell(ctx context.Context) error
34+
Update(ctx context.Context, opts devopt.UpdateOpts) error
35+
36+
// Interact with services
37+
ListServices(ctx context.Context) error
38+
RestartServices(ctx context.Context, services ...string) error
39+
Services() (services.Services, error)
4840
StartProcessManager(ctx context.Context, requestedServices []string, background bool, processComposeFileOrDir string) error
4941
StartServices(ctx context.Context, services ...string) error
5042
StopServices(ctx context.Context, allProjects bool, services ...string) error
51-
ListServices(ctx context.Context) error
5243

53-
Update(ctx context.Context, opts devopt.UpdateOpts) error
44+
// Generate files
45+
Generate(ctx context.Context) error
46+
GenerateDevcontainer(ctx context.Context, generateOpts devopt.GenerateOpts) error
47+
GenerateDockerfile(ctx context.Context, generateOpts devopt.GenerateOpts) error
48+
GenerateEnvrcFile(ctx context.Context, force bool, envFlags devopt.EnvFlags) error
5449
}
5550

5651
// Open opens a devbox by reading the config file in dir.

internal/boxcli/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func addCmd() *cobra.Command {
6666
func addCmdFunc(cmd *cobra.Command, args []string, flags addCmdFlags) error {
6767
box, err := devbox.Open(&devopt.Opts{
6868
Dir: flags.config.path,
69-
Writer: cmd.ErrOrStderr(),
69+
Stderr: cmd.ErrOrStderr(),
7070
AllowInsecureAdds: flags.allowInsecure,
7171
})
7272
if err != nil {

internal/boxcli/cloud.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func runCloudShellCmd(cmd *cobra.Command, flags *cloudShellCmdFlags) error {
142142

143143
box, err := devbox.Open(&devopt.Opts{
144144
Dir: flags.config.path,
145-
Writer: cmd.ErrOrStderr(),
145+
Stderr: cmd.ErrOrStderr(),
146146
})
147147
if err != nil {
148148
return errors.WithStack(err)
@@ -158,7 +158,7 @@ func runCloudInit(cmd *cobra.Command, flags *cloudShellCmdFlags) error {
158158

159159
box, err := devbox.Open(&devopt.Opts{
160160
Dir: flags.config.path,
161-
Writer: cmd.ErrOrStderr(),
161+
Stderr: cmd.ErrOrStderr(),
162162
})
163163
if err != nil {
164164
return errors.WithStack(err)

internal/boxcli/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func runGenerateCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
139139
// Check the directory exists.
140140
box, err := devbox.Open(&devopt.Opts{
141141
Dir: flags.config.path,
142-
Writer: cmd.ErrOrStderr(),
142+
Stderr: cmd.ErrOrStderr(),
143143
})
144144
if err != nil {
145145
return errors.WithStack(err)
@@ -167,7 +167,7 @@ func runGenerateDirenvCmd(cmd *cobra.Command, flags *generateCmdFlags) error {
167167

168168
box, err := devbox.Open(&devopt.Opts{
169169
Dir: flags.config.path,
170-
Writer: cmd.ErrOrStderr(),
170+
Stderr: cmd.ErrOrStderr(),
171171
})
172172
if err != nil {
173173
return errors.WithStack(err)

internal/boxcli/global.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ func listGlobalCmdFunc(cmd *cobra.Command, args []string) error {
6868

6969
box, err := devbox.Open(&devopt.Opts{
7070
Dir: path,
71-
Writer: cmd.OutOrStdout(),
71+
Stderr: cmd.ErrOrStderr(),
7272
})
7373
if err != nil {
7474
return errors.WithStack(err)
7575
}
76-
return box.PrintGlobalList()
76+
for _, p := range box.PackageNames() {
77+
fmt.Fprintf(cmd.OutOrStdout(), "* %s\n", p)
78+
}
79+
return nil
7780
}
7881

7982
var globalConfigPath string
@@ -125,7 +128,7 @@ func ensureGlobalEnvEnabled(cmd *cobra.Command, args []string) error {
125128

126129
box, err := devbox.Open(&devopt.Opts{
127130
Dir: path,
128-
Writer: cmd.ErrOrStderr(),
131+
Stderr: cmd.ErrOrStderr(),
129132
})
130133
if err != nil {
131134
return err

internal/boxcli/info.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package boxcli
55

66
import (
7+
"fmt"
8+
79
"github.com/pkg/errors"
810
"github.com/spf13/cobra"
911

@@ -36,11 +38,16 @@ func infoCmd() *cobra.Command {
3638
func infoCmdFunc(cmd *cobra.Command, pkg string, flags infoCmdFlags) error {
3739
box, err := devbox.Open(&devopt.Opts{
3840
Dir: flags.config.path,
39-
Writer: cmd.OutOrStdout(),
41+
Stderr: cmd.ErrOrStderr(),
4042
})
4143
if err != nil {
4244
return errors.WithStack(err)
4345
}
4446

45-
return box.Info(cmd.Context(), pkg, flags.markdown)
47+
info, err := box.Info(cmd.Context(), pkg, flags.markdown)
48+
if err != nil {
49+
return errors.WithStack(err)
50+
}
51+
fmt.Fprint(cmd.OutOrStdout(), info)
52+
return nil
4653
}

internal/boxcli/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func installCmdFunc(cmd *cobra.Command, flags runCmdFlags) error {
3434
// Check the directory exists.
3535
box, err := devbox.Open(&devopt.Opts{
3636
Dir: flags.config.path,
37-
Writer: cmd.ErrOrStderr(),
37+
Stderr: cmd.ErrOrStderr(),
3838
})
3939
if err != nil {
4040
return errors.WithStack(err)

internal/boxcli/integrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ func runIntegrateVSCodeCmd(cmd *cobra.Command) error {
7575
// todo: add error handling - consider sending error message to parent process
7676
box, err := devbox.Open(&devopt.Opts{
7777
Dir: message.ConfigDir,
78-
Writer: cmd.OutOrStdout(),
78+
Stderr: cmd.ErrOrStderr(),
7979
})
8080
if err != nil {
8181
return err
8282
}
8383
// Get env variables of a devbox shell
84-
envVars, err := box.PrintEnvVars(cmd.Context())
84+
envVars, err := box.EnvVars(cmd.Context())
8585
if err != nil {
8686
return err
8787
}

internal/boxcli/midcobra/telemetry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func getPackagesAndCommitHash(c *cobra.Command) ([]string, string) {
9494

9595
box, err := devbox.Open(&devopt.Opts{
9696
Dir: path,
97-
Writer: os.Stdout,
97+
Stderr: os.Stderr,
9898
IgnoreWarnings: true,
9999
})
100100
if err != nil {

internal/boxcli/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func pullCmd() *cobra.Command {
5050
func pullCmdFunc(cmd *cobra.Command, url string, flags *pullCmdFlags) error {
5151
box, err := devbox.Open(&devopt.Opts{
5252
Dir: flags.config.path,
53-
Writer: cmd.ErrOrStderr(),
53+
Stderr: cmd.ErrOrStderr(),
5454
})
5555
if err != nil {
5656
return errors.WithStack(err)

0 commit comments

Comments
 (0)