Skip to content

Commit 862e507

Browse files
committed
fix: linting issues
Signed-off-by: SequeI <[email protected]>
1 parent 17fceee commit 862e507

File tree

9 files changed

+117
-25
lines changed

9 files changed

+117
-25
lines changed

internal/cache/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package api
15+
package cacheapi
1616

1717
import "github.com/sigstore/gitsign/internal/config"
1818

internal/cache/cache_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/github/smimesign/fakeca"
3030
"github.com/google/go-cmp/cmp"
3131
"github.com/sigstore/gitsign/internal/cache"
32-
"github.com/sigstore/gitsign/internal/cache/api"
32+
cacheapi "github.com/sigstore/gitsign/internal/cache/api"
3333
"github.com/sigstore/gitsign/internal/cache/service"
3434
"github.com/sigstore/sigstore/pkg/cryptoutils"
3535
)
@@ -72,13 +72,13 @@ func TestCache(t *testing.T) {
7272
host, _ := os.Hostname()
7373
wd, _ := os.Getwd()
7474
id := fmt.Sprintf("%s@%s", host, wd)
75-
cred := new(api.Credential)
76-
if err := client.Client.Call("Service.GetCredential", &api.GetCredentialRequest{ID: id}, cred); err != nil {
75+
cred := new(cacheapi.Credential)
76+
if err := client.Client.Call("Service.GetCredential", &cacheapi.GetCredentialRequest{ID: id}, cred); err != nil {
7777
t.Fatal(err)
7878
}
7979

8080
privPEM, _ := cryptoutils.MarshalPrivateKeyToPEM(priv)
81-
want := &api.Credential{
81+
want := &cacheapi.Credential{
8282
PrivateKey: privPEM,
8383
Cert: certPEM,
8484
}

internal/cache/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"os"
2525
"time"
2626

27-
"github.com/sigstore/gitsign/internal/cache/api"
27+
cacheapi "github.com/sigstore/gitsign/internal/cache/api"
2828
"github.com/sigstore/gitsign/internal/config"
2929
"github.com/sigstore/sigstore/pkg/cryptoutils"
3030
)
@@ -40,8 +40,8 @@ func (c *Client) GetCredentials(_ context.Context, cfg *config.Config) (crypto.P
4040
if err != nil {
4141
return nil, nil, nil, fmt.Errorf("error getting credential ID: %w", err)
4242
}
43-
resp := new(api.Credential)
44-
if err := c.Client.Call("Service.GetCredential", api.GetCredentialRequest{
43+
resp := new(cacheapi.Credential)
44+
if err := c.Client.Call("Service.GetCredential", cacheapi.GetCredentialRequest{
4545
ID: id,
4646
Config: cfg,
4747
}, resp); err != nil {
@@ -96,14 +96,14 @@ func (c *Client) StoreCert(_ context.Context, priv crypto.PrivateKey, cert, chai
9696
return err
9797
}
9898

99-
if err := c.Client.Call("Service.StoreCredential", api.StoreCredentialRequest{
99+
if err := c.Client.Call("Service.StoreCredential", cacheapi.StoreCredentialRequest{
100100
ID: id,
101-
Credential: &api.Credential{
101+
Credential: &cacheapi.Credential{
102102
PrivateKey: privPEM,
103103
Cert: cert,
104104
Chain: chain,
105105
},
106-
}, new(api.Credential)); err != nil {
106+
}, new(cacheapi.Credential)); err != nil {
107107
return err
108108
}
109109

internal/cache/service/service.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/patrickmn/go-cache"
24-
"github.com/sigstore/gitsign/internal/cache/api"
24+
cacheapi "github.com/sigstore/gitsign/internal/cache/api"
2525
"github.com/sigstore/gitsign/internal/fulcio"
2626
"github.com/sigstore/sigstore/pkg/cryptoutils"
2727
)
@@ -42,7 +42,7 @@ func NewService() *Service {
4242
return s
4343
}
4444

45-
func (s *Service) StoreCredential(req api.StoreCredentialRequest, resp *api.Credential) error {
45+
func (s *Service) StoreCredential(req cacheapi.StoreCredentialRequest, resp *cacheapi.Credential) error {
4646
fmt.Println("Store", req.ID)
4747
if err := s.store.Add(req.ID, req.Credential, 10*time.Minute); err != nil {
4848
return err
@@ -51,13 +51,13 @@ func (s *Service) StoreCredential(req api.StoreCredentialRequest, resp *api.Cred
5151
return nil
5252
}
5353

54-
func (s *Service) GetCredential(req api.GetCredentialRequest, resp *api.Credential) error {
54+
func (s *Service) GetCredential(req cacheapi.GetCredentialRequest, resp *cacheapi.Credential) error {
5555
ctx := context.Background()
5656
fmt.Println("Get", req.ID)
5757
i, ok := s.store.Get(req.ID)
5858
if ok {
5959
fmt.Println("gitsign-credential-cache: found credential!")
60-
cred, ok := i.(*api.Credential)
60+
cred, ok := i.(*cacheapi.Credential)
6161
if !ok {
6262
return fmt.Errorf("unknown credential type %T", i)
6363
}
@@ -81,7 +81,7 @@ func (s *Service) GetCredential(req api.GetCredentialRequest, resp *api.Credenti
8181
if err != nil {
8282
return err
8383
}
84-
cred := &api.Credential{
84+
cred := &cacheapi.Credential{
8585
PrivateKey: privPEM,
8686
Cert: id.CertPEM,
8787
Chain: id.ChainPEM,

internal/commands/root/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
verifytag "github.com/sigstore/gitsign/internal/commands/verify-tag"
2626
"github.com/sigstore/gitsign/internal/commands/version"
2727
"github.com/sigstore/gitsign/internal/config"
28-
"github.com/sigstore/gitsign/internal/io"
28+
"github.com/sigstore/gitsign/internal/streams"
2929
)
3030

3131
type options struct {
@@ -66,7 +66,7 @@ func New(cfg *config.Config) *cobra.Command {
6666
Args: cobra.ArbitraryArgs,
6767
DisableAutoGenTag: true,
6868
RunE: func(cmd *cobra.Command, args []string) error {
69-
s := io.New(o.Config.LogPath)
69+
s := streams.New(o.Config.LogPath)
7070
defer s.Close()
7171
return s.Wrap(func() error {
7272
switch {

internal/commands/root/sign.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import (
2626
"github.com/sigstore/gitsign/internal/fulcio"
2727
"github.com/sigstore/gitsign/internal/git"
2828
"github.com/sigstore/gitsign/internal/gpg"
29-
gsio "github.com/sigstore/gitsign/internal/io"
3029
"github.com/sigstore/gitsign/internal/rekor"
3130
"github.com/sigstore/gitsign/internal/signature"
31+
"github.com/sigstore/gitsign/internal/streams"
3232
)
3333

3434
// commandSign implements gitsign commit signing.
3535
// This is implemented as a root command so that user can specify the
3636
// gitsign binary directly in their gitconfigs.
37-
func commandSign(o *options, s *gsio.Streams, args ...string) error {
37+
func commandSign(o *options, s *streams.Streams, args ...string) error {
3838
ctx := context.Background()
3939

4040
// Flag validation

internal/commands/root/verify.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import (
2626
"github.com/sigstore/gitsign/internal/commands/verify"
2727
"github.com/sigstore/gitsign/internal/gitsign"
2828
"github.com/sigstore/gitsign/internal/gpg"
29-
gsio "github.com/sigstore/gitsign/internal/io"
29+
"github.com/sigstore/gitsign/internal/streams"
3030
)
3131

3232
// commandSign implements gitsign commit verification.
3333
// This is implemented as a root command so that user can specify the
3434
// gitsign binary directly in their gitconfigs.
35-
func commandVerify(o *options, s *gsio.Streams, args ...string) error {
35+
func commandVerify(o *options, s *streams.Streams, args ...string) error {
3636
ctx := context.Background()
3737

3838
// Flag validation
@@ -91,7 +91,7 @@ func commandVerify(o *options, s *gsio.Streams, args ...string) error {
9191
return nil
9292
}
9393

94-
func readAttached(s *gsio.Streams, args ...string) ([]byte, error) {
94+
func readAttached(s *streams.Streams, args ...string) ([]byte, error) {
9595
var (
9696
f io.Reader
9797
err error
@@ -117,7 +117,7 @@ func readAttached(s *gsio.Streams, args ...string) ([]byte, error) {
117117
return sig.Bytes(), nil
118118
}
119119

120-
func readDetached(s *gsio.Streams, args ...string) ([]byte, []byte, error) {
120+
func readDetached(s *streams.Streams, args ...string) ([]byte, []byte, error) {
121121
// Read in signature
122122
sigFile, err := os.Open(args[0])
123123
if err != nil {

internal/io/streams.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
package io
16+
package streams
1717

1818
import (
1919
"fmt"

internal/streams/streams.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// Copyright 2022 The Sigstore Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package streams
17+
18+
import (
19+
"fmt"
20+
"io"
21+
"os"
22+
23+
"github.com/mattn/go-tty"
24+
)
25+
26+
type Streams struct {
27+
In io.Reader
28+
Out io.Writer
29+
Err io.Writer
30+
31+
TTYIn io.Reader
32+
TTYOut io.Writer
33+
34+
close []func() error
35+
}
36+
37+
func New(logPath string) *Streams {
38+
s := &Streams{
39+
In: os.Stdin,
40+
Out: os.Stdout,
41+
Err: os.Stderr,
42+
}
43+
44+
if logPath != "" {
45+
// Since Git eats both stdout and stderr, we don't have a good way of
46+
// getting error information back from clients if things go wrong.
47+
// As a janky way to preserve error message, tee stderr to
48+
// a temp file.
49+
if f, err := os.Create(logPath); err == nil {
50+
s.close = append(s.close, f.Close)
51+
s.Err = io.MultiWriter(s.Err, f)
52+
}
53+
}
54+
55+
// A TTY may not be available in all environments (e.g. in CI), so only
56+
// set the input/output if we can actually open it.
57+
tty, err := tty.Open()
58+
if err == nil {
59+
s.close = append(s.close, tty.Close)
60+
s.TTYIn = tty.Input()
61+
s.TTYOut = tty.Output()
62+
} else {
63+
// If we can't connect to a TTY, fall back to stderr for output (which
64+
// will also log to file if GITSIGN_LOG is set).
65+
s.TTYOut = s.Err
66+
}
67+
return s
68+
}
69+
70+
func (s *Streams) Wrap(fn func() error) error {
71+
// Log any panics to ttyout, since otherwise they will be lost to os.Stderr.
72+
defer func() {
73+
if r := recover(); r != nil {
74+
fmt.Fprintln(s.TTYOut, r)
75+
}
76+
}()
77+
78+
if err := fn(); err != nil {
79+
fmt.Fprintln(s.TTYOut, err)
80+
return err
81+
}
82+
return nil
83+
}
84+
85+
func (s *Streams) Close() error {
86+
for _, fn := range s.close {
87+
if err := fn(); err != nil {
88+
return err
89+
}
90+
}
91+
return nil
92+
}

0 commit comments

Comments
 (0)