Skip to content

[add] Print messages about actions taken, or not taken. #1527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions internal/devconfig/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package devconfig

import (
"encoding/json"
"fmt"
"io"
"slices"
"strings"

"github.com/pkg/errors"
"github.com/samber/lo"
Expand Down Expand Up @@ -70,7 +73,7 @@ func (pkgs *Packages) Remove(versionedName string) {
}

// AddPlatforms adds a platform to the list of platforms for a given package
func (pkgs *Packages) AddPlatforms(versionedname string, platforms []string) error {
func (pkgs *Packages) AddPlatforms(writer io.Writer, versionedname string, platforms []string) error {
if len(platforms) == 0 {
return nil
}
Expand All @@ -94,6 +97,10 @@ func (pkgs *Packages) AddPlatforms(versionedname string, platforms []string) err
pkg.VersionedName(),
)
}
fmt.Fprintf(writer,
"Added platform %s to package %s\n", strings.Join(platforms, ", "),
pkg.VersionedName(),
)

pkgs.jsonKind = jsonMap
pkg.kind = regular
Expand All @@ -105,7 +112,7 @@ func (pkgs *Packages) AddPlatforms(versionedname string, platforms []string) err
}

// ExcludePlatforms adds a platform to the list of excluded platforms for a given package
func (pkgs *Packages) ExcludePlatforms(versionedName string, platforms []string) error {
func (pkgs *Packages) ExcludePlatforms(writer io.Writer, versionedName string, platforms []string) error {
if len(platforms) == 0 {
return nil
}
Expand All @@ -127,6 +134,8 @@ func (pkgs *Packages) ExcludePlatforms(versionedName string, platforms []string)
pkg.VersionedName(),
)
}
fmt.Fprintf(writer, "Excluded platform %s for package %s\n", strings.Join(platforms, ", "),
pkg.VersionedName())

pkgs.jsonKind = jsonMap
pkg.kind = regular
Expand Down
25 changes: 23 additions & 2 deletions internal/impl/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
ctx, task := trace.NewTask(ctx, "devboxAdd")
defer task.End()

// Track which packages had no changes so we can report that to the user.
unchangedPackageNames := []string{}

// Only add packages that are not already in config. If same canonical exists,
// replace it.
pkgs := devpkg.PackageFromStrings(lo.Uniq(pkgsNames), d.lockfile)
Expand All @@ -53,6 +56,8 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
if slices.Contains(existingPackageNames, pkg.Versioned()) {
// But we still need to add to addedPackageNames. See its comment.
addedPackageNames = append(addedPackageNames, pkg.Versioned())
unchangedPackageNames = append(unchangedPackageNames, pkg.Versioned())
fmt.Fprintf(d.stderr, "Package %q already in devbox.json\n", pkg.Versioned())
continue
}

Expand Down Expand Up @@ -88,15 +93,16 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
return usererr.New("Package %s not found", pkg.Raw)
}

fmt.Fprintf(d.stderr, "Adding package %q to devbox.json\n", packageNameForConfig)
d.cfg.Packages.Add(packageNameForConfig)
addedPackageNames = append(addedPackageNames, packageNameForConfig)
}

for _, pkg := range addedPackageNames {
if err := d.cfg.Packages.AddPlatforms(pkg, platforms); err != nil {
if err := d.cfg.Packages.AddPlatforms(d.stderr, pkg, platforms); err != nil {
return err
}
if err := d.cfg.Packages.ExcludePlatforms(pkg, excludePlatforms); err != nil {
if err := d.cfg.Packages.ExcludePlatforms(d.stderr, pkg, excludePlatforms); err != nil {
return err
}
}
Expand All @@ -110,6 +116,11 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
if err != nil {
return err
}
// TODO: Now that config packages can have fields,
// we should set this in the config, not the lockfile.
if !p.AllowInsecure {
fmt.Fprintf(d.stderr, "Allowing insecure for %s\n", name)
}
p.AllowInsecure = true
}
}
Expand All @@ -134,6 +145,16 @@ func (d *Devbox) Add(ctx context.Context, platforms, excludePlatforms []string,
}
}

if len(platforms) == 0 && len(excludePlatforms) == 0 && !d.allowInsecureAdds {
if len(unchangedPackageNames) == 1 {
fmt.Fprintf(d.stderr, "Package %q was already in devbox.json and was not modified\n", unchangedPackageNames[0])
} else if len(unchangedPackageNames) > 1 {
fmt.Fprintf(d.stderr, "Packages %s were already in devbox.json and were not modified\n",
strings.Join(unchangedPackageNames, ", "),
)
}
}

return nil
}

Expand Down