Skip to content

Commit

Permalink
Configure output
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Oct 27, 2021
1 parent 945cdca commit 900399e
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions acmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"sort"
Expand Down Expand Up @@ -43,6 +44,10 @@ type Config struct {
// Version of the application.
Version string

// Output is a destionation where result will be printed.
// Exported for testing purpose only, if nil os.Stderr is used.
Output io.Writer

// Context for commands, if nil context based on os.Interrupt will be used.
Context context.Context

Expand Down Expand Up @@ -78,8 +83,12 @@ func (r *Runner) init() error {
r.ctx, _ = signal.NotifyContext(context.Background(), os.Interrupt)
}

if r.cfg.Output == nil {
r.cfg.Output = os.Stderr
}

if r.cfg.Usage == nil {
r.cfg.Usage = defaultUsage
r.cfg.Usage = defaultUsage(r.cfg.Output)
}

names := make(map[string]struct{})
Expand Down Expand Up @@ -121,7 +130,7 @@ func (r *Runner) run() error {
r.cfg.Usage(r.cfg, r.cmds)
return nil
case cmd == "version":
fmt.Printf("%s version: %s\n\n", r.cfg.AppName, r.cfg.Version)
fmt.Fprintf(r.cfg.Output, "%s version: %s\n\n", r.cfg.AppName, r.cfg.Version)
return nil
}

Expand All @@ -133,23 +142,25 @@ func (r *Runner) run() error {
return fmt.Errorf("no such command %q", cmd)
}

var defaultUsage = func(cfg Config, cmds []Command) {
if cfg.AppDescription != "" {
fmt.Fprintf(os.Stderr, "%s\n\n", cfg.AppDescription)
}
var defaultUsage = func(w io.Writer) func(cfg Config, cmds []Command) {
return func(cfg Config, cmds []Command) {
if cfg.AppDescription != "" {
fmt.Fprintf(w, "%s\n\n", cfg.AppDescription)
}

fmt.Fprintf(os.Stderr, "Usage:\n\n %s <command> [arguments]\n\nThe commands are:\n\n", cfg.AppName)
printCommands(cmds)
fmt.Fprintf(w, "Usage:\n\n %s <command> [arguments]\n\nThe commands are:\n\n", cfg.AppName)
printCommands(w, cmds)

if cfg.Version != "" {
fmt.Fprintf(os.Stderr, "Version: %s\n\n", cfg.Version)
if cfg.Version != "" {
fmt.Fprintf(w, "Version: %s\n\n", cfg.Version)
}
}
}

// printCommands in a table form (Name and Description)
func printCommands(cmds []Command) {
func printCommands(w io.Writer, cmds []Command) {
minwidth, tabwidth, padding, padchar, flags := 0, 0, 11, byte(' '), uint(0)
tw := tabwriter.NewWriter(os.Stderr, minwidth, tabwidth, padding, padchar, flags)
tw := tabwriter.NewWriter(w, minwidth, tabwidth, padding, padchar, flags)
for _, cmd := range cmds {
fmt.Fprintf(tw, " %s\t%s\n", cmd.Name, cmd.Description)
}
Expand Down

0 comments on commit 900399e

Please sign in to comment.