Skip to content

Commit 28476fb

Browse files
refactor: Make output for cmd help, adhoc data more consistent
Usage funcs in pkg cmd would write some stuff to stdout, some to stderr. Make this consistent. Going with stdout for usage funcs to make it easier to capture help in a file. Specifying outputs in the library code helps make the Info subcommand cleaner. If you've selected the json format output, you're piping to jq and there's an error, then invalid json is written to stdout. So, write that adhoc stuff to stderr.
1 parent 8d6ada0 commit 28476fb

File tree

8 files changed

+40
-30
lines changed

8 files changed

+40
-30
lines changed

godfish.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func runMigration(driver Driver, pathToFile string, mig internal.Migration) (err
186186
if mig.Indirection().Value == internal.DirReverse {
187187
gerund = "rolling back"
188188
}
189-
fmt.Printf("%s version %q ... ", gerund, mig.Version().String())
189+
fmt.Fprintf(os.Stderr, "%s version %q ... ", gerund, mig.Version().String())
190190

191191
if err = driver.Execute(string(data)); err != nil {
192192
err = &runMigrationError{
@@ -204,7 +204,7 @@ func runMigration(driver Driver, pathToFile string, mig internal.Migration) (err
204204
mig.Version().String(),
205205
)
206206
if err == nil {
207-
fmt.Println("ok")
207+
fmt.Fprintln(os.Stderr, "ok")
208208
}
209209
return
210210
}
@@ -252,7 +252,7 @@ func choosePrinter(format string, w io.Writer) (out internal.InfoPrinter) {
252252
func Init(pathToFile string) (err error) {
253253
_, err = os.Stat(pathToFile)
254254
if err == nil {
255-
fmt.Printf("config file %q already present\n", pathToFile)
255+
fmt.Fprintf(os.Stderr, "config file %q already present\n", pathToFile)
256256
return nil
257257
}
258258
if !os.IsNotExist(err) {
@@ -363,7 +363,7 @@ func (m *migrationFinder) available() (out []internal.Migration, err error) {
363363
for _, fn := range filenames {
364364
mig, ierr := internal.ParseMigration(internal.Filename(fn))
365365
if errors.Is(ierr, internal.ErrDataInvalid) {
366-
fmt.Println(ierr)
366+
fmt.Fprintln(os.Stderr, ierr)
367367
continue
368368
} else if ierr != nil {
369369
err = ierr

internal/cmd/cmd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func New(driver godfish.Driver) Root {
5050
},
5151
}
5252

53-
rootFlags := flag.NewFlagSet("godfish", flag.ExitOnError)
53+
rootFlags := newFlagSet("godfish")
5454
rootFlags.Usage = func() {
5555
fmt.Fprintf(rootFlags.Output(), `Usage:
5656
@@ -124,9 +124,15 @@ Examples:
124124
}
125125
}
126126

127+
func newFlagSet(name string) (out *flag.FlagSet) {
128+
out = flag.NewFlagSet(name, flag.ExitOnError)
129+
out.SetOutput(os.Stdout)
130+
return
131+
}
132+
127133
// printFlagDefaults calls PrintDefaults on f. It helps make help message
128134
// formatting more consistent.
129135
func printFlagDefaults(f *flag.FlagSet) {
130-
fmt.Printf("\n%s flags:\n\n", f.Name())
136+
fmt.Fprintf(f.Output(), "\n%s flags:\n\n", f.Name())
131137
f.PrintDefaults()
132138
}

internal/cmd/create-migration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func makeCreateMigration(subcmdName string) alf.Directive {
1818

1919
// Other subcommands scope the flagset within the Setup func. However, this
2020
// one is scoped up here to check if some flags were specified at runtime.
21-
flags := flag.NewFlagSet(subcmdName, flag.ExitOnError)
21+
flags := newFlagSet(subcmdName)
2222

2323
return &alf.Command{
2424
Description: "generate migration files",
@@ -48,7 +48,7 @@ func makeCreateMigration(subcmdName string) alf.Directive {
4848
"customize the directional part of the filename for reverse migration",
4949
)
5050
flags.Usage = func() {
51-
fmt.Printf(`Usage: %s [godfish-flags] %s [%s-flags]
51+
fmt.Fprintf(flags.Output(), `Usage: %s [godfish-flags] %s [%s-flags]
5252
5353
Generate migration files: one meant for the "forward" direction,
5454
another meant for "reverse". Optionally create a migration in the forward

internal/cmd/info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func makeInfo(name string) alf.Directive {
1818
return &alf.Command{
1919
Description: "output applied migrations, migrations to apply",
2020
Setup: func(p flag.FlagSet) *flag.FlagSet {
21-
flags := flag.NewFlagSet(name, flag.ExitOnError)
21+
flags := newFlagSet(name)
2222
flags.StringVar(
2323
&direction,
2424
"direction",
@@ -38,7 +38,7 @@ func makeInfo(name string) alf.Directive {
3838
fmt.Sprintf("timestamp of migration, format: %s", internal.TimeFormat),
3939
)
4040
flags.Usage = func() {
41-
fmt.Printf(`Usage: %s [godfish-flags] %s [%s-flags]
41+
fmt.Fprintf(flags.Output(), `Usage: %s [godfish-flags] %s [%s-flags]
4242
4343
List applied migrations, preview migrations to apply.
4444

internal/cmd/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ func makeInit(name string) alf.Directive {
1515
return &alf.Command{
1616
Description: "create godfish configuration file",
1717
Setup: func(p flag.FlagSet) *flag.FlagSet {
18-
flags := flag.NewFlagSet(name, flag.ExitOnError)
18+
flags := newFlagSet(name)
1919
flags.StringVar(
2020
&conf,
2121
"conf",
2222
".godfish.json",
2323
"path to godfish config file",
2424
)
2525
flags.Usage = func() {
26-
fmt.Printf(`Usage: %s [godfish-flags] %s [%s-flags]
26+
fmt.Fprintf(flags.Output(), `Usage: %s [godfish-flags] %s [%s-flags]
2727
2828
Creates a configuration file, unless it already exists.
2929
`,

internal/cmd/migrate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ func makeMigrate(name string) alf.Directive {
1616
return &alf.Command{
1717
Description: "execute migration(s) in the forward direction",
1818
Setup: func(p flag.FlagSet) *flag.FlagSet {
19-
flags := flag.NewFlagSet(name, flag.ExitOnError)
19+
flags := newFlagSet(name)
2020
flags.StringVar(
2121
&version,
2222
"version",
2323
"",
2424
fmt.Sprintf("timestamp of migration, format: %s", internal.TimeFormat),
2525
)
2626
flags.Usage = func() {
27-
fmt.Printf(`Usage: %s [godfish-flags] %s [%s-flags]
27+
fmt.Fprintf(flags.Output(), `Usage: %s [godfish-flags] %s [%s-flags]
2828
2929
Execute migration(s) in the forward direction. If the "version" is left
3030
unspecified, then all available migrations are executed. Otherwise,
@@ -57,9 +57,9 @@ func makeRemigrate(name string) alf.Directive {
5757
return &alf.Command{
5858
Description: "rollback and then re-apply the last migration",
5959
Setup: func(p flag.FlagSet) *flag.FlagSet {
60-
flags := flag.NewFlagSet(name, flag.ExitOnError)
60+
flags := newFlagSet(name)
6161
flags.Usage = func() {
62-
fmt.Printf(`Usage: %s [godfish-flags] %s [%s-flags]
62+
fmt.Fprintf(flags.Output(), `Usage: %s [godfish-flags] %s [%s-flags]
6363
6464
Execute the last migration in reverse (rollback) and then execute the same
6565
one forward. This could be useful for development.
@@ -89,15 +89,15 @@ func makeRollback(name string) alf.Directive {
8989
return &alf.Command{
9090
Description: "execute migration(s) in the reverse direction",
9191
Setup: func(p flag.FlagSet) *flag.FlagSet {
92-
flags := flag.NewFlagSet(name, flag.ExitOnError)
92+
flags := newFlagSet(name)
9393
flags.StringVar(
9494
&version,
9595
"version",
9696
"",
9797
fmt.Sprintf("timestamp of migration, format: %s", internal.TimeFormat),
9898
)
9999
flags.Usage = func() {
100-
fmt.Printf(`Usage: %s [godfish-flags] %s [%s-flags]
100+
fmt.Fprintf(flags.Output(), `Usage: %s [godfish-flags] %s [%s-flags]
101101
102102
Execute migration(s) in the reverse direction. If the "version" is left
103103
unspecified, then only the first available migration is executed. Otherwise,

internal/cmd/version.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"flag"
77
"fmt"
8+
"os"
89

910
"github.com/rafaelespinoza/alf"
1011
)
@@ -26,10 +27,10 @@ func makeVersion(name string) alf.Directive {
2627
return &alf.Command{
2728
Description: "show metadata about the build",
2829
Setup: func(p flag.FlagSet) *flag.FlagSet {
29-
flags := flag.NewFlagSet(name, flag.ExitOnError)
30+
flags := newFlagSet(name)
3031
flags.BoolVar(&formatJSON, "json", false, "format output as JSON")
3132
flags.Usage = func() {
32-
fmt.Printf(`Usage: %s [flags]
33+
fmt.Fprintf(flags.Output(), `Usage: %s [flags]
3334
3435
Prints some versioning info to stdout. Pass the -json flag to get JSON.
3536
`,
@@ -40,13 +41,16 @@ func makeVersion(name string) alf.Directive {
4041
return flags
4142
},
4243
Run: func(_ context.Context) error {
44+
// Calling fmt.Print* also writes to stdout, but want to be explicit
45+
// about where this subcommand output goes.
46+
w := os.Stdout
4347
if !formatJSON {
44-
fmt.Printf("BranchName: %s\n", versionBranchName)
45-
fmt.Printf("BuildTime: %s\n", versionBuildTime)
46-
fmt.Printf("Driver: %s\n", versionDriver)
47-
fmt.Printf("CommitHash: %s\n", versionCommitHash)
48-
fmt.Printf("GoVersion: %s\n", versionGoVersion)
49-
fmt.Printf("Tag: %s\n", versionTag)
48+
fmt.Fprintf(w, "BranchName: %s\n", versionBranchName)
49+
fmt.Fprintf(w, "BuildTime: %s\n", versionBuildTime)
50+
fmt.Fprintf(w, "Driver: %s\n", versionDriver)
51+
fmt.Fprintf(w, "CommitHash: %s\n", versionCommitHash)
52+
fmt.Fprintf(w, "GoVersion: %s\n", versionGoVersion)
53+
fmt.Fprintf(w, "Tag: %s\n", versionTag)
5054
return nil
5155
}
5256
out, err := json.Marshal(
@@ -62,7 +66,7 @@ func makeVersion(name string) alf.Directive {
6266
if err != nil {
6367
return err
6468
}
65-
fmt.Printf("%s\n", out)
69+
fmt.Fprintf(w, "%s\n", out)
6670
return nil
6771
},
6872
}

internal/migration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,18 @@ func (m *MigrationParams) GenerateFiles() (err error) {
118118
if forwardFile, err = newMigrationFile(m.Forward, m.Dirpath); err != nil {
119119
return
120120
}
121-
fmt.Println("created forward file:", forwardFile.Name())
121+
fmt.Fprintln(os.Stderr, "created forward file:", forwardFile.Name())
122122
defer forwardFile.Close()
123123

124124
if !m.Reversible {
125-
fmt.Println("migration marked irreversible, did not create reverse file")
125+
fmt.Fprintln(os.Stderr, "migration marked irreversible, did not create reverse file")
126126
return
127127
}
128128

129129
if reverseFile, err = newMigrationFile(m.Reverse, m.Dirpath); err != nil {
130130
return
131131
}
132-
fmt.Println("created reverse file:", reverseFile.Name())
132+
fmt.Fprintln(os.Stderr, "created reverse file:", reverseFile.Name())
133133
defer reverseFile.Close()
134134
return
135135
}

0 commit comments

Comments
 (0)