Skip to content

Commit 9c3bc6a

Browse files
authored
Send log messages to stderr, not stdout (#339)
* 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`
1 parent 069987b commit 9c3bc6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1205
-874
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.8.0-alpha.5 - 2024-12-08
9+
10+
### Changed
11+
12+
- (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.
13+
814
## 0.8.0-alpha.4 - 2024-12-07
915

1016
### Added

cmd/forklift/cache/cache.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cache
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"sort"
78

89
units "github.com/docker/go-units"
@@ -21,7 +22,7 @@ var errMissingCache = errors.New(
2122
func getMirrorCache(wpath string, ensureWorkspace bool) (*forklift.FSMirrorCache, error) {
2223
if ensureWorkspace {
2324
if !forklift.DirExists(wpath) {
24-
fmt.Printf("Making a new workspace at %s...", wpath)
25+
fmt.Fprintf(os.Stderr, "Making a new workspace at %s...", wpath)
2526
}
2627
if err := forklift.EnsureExists(wpath); err != nil {
2728
return nil, errors.Wrapf(err, "couldn't make new workspace at %s", wpath)
@@ -41,7 +42,7 @@ func getMirrorCache(wpath string, ensureWorkspace bool) (*forklift.FSMirrorCache
4142
func getPalletCache(wpath string, ensureWorkspace bool) (*forklift.FSPalletCache, error) {
4243
if ensureWorkspace {
4344
if !forklift.DirExists(wpath) {
44-
fmt.Printf("Making a new workspace at %s...", wpath)
45+
fmt.Fprintf(os.Stderr, "Making a new workspace at %s...", wpath)
4546
}
4647
if err := forklift.EnsureExists(wpath); err != nil {
4748
return nil, errors.Wrapf(err, "couldn't make new workspace at %s", wpath)
@@ -61,7 +62,7 @@ func getPalletCache(wpath string, ensureWorkspace bool) (*forklift.FSPalletCache
6162
func getRepoCache(wpath string, ensureWorkspace bool) (*forklift.FSRepoCache, error) {
6263
if ensureWorkspace {
6364
if !forklift.DirExists(wpath) {
64-
fmt.Printf("Making a new workspace at %s...", wpath)
65+
fmt.Fprintf(os.Stderr, "Making a new workspace at %s...", wpath)
6566
}
6667
if err := forklift.EnsureExists(wpath); err != nil {
6768
return nil, errors.Wrapf(err, "couldn't make new workspace at %s", wpath)
@@ -100,7 +101,7 @@ func rmAllAction(c *cli.Context) error {
100101
// rm-img
101102

102103
func rmImgAction(_ *cli.Context) error {
103-
fmt.Println("Removing unused Docker container images...")
104+
fmt.Fprintln(os.Stderr, "Removing unused Docker container images...")
104105
client, err := docker.NewClient()
105106
if err != nil {
106107
return errors.Wrap(err, "couldn't make Docker API client")
@@ -114,12 +115,14 @@ func rmImgAction(_ *cli.Context) error {
114115
})
115116
for _, deleted := range report.ImagesDeleted {
116117
if deleted.Untagged != "" {
117-
fmt.Printf("Untagged %s\n", deleted.Untagged)
118+
fmt.Fprintf(os.Stderr, "Untagged %s\n", deleted.Untagged)
118119
}
119120
if deleted.Deleted != "" {
120-
fmt.Printf("Deleted %s\n", deleted.Deleted)
121+
fmt.Fprintf(os.Stderr, "Deleted %s\n", deleted.Deleted)
121122
}
122123
}
123-
fmt.Printf("Total reclaimed space: %s\n", units.HumanSize(float64(report.SpaceReclaimed)))
124+
fmt.Fprintf(
125+
os.Stderr, "Total reclaimed space: %s\n", units.HumanSize(float64(report.SpaceReclaimed)),
126+
)
124127
return nil
125128
}

cmd/forklift/cache/downloads.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cache
33
import (
44
"fmt"
55
"io/fs"
6+
"os"
67

78
"github.com/bmatcuk/doublestar/v4"
89
"github.com/pkg/errors"
@@ -56,7 +57,7 @@ func rmDlAction(c *cli.Context) error {
5657
return err
5758
}
5859

59-
fmt.Println("Clearing downloads cache...")
60+
fmt.Fprintln(os.Stderr, "Clearing downloads cache...")
6061
if err = cache.Remove(); err != nil {
6162
return errors.Wrap(err, "couldn't clear downloads cache")
6263
}

cmd/forklift/cache/git-repositories.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cache
22

33
import (
44
"fmt"
5+
"io"
6+
"os"
57
"sort"
68
"strings"
79

@@ -40,9 +42,12 @@ func lsGitRepo[GitRepo versionQuerier](
4042
// show-*
4143

4244
func showGitRepo[GitRepo any](
45+
out io.Writer,
4346
cache core.Pather, versionQuery string,
4447
loader func(path, version string) (GitRepo, error),
45-
printer func(indent int, cache core.Pather, gitRepo GitRepo, printHeader bool) error,
48+
fprinter func(
49+
indent int, out io.Writer, cache core.Pather, gitRepo GitRepo, printHeader bool,
50+
) error,
4651
printHeader bool,
4752
) error {
4853
gitRepoPath, version, ok := strings.Cut(versionQuery, "@")
@@ -55,7 +60,7 @@ func showGitRepo[GitRepo any](
5560
if err != nil {
5661
return errors.Wrapf(err, "couldn't find %s@%s", gitRepoPath, version)
5762
}
58-
return printer(0, cache, gitRepo, printHeader)
63+
return fprinter(0, out, cache, gitRepo, printHeader)
5964
}
6065

6166
// add-*
@@ -79,8 +84,8 @@ func addGitRepoAction[Cache core.Pather](
7984
); err != nil {
8085
return err
8186
}
82-
fmt.Println()
83-
fmt.Println("Done!")
87+
fmt.Fprintln(os.Stderr)
88+
fmt.Fprintln(os.Stderr, "Done!")
8489
return nil
8590
}
8691
}
@@ -100,7 +105,7 @@ func rmGitRepoAction[Cache remover](
100105
return err
101106
}
102107

103-
fmt.Printf("Clearing %s cache...\n", gitRepoType)
108+
fmt.Fprintf(os.Stderr, "Clearing %s cache...\n", gitRepoType)
104109
if err = cache.Remove(); err != nil {
105110
return errors.Wrapf(err, "couldn't clear %s cache", gitRepoType)
106111
}

cmd/forklift/cache/packages.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cache
22

33
import (
44
"fmt"
5+
"os"
56
"sort"
67
"strings"
78

@@ -59,6 +60,6 @@ func showPkgAction(c *cli.Context) error {
5960
if err != nil {
6061
return errors.Wrapf(err, "couldn't resolve package query %s@%s", pkgPath, version)
6162
}
62-
fcli.PrintPkg(0, cache, pkg)
63+
fcli.FprintPkg(0, os.Stdout, cache, pkg)
6364
return nil
6465
}

cmd/forklift/cache/pallets.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cache
22

33
import (
4+
"os"
5+
46
"github.com/urfave/cli/v2"
57

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

38-
return showGitRepo(cache, c.Args().First(), cache.LoadFSPallet, fcli.PrintCachedPallet, true)
40+
return showGitRepo(
41+
os.Stdout, cache, c.Args().First(), cache.LoadFSPallet, fcli.FprintCachedPallet, true,
42+
)
3943
}

cmd/forklift/cache/repositories.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cache
22

33
import (
4+
"os"
5+
46
"github.com/urfave/cli/v2"
57

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

38-
return showGitRepo(cache, c.Args().First(), cache.LoadFSRepo, fcli.PrintCachedRepo, true)
40+
return showGitRepo(
41+
os.Stdout, cache, c.Args().First(), cache.LoadFSRepo, fcli.FprintCachedRepo, true,
42+
)
3943
}

cmd/forklift/dev/plt/deployments.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plt
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/urfave/cli/v2"
78

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

22-
return fcli.PrintPalletDepls(0, plt)
23+
return fcli.FprintPalletDepls(0, os.Stdout, plt)
2324
}
2425

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

37-
return fcli.PrintDeplInfo(0, plt, caches.r, c.Args().First())
38+
return fcli.FprintDeplInfo(0, os.Stdout, plt, caches.r, c.Args().First())
3839
}
3940

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

52-
return fcli.PrintDeplPkgLocation(0, plt, caches.r, c.Args().First(), c.Bool("allow-disabled"))
53+
return fcli.FprintDeplPkgLocation(
54+
0, os.Stdout, plt, caches.r, c.Args().First(), c.Bool("allow-disabled"),
55+
)
5356
}
5457

5558
// add-depl
@@ -86,7 +89,7 @@ func addDeplAction(versions Versions) cli.ActionFunc {
8689
case c.Bool("stage"):
8790
return stageAction(versions)(c)
8891
default:
89-
fmt.Println("Done!")
92+
fmt.Fprintln(os.Stderr, "Done!")
9093
return nil
9194
}
9295
}
@@ -121,7 +124,7 @@ func rmDeplAction(versions Versions) cli.ActionFunc {
121124
case c.Bool("stage"):
122125
return stageAction(versions)(c)
123126
default:
124-
fmt.Println("Done!")
127+
fmt.Fprintln(os.Stderr, "Done!")
125128
return nil
126129
}
127130
}
@@ -158,7 +161,7 @@ func setDeplPkgAction(versions Versions) cli.ActionFunc {
158161
case c.Bool("stage"):
159162
return stageAction(versions)(c)
160163
default:
161-
fmt.Println("Done!")
164+
fmt.Fprintln(os.Stderr, "Done!")
162165
return nil
163166
}
164167
}
@@ -197,7 +200,7 @@ func addDeplFeatAction(versions Versions) cli.ActionFunc {
197200
case c.Bool("stage"):
198201
return stageAction(versions)(c)
199202
default:
200-
fmt.Println("Done!")
203+
fmt.Fprintln(os.Stderr, "Done!")
201204
return nil
202205
}
203206
}
@@ -234,7 +237,7 @@ func rmDeplFeatAction(versions Versions) cli.ActionFunc {
234237
case c.Bool("stage"):
235238
return stageAction(versions)(c)
236239
default:
237-
fmt.Println("Done!")
240+
fmt.Fprintln(os.Stderr, "Done!")
238241
return nil
239242
}
240243
}
@@ -270,7 +273,7 @@ func setDeplDisabledAction(versions Versions, setting bool) cli.ActionFunc {
270273
case c.Bool("stage"):
271274
return stageAction(versions)(c)
272275
default:
273-
fmt.Println("Done!")
276+
fmt.Fprintln(os.Stderr, "Done!")
274277
return nil
275278
}
276279
}

cmd/forklift/dev/plt/downloads.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plt
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/urfave/cli/v2"
78

@@ -57,8 +58,8 @@ func cacheDlAction(versions Versions) cli.ActionFunc {
5758
); err != nil {
5859
return err
5960
}
60-
fmt.Println()
61-
fmt.Println("Done!")
61+
fmt.Fprintln(os.Stderr)
62+
fmt.Fprintln(os.Stderr, "Done!")
6263
return nil
6364
}
6465
}

cmd/forklift/dev/plt/features.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package plt
22

33
import (
4+
"os"
5+
46
"github.com/urfave/cli/v2"
57

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

21-
return fcli.PrintPalletFeatures(0, plt)
23+
return fcli.FprintPalletFeatures(0, os.Stdout, plt)
2224
}
2325

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

36-
return fcli.PrintFeatureInfo(0, plt, caches.p, c.Args().First())
38+
return fcli.FprintFeatureInfo(0, os.Stdout, plt, caches.p, c.Args().First())
3739
}

0 commit comments

Comments
 (0)