Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass logger into repo and client #385

Merged
merged 5 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ linters:
- gosimple
- unused
- typecheck
- forbidigo
trishankatdatadog marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion cmd/tuf-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"os"

docopt "github.com/flynn/go-docopt"
tuf "github.com/theupdateframework/go-tuf/client"
Expand Down Expand Up @@ -32,7 +33,7 @@ See "tuf-client help <command>" for more information on a specific command.

if cmd == "help" {
if len(cmdArgs) == 0 { // `tuf-client help`
fmt.Println(usage)
fmt.Fprint(os.Stderr, usage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is usage going to stderr?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷 I've seen it both ways. There are some tools that define an explicit, strict "everything that gets logged to stdout must be computer-readable" philosophy and apply that to help text too. Many tools do, of course, print usage on STDOUT.

See for instance this request for usage on STDERR. I particularly like the recommendation to follow picocli (just one example):

when the user specified invalid input, the error message and usage help should be printed to stderr, but when the user requests help with --help, the usage help should be printed to stdout so users can pipe the output to tools like grep and less.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you for the comment: Yeah, I think I originally assumed that help should be part of STDERR, but here, since the user explicitly requested for help, I think I concur, it should be STDOUT. I'll put out a fix PR.

return
} else { // `tuf-client help <command>`
cmd = cmdArgs[0]
Expand Down
5 changes: 3 additions & 2 deletions cmd/tuf/gen_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"time"

"github.com/flynn/go-docopt"
Expand Down Expand Up @@ -39,7 +40,7 @@ func cmdGenKey(args *docopt.Args, repo *tuf.Repo) error {
string(data.KeySchemeRSASSA_PSS_SHA256):
keyScheme = data.KeyScheme(t)
default:
fmt.Println("Using default key scheme", keyScheme)
fmt.Fprint(os.Stderr, "Using default key scheme", keyScheme)
}

var err error
Expand All @@ -57,7 +58,7 @@ func cmdGenKey(args *docopt.Args, repo *tuf.Repo) error {
return err
}
for _, id := range keyids {
fmt.Println("Generated", role, keyScheme, "key with ID", id)
fmt.Fprintf(os.Stdout, "Generated %s %s key with ID %s", role, keyScheme, id)
}
return nil
}
3 changes: 2 additions & 1 deletion cmd/tuf/get_threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/flynn/go-docopt"
"github.com/theupdateframework/go-tuf"
Expand All @@ -23,6 +24,6 @@ func cmdGetThreshold(args *docopt.Args, repo *tuf.Repo) error {
return err
}

fmt.Println("The threshold for", role, "role is", threshold)
fmt.Fprintf(os.Stdout, "The threshold for %s role is %d", role, threshold)
return nil
}
8 changes: 6 additions & 2 deletions cmd/tuf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ See "tuf help <command>" for more information on a specific command

if cmd == "help" {
if len(cmdArgs) == 0 { // `tuf help`
fmt.Println(usage)
fmt.Fprint(os.Stderr, usage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

return
} else { // `tuf help <command>`
cmd = cmdArgs[0]
Expand Down Expand Up @@ -115,7 +115,11 @@ func runCommand(name string, args []string, dir string, insecure bool) error {
if !insecure {
p = getPassphrase
}
repo, err := tuf.NewRepo(tuf.FileSystemStore(dir, p))
logger := log.New(os.Stdout, "", 0)
storeOpts := tuf.StoreOpts{Logger: logger, PassFunc: p}

repo, err := tuf.NewRepoWithOpts(tuf.FileSystemStoreWithOpts(dir, storeOpts),
tuf.WithLogger(logger))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/tuf/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/flynn/go-docopt"
"github.com/theupdateframework/go-tuf"
Expand All @@ -20,6 +21,6 @@ func cmdPayload(args *docopt.Args, repo *tuf.Repo) error {
if err != nil {
return err
}
fmt.Print(string(p))
fmt.Fprint(os.Stdout, string(p))
return nil
}
3 changes: 2 additions & 1 deletion cmd/tuf/set_threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"strconv"

"github.com/flynn/go-docopt"
Expand All @@ -28,6 +29,6 @@ func cmdSetThreshold(args *docopt.Args, repo *tuf.Repo) error {
return err
}

fmt.Println("The threshold for", role, "role is now", threshold)
fmt.Fprintf(os.Stdout, "The threshold for %s role is now %d", role, threshold)
return nil
}
2 changes: 1 addition & 1 deletion cmd/tuf/sign_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func cmdSignPayload(args *docopt.Args, repo *tuf.Repo) error {
if err != nil {
return err
}
fmt.Print(string(bytes))
fmt.Fprint(os.Stdout, string(bytes))

fmt.Fprintln(os.Stderr, "tuf: signed with", numKeys, "key(s)")
return nil
Expand Down