Skip to content

Commit 494c2b3

Browse files
Igor DrozdovVasilii Iakliushin
authored andcommitted
Merge branch 'duo-edit-20260107-094508' into 'main'
Fix linter warnings in command package See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1358 Merged-by: Igor Drozdov <idrozdov@gitlab.com> Approved-by: Igor Drozdov <idrozdov@gitlab.com> Co-authored-by: Vasilii Iakliushin <viakliushin@gitlab.com>
2 parents e5fe24f + 72565ad commit 494c2b3

8 files changed

Lines changed: 43 additions & 42 deletions

File tree

internal/command/command.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Package command provides the core command execution infrastructure for gitlab-shell.
2+
// It defines the Command interface that all shell commands must implement,
3+
// along with shared utilities for logging, tracing, and context management.
14
package command
25

36
import (
@@ -12,17 +15,21 @@ import (
1215
"gitlab.com/gitlab-org/labkit/tracing"
1316
)
1417

18+
// Command is the interface that all gitlab-shell commands must implement.
19+
// Execute runs the command and returns the updated context and any error.
1520
type Command interface {
1621
Execute(ctx context.Context) (context.Context, error)
1722
}
1823

24+
// LogMetadata contains project and namespace information for structured logging.
1925
type LogMetadata struct {
2026
Project string `json:"project,omitempty"`
2127
RootNamespace string `json:"root_namespace,omitempty"`
2228
ProjectID int `json:"project_id,omitempty"`
2329
RootNamespaceID int `json:"root_namespace_id,omitempty"`
2430
}
2531

32+
// LogData contains user and request information for structured logging.
2633
type LogData struct {
2734
Username string `json:"username"`
2835
WrittenBytes int64 `json:"written_bytes"`
@@ -34,6 +41,8 @@ type contextKey string
3441
// LogDataKey is the context key used to store log data in request contexts.
3542
const LogDataKey contextKey = "logData"
3643

44+
// CheckForVersionFlag checks if the -version flag was passed and prints version info if so.
45+
// It exits the program after printing the version.
3746
func CheckForVersionFlag(osArgs []string, version, buildTime string) {
3847
// We can't use the flag library because gitlab-shell receives other arguments
3948
// that confuse the parser.
@@ -45,7 +54,7 @@ func CheckForVersionFlag(osArgs []string, version, buildTime string) {
4554
}
4655
}
4756

48-
// Setup() initializes tracing from the configuration file and generates a
57+
// Setup initializes tracing from the configuration file and generates a
4958
// background context from which all other contexts in the process should derive
5059
// from, as it has a service name and initial correlation ID set.
5160
func Setup(serviceName string, config *config.Config) (context.Context, func()) {
@@ -77,10 +86,12 @@ func Setup(serviceName string, config *config.Config) (context.Context, func())
7786

7887
return ctx, func() {
7988
finished()
80-
closer.Close()
89+
_ = closer.Close()
8190
}
8291
}
8392

93+
// NewLogData creates a new LogData instance with the given project, username, and IDs.
94+
// It extracts the root namespace from the project path.
8495
func NewLogData(project, username string, projectID, rootNamespaceID int) LogData {
8596
rootNameSpace := ""
8697

internal/command/githttp/pull.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ func (c *PullCommand) Execute(ctx context.Context) error {
5151

5252
// For Git over SSH routing
5353
if data.GeoProxyFetchSSHDirectToPrimary {
54-
log.ContextLogger(ctx).Info("Using Git over SSH upload pack")
55-
5654
client.Headers["Git-Protocol"] = c.Args.Env.GitProtocolVersion
5755
return c.requestSSHUploadPack(ctx, client)
5856
}
@@ -65,15 +63,9 @@ func (c *PullCommand) Execute(ctx context.Context) error {
6563
}
6664

6765
func (c *PullCommand) requestSSHUploadPack(ctx context.Context, client *git.Client) error {
68-
response, err := client.SSHUploadPack(ctx, io.NopCloser(c.ReadWriter.In))
69-
if err != nil {
70-
return err
71-
}
72-
defer response.Body.Close() //nolint:errcheck
73-
74-
_, err = io.Copy(c.ReadWriter.Out, response.Body)
66+
log.ContextLogger(ctx).Info("Using Git over SSH upload pack")
7567

76-
return err
68+
return executeSSHRequest(ctx, client.SSHUploadPack, c.ReadWriter)
7769
}
7870

7971
func (c *PullCommand) requestUploadPack(ctx context.Context, client *git.Client) error {

internal/command/githttp/push.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ func (c *PushCommand) Execute(ctx context.Context) error {
4848

4949
// For Git over SSH routing
5050
if data.GeoProxyPushSSHDirectToPrimary {
51-
log.ContextLogger(ctx).Info("Using Git over SSH receive pack")
52-
5351
client.Headers["Git-Protocol"] = c.Args.Env.GitProtocolVersion
5452
return c.requestSSHReceivePack(ctx, client)
5553
}
@@ -62,15 +60,9 @@ func (c *PushCommand) Execute(ctx context.Context) error {
6260
}
6361

6462
func (c *PushCommand) requestSSHReceivePack(ctx context.Context, client *git.Client) error {
65-
response, err := client.SSHReceivePack(ctx, io.NopCloser(c.ReadWriter.In))
66-
if err != nil {
67-
return err
68-
}
69-
defer response.Body.Close() //nolint:errcheck
70-
71-
_, err = io.Copy(c.ReadWriter.Out, response.Body)
63+
log.ContextLogger(ctx).Info("Using Git over SSH receive pack")
7264

73-
return err
65+
return executeSSHRequest(ctx, client.SSHReceivePack, c.ReadWriter)
7466
}
7567

7668
func (c *PushCommand) requestReceivePack(ctx context.Context, client *git.Client) error {

internal/command/githttp/util.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"io"
8+
"net/http"
89

910
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/readwriter"
1011
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet/git"
@@ -39,3 +40,19 @@ func requestInfoRefs(ctx context.Context, client *git.Client, command gitHTTPCom
3940

4041
return err
4142
}
43+
44+
// sshRequestFunc is a function type for SSH pack requests
45+
type sshRequestFunc func(ctx context.Context, body io.Reader) (*http.Response, error)
46+
47+
// executeSSHRequest executes an SSH request and copies the response to the output
48+
func executeSSHRequest(ctx context.Context, requestFn sshRequestFunc, rw *readwriter.ReadWriter) error {
49+
response, err := requestFn(ctx, io.NopCloser(rw.In))
50+
if err != nil {
51+
return err
52+
}
53+
defer response.Body.Close() //nolint:errcheck
54+
55+
_, err = io.Copy(rw.Out, response.Body)
56+
57+
return err
58+
}

internal/command/lfsauthenticate/lfsauthenticate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func (c *Command) Execute(ctx context.Context) (context.Context, error) {
8484
return ctxWithLogData, nil
8585
}
8686

87-
fmt.Fprintf(c.ReadWriter.Out, "%s\n", payload)
87+
if _, err := fmt.Fprintf(c.ReadWriter.Out, "%s\n", payload); err != nil {
88+
return ctxWithLogData, err
89+
}
8890

8991
return ctxWithLogData, nil
9092
}

internal/command/readwriter/readwriter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Package readwriter provides I/O abstractions for command input and output streams.
12
package readwriter
23

34
import (
45
"io"
56
)
67

8+
// ReadWriter bundles the standard input, output, and error streams for command execution.
79
type ReadWriter struct {
810
Out io.Writer
911
In io.Reader

internal/command/receivepack/gitalycall_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ func TestReceivePack(t *testing.T) {
2828

2929
testCases := []struct {
3030
username string
31-
keyId string
31+
keyID string
3232
}{
3333
{
3434
username: "john.doe",
3535
},
3636
{
37-
keyId: "123",
37+
keyID: "123",
3838
},
3939
}
4040

@@ -58,7 +58,7 @@ func TestReceivePack(t *testing.T) {
5858
if tc.username != "" {
5959
args.GitlabUsername = tc.username
6060
} else {
61-
args.GitlabKeyID = tc.keyId
61+
args.GitlabKeyID = tc.keyID
6262
}
6363

6464
cfg := &config.Config{GitlabURL: url}
@@ -95,7 +95,7 @@ func TestReceivePack(t *testing.T) {
9595
require.Equal(t, v, actual[0])
9696
}
9797
require.Empty(t, testServer.ReceivedMD["some-other-ff"])
98-
require.Equal(t, testServer.ReceivedMD["x-gitlab-correlation-id"][0], "a-correlation-id")
98+
require.Equal(t, "a-correlation-id", testServer.ReceivedMD["x-gitlab-correlation-id"][0])
9999
}
100100
})
101101
}
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
internal/command/command.go:1:1: package-comments: should have a package comment (revive)
2-
internal/command/command.go:15:6: exported: exported type Command should have comment or be unexported (revive)
3-
internal/command/command.go:19:6: exported: exported type LogMetadata should have comment or be unexported (revive)
4-
internal/command/command.go:26:6: exported: exported type LogData should have comment or be unexported (revive)
5-
internal/command/command.go:37:1: exported: exported function CheckForVersionFlag should have comment or be unexported (revive)
6-
internal/command/command.go:48:1: exported: comment on exported function Setup should be of the form "Setup ..." (revive)
7-
internal/command/command.go:80:15: Error return value of `closer.Close` is not checked (errcheck)
8-
internal/command/command.go:84:1: exported: exported function NewLogData should have comment or be unexported (revive)
9-
internal/command/githttp/pull.go:48: 48-65 lines are duplicate of `internal/command/githttp/push.go:45-62` (dupl)
10-
internal/command/githttp/push.go:45: 45-62 lines are duplicate of `internal/command/githttp/pull.go:48-65` (dupl)
11-
internal/command/lfsauthenticate/lfsauthenticate.go:87:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
12-
internal/command/readwriter/readwriter.go:1:1: package-comments: should have a package comment (revive)
13-
internal/command/readwriter/readwriter.go:7:6: exported: exported type ReadWriter should have comment or be unexported (revive)
14-
internal/command/receivepack/gitalycall_test.go:31:5: var-naming: struct field keyId should be keyID (revive)
15-
internal/command/receivepack/gitalycall_test.go:98:5: expected-actual: need to reverse actual and expected values (testifylint)

0 commit comments

Comments
 (0)