Skip to content

Commit

Permalink
Send log messages to stderr, not stdout (#339)
Browse files Browse the repository at this point in the history
* Move various logging messages from stdout to stderr

* Move more logging messages to stderr

* Print list of packages & their features when showing repos

* Print list of packages, deployments, and import features in summary of required pallet

* Bump version in `CHANGELOG.md`
  • Loading branch information
ethanjli authored Dec 8, 2024
1 parent 069987b commit 9c3bc6a
Show file tree
Hide file tree
Showing 53 changed files with 1,205 additions and 874 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.8.0-alpha.5 - 2024-12-08

### Changed

- (Breaking change; cli) Log messages are now (supposed to be) printed to stderr instead of stdout; stdout is only meant to be used for outputting values meant to be piped or captured in a subshell.

## 0.8.0-alpha.4 - 2024-12-07

### Added
Expand Down
17 changes: 10 additions & 7 deletions cmd/forklift/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"fmt"
"os"
"sort"

units "github.com/docker/go-units"
Expand All @@ -21,7 +22,7 @@ var errMissingCache = errors.New(
func getMirrorCache(wpath string, ensureWorkspace bool) (*forklift.FSMirrorCache, error) {
if ensureWorkspace {
if !forklift.DirExists(wpath) {
fmt.Printf("Making a new workspace at %s...", wpath)
fmt.Fprintf(os.Stderr, "Making a new workspace at %s...", wpath)
}
if err := forklift.EnsureExists(wpath); err != nil {
return nil, errors.Wrapf(err, "couldn't make new workspace at %s", wpath)
Expand All @@ -41,7 +42,7 @@ func getMirrorCache(wpath string, ensureWorkspace bool) (*forklift.FSMirrorCache
func getPalletCache(wpath string, ensureWorkspace bool) (*forklift.FSPalletCache, error) {
if ensureWorkspace {
if !forklift.DirExists(wpath) {
fmt.Printf("Making a new workspace at %s...", wpath)
fmt.Fprintf(os.Stderr, "Making a new workspace at %s...", wpath)
}
if err := forklift.EnsureExists(wpath); err != nil {
return nil, errors.Wrapf(err, "couldn't make new workspace at %s", wpath)
Expand All @@ -61,7 +62,7 @@ func getPalletCache(wpath string, ensureWorkspace bool) (*forklift.FSPalletCache
func getRepoCache(wpath string, ensureWorkspace bool) (*forklift.FSRepoCache, error) {
if ensureWorkspace {
if !forklift.DirExists(wpath) {
fmt.Printf("Making a new workspace at %s...", wpath)
fmt.Fprintf(os.Stderr, "Making a new workspace at %s...", wpath)
}
if err := forklift.EnsureExists(wpath); err != nil {
return nil, errors.Wrapf(err, "couldn't make new workspace at %s", wpath)
Expand Down Expand Up @@ -100,7 +101,7 @@ func rmAllAction(c *cli.Context) error {
// rm-img

func rmImgAction(_ *cli.Context) error {
fmt.Println("Removing unused Docker container images...")
fmt.Fprintln(os.Stderr, "Removing unused Docker container images...")
client, err := docker.NewClient()
if err != nil {
return errors.Wrap(err, "couldn't make Docker API client")
Expand All @@ -114,12 +115,14 @@ func rmImgAction(_ *cli.Context) error {
})
for _, deleted := range report.ImagesDeleted {
if deleted.Untagged != "" {
fmt.Printf("Untagged %s\n", deleted.Untagged)
fmt.Fprintf(os.Stderr, "Untagged %s\n", deleted.Untagged)
}
if deleted.Deleted != "" {
fmt.Printf("Deleted %s\n", deleted.Deleted)
fmt.Fprintf(os.Stderr, "Deleted %s\n", deleted.Deleted)
}
}
fmt.Printf("Total reclaimed space: %s\n", units.HumanSize(float64(report.SpaceReclaimed)))
fmt.Fprintf(
os.Stderr, "Total reclaimed space: %s\n", units.HumanSize(float64(report.SpaceReclaimed)),
)
return nil
}
3 changes: 2 additions & 1 deletion cmd/forklift/cache/downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"fmt"
"io/fs"
"os"

"github.com/bmatcuk/doublestar/v4"
"github.com/pkg/errors"
Expand Down Expand Up @@ -56,7 +57,7 @@ func rmDlAction(c *cli.Context) error {
return err
}

fmt.Println("Clearing downloads cache...")
fmt.Fprintln(os.Stderr, "Clearing downloads cache...")
if err = cache.Remove(); err != nil {
return errors.Wrap(err, "couldn't clear downloads cache")
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/forklift/cache/git-repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cache

import (
"fmt"
"io"
"os"
"sort"
"strings"

Expand Down Expand Up @@ -40,9 +42,12 @@ func lsGitRepo[GitRepo versionQuerier](
// show-*

func showGitRepo[GitRepo any](
out io.Writer,
cache core.Pather, versionQuery string,
loader func(path, version string) (GitRepo, error),
printer func(indent int, cache core.Pather, gitRepo GitRepo, printHeader bool) error,
fprinter func(
indent int, out io.Writer, cache core.Pather, gitRepo GitRepo, printHeader bool,
) error,
printHeader bool,
) error {
gitRepoPath, version, ok := strings.Cut(versionQuery, "@")
Expand All @@ -55,7 +60,7 @@ func showGitRepo[GitRepo any](
if err != nil {
return errors.Wrapf(err, "couldn't find %s@%s", gitRepoPath, version)
}
return printer(0, cache, gitRepo, printHeader)
return fprinter(0, out, cache, gitRepo, printHeader)
}

// add-*
Expand All @@ -79,8 +84,8 @@ func addGitRepoAction[Cache core.Pather](
); err != nil {
return err
}
fmt.Println()
fmt.Println("Done!")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand All @@ -100,7 +105,7 @@ func rmGitRepoAction[Cache remover](
return err
}

fmt.Printf("Clearing %s cache...\n", gitRepoType)
fmt.Fprintf(os.Stderr, "Clearing %s cache...\n", gitRepoType)
if err = cache.Remove(); err != nil {
return errors.Wrapf(err, "couldn't clear %s cache", gitRepoType)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/forklift/cache/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"fmt"
"os"
"sort"
"strings"

Expand Down Expand Up @@ -59,6 +60,6 @@ func showPkgAction(c *cli.Context) error {
if err != nil {
return errors.Wrapf(err, "couldn't resolve package query %s@%s", pkgPath, version)
}
fcli.PrintPkg(0, cache, pkg)
fcli.FprintPkg(0, os.Stdout, cache, pkg)
return nil
}
6 changes: 5 additions & 1 deletion cmd/forklift/cache/pallets.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cache

import (
"os"

"github.com/urfave/cli/v2"

"github.com/PlanktoScope/forklift/internal/app/forklift"
Expand Down Expand Up @@ -35,5 +37,7 @@ func showPltAction(c *cli.Context) error {
return errMissingCache
}

return showGitRepo(cache, c.Args().First(), cache.LoadFSPallet, fcli.PrintCachedPallet, true)
return showGitRepo(
os.Stdout, cache, c.Args().First(), cache.LoadFSPallet, fcli.FprintCachedPallet, true,
)
}
6 changes: 5 additions & 1 deletion cmd/forklift/cache/repositories.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cache

import (
"os"

"github.com/urfave/cli/v2"

fcli "github.com/PlanktoScope/forklift/internal/app/forklift/cli"
Expand Down Expand Up @@ -35,5 +37,7 @@ func showRepoAction(c *cli.Context) error {
return errMissingCache
}

return showGitRepo(cache, c.Args().First(), cache.LoadFSRepo, fcli.PrintCachedRepo, true)
return showGitRepo(
os.Stdout, cache, c.Args().First(), cache.LoadFSRepo, fcli.FprintCachedRepo, true,
)
}
21 changes: 12 additions & 9 deletions cmd/forklift/dev/plt/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plt

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

Expand All @@ -19,7 +20,7 @@ func lsDeplAction(c *cli.Context) error {
return err
}

return fcli.PrintPalletDepls(0, plt)
return fcli.FprintPalletDepls(0, os.Stdout, plt)
}

// show-depl
Expand All @@ -34,7 +35,7 @@ func showDeplAction(c *cli.Context) error {
return err
}

return fcli.PrintDeplInfo(0, plt, caches.r, c.Args().First())
return fcli.FprintDeplInfo(0, os.Stdout, plt, caches.r, c.Args().First())
}

// locate-depl-pkg
Expand All @@ -49,7 +50,9 @@ func locateDeplPkgAction(c *cli.Context) error {
return err
}

return fcli.PrintDeplPkgLocation(0, plt, caches.r, c.Args().First(), c.Bool("allow-disabled"))
return fcli.FprintDeplPkgLocation(
0, os.Stdout, plt, caches.r, c.Args().First(), c.Bool("allow-disabled"),
)
}

// add-depl
Expand Down Expand Up @@ -86,7 +89,7 @@ func addDeplAction(versions Versions) cli.ActionFunc {
case c.Bool("stage"):
return stageAction(versions)(c)
default:
fmt.Println("Done!")
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand Down Expand Up @@ -121,7 +124,7 @@ func rmDeplAction(versions Versions) cli.ActionFunc {
case c.Bool("stage"):
return stageAction(versions)(c)
default:
fmt.Println("Done!")
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand Down Expand Up @@ -158,7 +161,7 @@ func setDeplPkgAction(versions Versions) cli.ActionFunc {
case c.Bool("stage"):
return stageAction(versions)(c)
default:
fmt.Println("Done!")
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand Down Expand Up @@ -197,7 +200,7 @@ func addDeplFeatAction(versions Versions) cli.ActionFunc {
case c.Bool("stage"):
return stageAction(versions)(c)
default:
fmt.Println("Done!")
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand Down Expand Up @@ -234,7 +237,7 @@ func rmDeplFeatAction(versions Versions) cli.ActionFunc {
case c.Bool("stage"):
return stageAction(versions)(c)
default:
fmt.Println("Done!")
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand Down Expand Up @@ -270,7 +273,7 @@ func setDeplDisabledAction(versions Versions, setting bool) cli.ActionFunc {
case c.Bool("stage"):
return stageAction(versions)(c)
default:
fmt.Println("Done!")
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/forklift/dev/plt/downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plt

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -57,8 +58,8 @@ func cacheDlAction(versions Versions) cli.ActionFunc {
); err != nil {
return err
}
fmt.Println()
fmt.Println("Done!")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
6 changes: 4 additions & 2 deletions cmd/forklift/dev/plt/features.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plt

import (
"os"

"github.com/urfave/cli/v2"

fcli "github.com/PlanktoScope/forklift/internal/app/forklift/cli"
Expand All @@ -18,7 +20,7 @@ func lsFeatAction(c *cli.Context) error {
return err
}

return fcli.PrintPalletFeatures(0, plt)
return fcli.FprintPalletFeatures(0, os.Stdout, plt)
}

// show-feat
Expand All @@ -33,5 +35,5 @@ func showFeatAction(c *cli.Context) error {
return err
}

return fcli.PrintFeatureInfo(0, plt, caches.p, c.Args().First())
return fcli.FprintFeatureInfo(0, os.Stdout, plt, caches.p, c.Args().First())
}
3 changes: 2 additions & 1 deletion cmd/forklift/dev/plt/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plt

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -64,7 +65,7 @@ func showFileAction(c *cli.Context) error {
return err
}

return fcli.PrintFile(plt, c.Args().First())
return fcli.FprintFile(os.Stdout, plt, c.Args().First())
}

// edit-file
Expand Down
9 changes: 6 additions & 3 deletions cmd/forklift/dev/plt/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plt

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -49,14 +50,16 @@ func cacheImgAction(versions Versions) cli.ActionFunc {
return err
}

fmt.Println("Downloading Docker container images specified by the development pallet...")
fmt.Fprintln(
os.Stderr, "Downloading Docker container images specified by the development pallet...",
)
if err := fcli.DownloadImages(
0, plt, caches.r, c.String("platform"), c.Bool("include-disabled"), c.Bool("parallel"),
); err != nil {
return err
}
fmt.Println()
fmt.Println("Done!")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Done!")
return nil
}
}
Loading

0 comments on commit 9c3bc6a

Please sign in to comment.