Skip to content

Commit f237d7c

Browse files
authored
feat: pass logger into repo and client (#385)
* feat: pass logger into repo and client Signed-off-by: Asra Ali <[email protected]>
1 parent 040092c commit f237d7c

File tree

13 files changed

+111
-30
lines changed

13 files changed

+111
-30
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ linters:
1313
- gosimple
1414
- unused
1515
- typecheck
16+
- forbidigo

cmd/tuf-client/main.go

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

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

3334
if cmd == "help" {
3435
if len(cmdArgs) == 0 { // `tuf-client help`
35-
fmt.Println(usage)
36+
fmt.Fprint(os.Stderr, usage)
3637
return
3738
} else { // `tuf-client help <command>`
3839
cmd = cmdArgs[0]

cmd/tuf/gen_key.go

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

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

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

4546
var err error
@@ -57,7 +58,7 @@ func cmdGenKey(args *docopt.Args, repo *tuf.Repo) error {
5758
return err
5859
}
5960
for _, id := range keyids {
60-
fmt.Println("Generated", role, keyScheme, "key with ID", id)
61+
fmt.Fprintf(os.Stdout, "Generated %s %s key with ID %s", role, keyScheme, id)
6162
}
6263
return nil
6364
}

cmd/tuf/get_threshold.go

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

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

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

26-
fmt.Println("The threshold for", role, "role is", threshold)
27+
fmt.Fprintf(os.Stdout, "The threshold for %s role is %d", role, threshold)
2728
return nil
2829
}

cmd/tuf/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ See "tuf help <command>" for more information on a specific command
5858

5959
if cmd == "help" {
6060
if len(cmdArgs) == 0 { // `tuf help`
61-
fmt.Println(usage)
61+
fmt.Fprint(os.Stderr, usage)
6262
return
6363
} else { // `tuf help <command>`
6464
cmd = cmdArgs[0]
@@ -115,7 +115,11 @@ func runCommand(name string, args []string, dir string, insecure bool) error {
115115
if !insecure {
116116
p = getPassphrase
117117
}
118-
repo, err := tuf.NewRepo(tuf.FileSystemStore(dir, p))
118+
logger := log.New(os.Stdout, "", 0)
119+
storeOpts := tuf.StoreOpts{Logger: logger, PassFunc: p}
120+
121+
repo, err := tuf.NewRepoWithOpts(tuf.FileSystemStoreWithOpts(dir, storeOpts),
122+
tuf.WithLogger(logger))
119123
if err != nil {
120124
return err
121125
}

cmd/tuf/payload.go

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

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

67
"github.com/flynn/go-docopt"
78
"github.com/theupdateframework/go-tuf"
@@ -20,6 +21,6 @@ func cmdPayload(args *docopt.Args, repo *tuf.Repo) error {
2021
if err != nil {
2122
return err
2223
}
23-
fmt.Print(string(p))
24+
fmt.Fprint(os.Stdout, string(p))
2425
return nil
2526
}

cmd/tuf/set_threshold.go

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

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

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

31-
fmt.Println("The threshold for", role, "role is now", threshold)
32+
fmt.Fprintf(os.Stdout, "The threshold for %s role is now %d", role, threshold)
3233
return nil
3334
}

cmd/tuf/sign_payload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func cmdSignPayload(args *docopt.Args, repo *tuf.Repo) error {
3636
if err != nil {
3737
return err
3838
}
39-
fmt.Print(string(bytes))
39+
fmt.Fprint(os.Stdout, string(bytes))
4040

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

internal/fsutil/perm_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package fsutil
55

66
import (
7-
"fmt"
87
"os"
98
"path/filepath"
109
"testing"
@@ -59,7 +58,6 @@ func TestEnsureMaxPermissions(t *testing.T) {
5958
assert.NoError(t, err)
6059
err = EnsureMaxPermissions(fi, os.FileMode(0222))
6160
assert.Error(t, err)
62-
fmt.Println(err)
6361

6462
// Check matching due to more restrictive perms on file
6563
err = os.Chmod(p, 0444)

local_store.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"io/fs"
10+
"log"
1011
"os"
1112
"path/filepath"
1213
"strings"
@@ -197,18 +198,44 @@ type persistedKeys struct {
197198
Data json.RawMessage `json:"data"`
198199
}
199200

201+
type StoreOpts struct {
202+
Logger *log.Logger
203+
PassFunc util.PassphraseFunc
204+
}
205+
200206
func FileSystemStore(dir string, p util.PassphraseFunc) LocalStore {
201207
return &fileSystemStore{
202208
dir: dir,
203209
passphraseFunc: p,
210+
logger: log.New(io.Discard, "", 0),
204211
signerForKeyID: make(map[string]keys.Signer),
205212
keyIDsForRole: make(map[string][]string),
206213
}
207214
}
208215

216+
func FileSystemStoreWithOpts(dir string, opts ...StoreOpts) LocalStore {
217+
store := &fileSystemStore{
218+
dir: dir,
219+
passphraseFunc: nil,
220+
logger: log.New(io.Discard, "", 0),
221+
signerForKeyID: make(map[string]keys.Signer),
222+
keyIDsForRole: make(map[string][]string),
223+
}
224+
for _, opt := range opts {
225+
if opt.Logger != nil {
226+
store.logger = opt.Logger
227+
}
228+
if opt.PassFunc != nil {
229+
store.passphraseFunc = opt.PassFunc
230+
}
231+
}
232+
return store
233+
}
234+
209235
type fileSystemStore struct {
210236
dir string
211237
passphraseFunc util.PassphraseFunc
238+
logger *log.Logger
212239

213240
signerForKeyID map[string]keys.Signer
214241
keyIDsForRole map[string][]string
@@ -526,7 +553,7 @@ func (f *fileSystemStore) ChangePassphrase(role string) error {
526553
keys, _, err := f.loadPrivateKeys(role)
527554
if err != nil {
528555
if os.IsNotExist(err) {
529-
fmt.Printf("Failed to change passphrase. Missing keys file for %s role. \n", role)
556+
f.logger.Printf("Failed to change passphrase. Missing keys file for %s role. \n", role)
530557
}
531558
return err
532559
}
@@ -548,7 +575,7 @@ func (f *fileSystemStore) ChangePassphrase(role string) error {
548575
if err := util.AtomicallyWriteFile(f.keysPath(role), append(data, '\n'), 0600); err != nil {
549576
return err
550577
}
551-
fmt.Printf("Successfully changed passphrase for %s keys file\n", role)
578+
f.logger.Printf("Successfully changed passphrase for %s keys file\n", role)
552579
return nil
553580
}
554581

0 commit comments

Comments
 (0)