Skip to content

Commit

Permalink
Merge pull request #5783 from thaJeztah/newline_galore
Browse files Browse the repository at this point in the history
assorted cleanups: use Println, rename vars for consistency
  • Loading branch information
thaJeztah authored Feb 3, 2025
2 parents 4d7fe01 + 987da09 commit 4808d1b
Show file tree
Hide file tree
Showing 71 changed files with 382 additions and 367 deletions.
26 changes: 13 additions & 13 deletions cli-plugins/examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import (
)

func main() {
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
plugin.Run(func(dockerCLI command.Cli) *cobra.Command {
goodbye := &cobra.Command{
Use: "goodbye",
Short: "Say Goodbye instead of Hello",
Run: func(cmd *cobra.Command, _ []string) {
fmt.Fprintln(dockerCli.Out(), "Goodbye World!")
_, _ = fmt.Fprintln(dockerCLI.Out(), "Goodbye World!")
},
}
apiversion := &cobra.Command{
Use: "apiversion",
Short: "Print the API version of the server",
RunE: func(_ *cobra.Command, _ []string) error {
cli := dockerCli.Client()
ping, err := cli.Ping(context.Background())
apiClient := dockerCLI.Client()
ping, err := apiClient.Ping(context.Background())
if err != nil {
return err
}
fmt.Println(ping.APIVersion)
_, _ = fmt.Println(ping.APIVersion)
return nil
},
}
Expand All @@ -38,7 +38,7 @@ func main() {
Use: "exitstatus2",
Short: "Exit with status 2",
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Fprintln(dockerCli.Err(), "Exiting with error status 2")
_, _ = fmt.Fprintln(dockerCLI.Err(), "Exiting with error status 2")
os.Exit(2)
return nil
},
Expand All @@ -56,33 +56,33 @@ func main() {
return err
}
if preRun {
fmt.Fprintf(dockerCli.Err(), "Plugin PersistentPreRunE called")
_, _ = fmt.Fprintln(dockerCLI.Err(), "Plugin PersistentPreRunE called")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if debug {
fmt.Fprintf(dockerCli.Err(), "Plugin debug mode enabled")
_, _ = fmt.Fprintln(dockerCLI.Err(), "Plugin debug mode enabled")
}

switch optContext {
case "Christmas":
fmt.Fprintf(dockerCli.Out(), "Merry Christmas!\n")
_, _ = fmt.Fprintln(dockerCLI.Out(), "Merry Christmas!")
return nil
case "":
// nothing
}

if who == "" {
who, _ = dockerCli.ConfigFile().PluginConfig("helloworld", "who")
who, _ = dockerCLI.ConfigFile().PluginConfig("helloworld", "who")
}
if who == "" {
who = "World"
}

fmt.Fprintf(dockerCli.Out(), "Hello %s!\n", who)
dockerCli.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
return dockerCli.ConfigFile().Save()
_, _ = fmt.Fprintln(dockerCLI.Out(), "Hello", who)
dockerCLI.ConfigFile().SetPluginConfig("helloworld", "lastwho", who)
return dockerCLI.ConfigFile().Save()
},
}

Expand Down
4 changes: 2 additions & 2 deletions cli-plugins/hooks/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func PrintNextSteps(out io.Writer, messages []string) {
if len(messages) == 0 {
return
}
fmt.Fprintln(out, aec.Bold.Apply("\nWhat's next:"))
_, _ = fmt.Fprintln(out, aec.Bold.Apply("\nWhat's next:"))
for _, n := range messages {
_, _ = fmt.Fprintf(out, " %s\n", n)
_, _ = fmt.Fprintln(out, " ", n)
}
}
6 changes: 3 additions & 3 deletions cli/command/checkpoint/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}

func runCreate(ctx context.Context, dockerCli command.Cli, opts createOptions) error {
err := dockerCli.Client().CheckpointCreate(ctx, opts.container, checkpoint.CreateOptions{
func runCreate(ctx context.Context, dockerCLI command.Cli, opts createOptions) error {
err := dockerCLI.Client().CheckpointCreate(ctx, opts.container, checkpoint.CreateOptions{
CheckpointID: opts.checkpoint,
CheckpointDir: opts.checkpointDir,
Exit: !opts.leaveRunning,
Expand All @@ -50,6 +50,6 @@ func runCreate(ctx context.Context, dockerCli command.Cli, opts createOptions) e
return err
}

fmt.Fprintf(dockerCli.Out(), "%s\n", opts.checkpoint)
_, _ = fmt.Fprintln(dockerCLI.Out(), opts.checkpoint)
return nil
}
4 changes: 2 additions & 2 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (cli *DockerCli) CurrentVersion() string {
// Client returns the APIClient
func (cli *DockerCli) Client() client.APIClient {
if err := cli.initialize(); err != nil {
_, _ = fmt.Fprintf(cli.Err(), "Failed to initialize: %s\n", err)
_, _ = fmt.Fprintln(cli.Err(), "Failed to initialize:", err)
os.Exit(1)
}
return cli.client
Expand Down Expand Up @@ -475,7 +475,7 @@ func (cli *DockerCli) DockerEndpoint() docker.Endpoint {
if err := cli.initialize(); err != nil {
// Note that we're not terminating here, as this function may be used
// in cases where we're able to continue.
_, _ = fmt.Fprintf(cli.Err(), "%v\n", cli.initErr)
_, _ = fmt.Fprintln(cli.Err(), cli.initErr)
}
return cli.dockerEndpoint
}
Expand Down
13 changes: 6 additions & 7 deletions cli/command/container/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,12 @@ func NewCopyCommand(dockerCli command.Cli) *cobra.Command {
Use: `cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH`,
Short: "Copy files/folders between a container and the local filesystem",
Long: strings.Join([]string{
"Copy files/folders between a container and the local filesystem\n",
"\nUse '-' as the source to read a tar archive from stdin\n",
"and extract it to a directory destination in a container.\n",
"Use '-' as the destination to stream a tar archive of a\n",
"container source to stdout.",
}, ""),
Long: `Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.`,
Args: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if args[0] == "" {
Expand Down
8 changes: 4 additions & 4 deletions cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
if errdefs.IsNotFound(err) && namedRef != nil && options.pull == PullImageMissing {
if !options.quiet {
// we don't want to write to stdout anything apart from container.ID
fmt.Fprintf(dockerCli.Err(), "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef))
_, _ = fmt.Fprintf(dockerCli.Err(), "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef))
}

if err := pullAndTagImage(); err != nil {
Expand All @@ -292,15 +292,15 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
}

for _, w := range response.Warnings {
_, _ = fmt.Fprintf(dockerCli.Err(), "WARNING: %s\n", w)
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING:", w)
}
err = containerIDFile.Write(response.ID)
return response.ID, err
}

func warnOnOomKillDisable(hostConfig container.HostConfig, stderr io.Writer) {
if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
fmt.Fprintln(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.")
_, _ = fmt.Fprintln(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.")
}
}

Expand All @@ -309,7 +309,7 @@ func warnOnOomKillDisable(hostConfig container.HostConfig, stderr io.Writer) {
func warnOnLocalhostDNS(hostConfig container.HostConfig, stderr io.Writer) {
for _, dnsIP := range hostConfig.DNS {
if isLocalhost(dnsIP) {
fmt.Fprintf(stderr, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
_, _ = fmt.Fprintf(stderr, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
for _, line := range strings.Split(statsTextBuffer.String(), "\n") {
// In case the new text is shorter than the one we are writing over,
// we'll append the "erase line" escape sequence to clear the remaining text.
_, _ = fmt.Fprint(&statsTextBuffer, line, "\033[K\n")
_, _ = fmt.Fprintln(&statsTextBuffer, line, "\033[K")
}

// We might have fewer containers than before, so let's clear the remaining text
Expand Down
10 changes: 5 additions & 5 deletions cli/command/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func longCreateDescription() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("Create a context\n\nDocker endpoint config:\n\n")
tw := tabwriter.NewWriter(buf, 20, 1, 3, ' ', 0)
fmt.Fprintln(tw, "NAME\tDESCRIPTION")
_, _ = fmt.Fprintln(tw, "NAME\tDESCRIPTION")
for _, d := range dockerConfigKeysDescriptions {
fmt.Fprintf(tw, "%s\t%s\n", d.name, d.description)
_, _ = fmt.Fprintf(tw, "%s\t%s\n", d.name, d.description)
}
tw.Flush()
_ = tw.Flush()
buf.WriteString("\nExample:\n\n$ docker context create my-context --description \"some description\" --docker \"host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file\"\n")
return buf.String()
}
Expand Down Expand Up @@ -79,8 +79,8 @@ func RunCreate(dockerCLI command.Cli, o *CreateOptions) error {
err = createNewContext(s, o)
}
if err == nil {
fmt.Fprintln(dockerCLI.Out(), o.Name)
fmt.Fprintf(dockerCLI.Err(), "Successfully created context %q\n", o.Name)
_, _ = fmt.Fprintln(dockerCLI.Out(), o.Name)
_, _ = fmt.Fprintf(dockerCLI.Err(), "Successfully created context %q\n", o.Name)
}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/context/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func RunImport(dockerCli command.Cli, name string, source string) error {
return err
}

fmt.Fprintln(dockerCli.Out(), name)
fmt.Fprintf(dockerCli.Err(), "Successfully imported context %q\n", name)
_, _ = fmt.Fprintln(dockerCli.Out(), name)
_, _ = fmt.Fprintf(dockerCli.Err(), "Successfully imported context %q\n", name)
return nil
}
10 changes: 5 additions & 5 deletions cli/command/context/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func longUpdateDescription() string {
buf := bytes.NewBuffer(nil)
buf.WriteString("Update a context\n\nDocker endpoint config:\n\n")
tw := tabwriter.NewWriter(buf, 20, 1, 3, ' ', 0)
fmt.Fprintln(tw, "NAME\tDESCRIPTION")
_, _ = fmt.Fprintln(tw, "NAME\tDESCRIPTION")
for _, d := range dockerConfigKeysDescriptions {
fmt.Fprintf(tw, "%s\t%s\n", d.name, d.description)
_, _ = fmt.Fprintf(tw, "%s\t%s\n", d.name, d.description)
}
tw.Flush()
_ = tw.Flush()
buf.WriteString("\nExample:\n\n$ docker context update my-context --description \"some description\" --docker \"host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file\"\n")
return buf.String()
}
Expand Down Expand Up @@ -93,8 +93,8 @@ func RunUpdate(dockerCLI command.Cli, o *UpdateOptions) error {
}
}

fmt.Fprintln(dockerCLI.Out(), o.Name)
fmt.Fprintf(dockerCLI.Err(), "Successfully updated context %q\n", o.Name)
_, _ = fmt.Fprintln(dockerCLI.Out(), o.Name)
_, _ = fmt.Fprintf(dockerCLI.Err(), "Successfully updated context %q\n", o.Name)
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions cli/command/context/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ func newUseCommand(dockerCli command.Cli) *cobra.Command {
}

// RunUse set the current Docker context
func RunUse(dockerCli command.Cli, name string) error {
func RunUse(dockerCLI command.Cli, name string) error {
// configValue uses an empty string for "default"
var configValue string
if name != command.DefaultContextName {
if err := store.ValidateContextName(name); err != nil {
return err
}
if _, err := dockerCli.ContextStore().GetMetadata(name); err != nil {
if _, err := dockerCLI.ContextStore().GetMetadata(name); err != nil {
return err
}
configValue = name
}
dockerConfig := dockerCli.ConfigFile()
dockerConfig := dockerCLI.ConfigFile()
// Avoid updating the config-file if nothing changed. This also prevents
// creating the file and config-directory if the default is used and
// no config-file existed yet.
Expand All @@ -46,10 +46,10 @@ func RunUse(dockerCli command.Cli, name string) error {
return err
}
}
fmt.Fprintln(dockerCli.Out(), name)
fmt.Fprintf(dockerCli.Err(), "Current context is now %q\n", name)
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
_, _ = fmt.Fprintf(dockerCLI.Err(), "Current context is now %q\n", name)
if name != command.DefaultContextName && os.Getenv(client.EnvOverrideHost) != "" {
fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Warning: %[1]s environment variable overrides the active context. "+
"To use %[2]q, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost, name)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/command/formatter/disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (ctx *DiskUsageContext) verboseWriteTable(duc *diskUsageContext) error {
if err != nil {
return err
}
ctx.Output.Write([]byte("\nLocal Volumes space usage:\n\n"))
_, _ = ctx.Output.Write([]byte("\nLocal Volumes space usage:\n\n"))
for _, v := range duc.Volumes {
if err := ctx.contextFormat(tmpl, v); err != nil {
return err
Expand All @@ -249,7 +249,7 @@ func (ctx *DiskUsageContext) verboseWriteTable(duc *diskUsageContext) error {
if err != nil {
return err
}
fmt.Fprintf(ctx.Output, "\nBuild cache usage: %s\n\n", units.HumanSize(float64(ctx.BuilderSize)))
_, _ = fmt.Fprintf(ctx.Output, "\nBuild cache usage: %s\n\n", units.HumanSize(float64(ctx.BuilderSize)))
for _, v := range duc.BuildCache {
if err := ctx.contextFormat(tmpl, v); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cli/command/image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func RunPull(ctx context.Context, dockerCLI command.Cli, opts PullOptions) error
case !opts.all && reference.IsNameOnly(distributionRef):
distributionRef = reference.TagNameOnly(distributionRef)
if tagged, ok := distributionRef.(reference.Tagged); ok && !opts.quiet {
fmt.Fprintf(dockerCLI.Out(), "Using default tag: %s\n", tagged.Tag())
_, _ = fmt.Fprintln(dockerCLI.Out(), "Using default tag:", tagged.Tag())
}
}

Expand All @@ -88,6 +88,6 @@ func RunPull(ctx context.Context, dockerCLI command.Cli, opts PullOptions) error
}
return err
}
fmt.Fprintln(dockerCLI.Out(), imgRefAndAuth.Reference().String())
_, _ = fmt.Fprintln(dockerCLI.Out(), imgRefAndAuth.Reference().String())
return nil
}
2 changes: 1 addition & 1 deletion cli/command/image/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ To push the complete multi-platform image, remove the --platform flag.
case !opts.all && reference.IsNameOnly(ref):
ref = reference.TagNameOnly(ref)
if tagged, ok := ref.(reference.Tagged); ok && !opts.quiet {
_, _ = fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", tagged.Tag())
_, _ = fmt.Fprintln(dockerCli.Out(), "Using default tag:", tagged.Tag())
}
}

Expand Down
12 changes: 6 additions & 6 deletions cli/command/image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
return &cmd
}

func runRemove(ctx context.Context, dockerCli command.Cli, opts removeOptions, images []string) error {
client := dockerCli.Client()
func runRemove(ctx context.Context, dockerCLI command.Cli, opts removeOptions, images []string) error {
apiClient := dockerCLI.Client()

options := image.RemoveOptions{
Force: opts.force,
Expand All @@ -62,7 +62,7 @@ func runRemove(ctx context.Context, dockerCli command.Cli, opts removeOptions, i
var errs []string
fatalErr := false
for _, img := range images {
dels, err := client.ImageRemove(ctx, img, options)
dels, err := apiClient.ImageRemove(ctx, img, options)
if err != nil {
if !errdefs.IsNotFound(err) {
fatalErr = true
Expand All @@ -71,9 +71,9 @@ func runRemove(ctx context.Context, dockerCli command.Cli, opts removeOptions, i
} else {
for _, del := range dels {
if del.Deleted != "" {
fmt.Fprintf(dockerCli.Out(), "Deleted: %s\n", del.Deleted)
_, _ = fmt.Fprintln(dockerCLI.Out(), "Deleted:", del.Deleted)
} else {
fmt.Fprintf(dockerCli.Out(), "Untagged: %s\n", del.Untagged)
_, _ = fmt.Fprintln(dockerCLI.Out(), "Untagged:", del.Untagged)
}
}
}
Expand All @@ -84,7 +84,7 @@ func runRemove(ctx context.Context, dockerCli command.Cli, opts removeOptions, i
if !opts.force || fatalErr {
return errors.New(msg)
}
fmt.Fprintln(dockerCli.Err(), msg)
_, _ = fmt.Fprintln(dockerCLI.Err(), msg)
}
return nil
}
Loading

0 comments on commit 4808d1b

Please sign in to comment.