Skip to content

Commit 3f8f61b

Browse files
authored
fix(lint): resolve dupl findings (#891)
1 parent ffb6784 commit 3f8f61b

7 files changed

Lines changed: 94 additions & 100 deletions

File tree

‎cmd/pro/check_update.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type CheckUpdateCmd struct {
2525
}
2626

2727
// NewCheckUpdateCmd creates a new command.
28+
//
29+
//nolint:dupl // structurally similar to NewHealthCmd; intentional sibling factory
2830
func NewCheckUpdateCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
2931
cmd := &CheckUpdateCmd{
3032
GlobalFlags: globalFlags,

‎cmd/pro/daemon/netcheck.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type NetcheckCmd struct {
2727
}
2828

2929
// NewNetcheckCmd creates a new command.
30+
//
31+
//nolint:dupl // structurally similar to NewStatusCmd; intentional sibling factory
3032
func NewNetcheckCmd(flags *proflags.GlobalFlags) *cobra.Command {
3133
cmd := &NetcheckCmd{
3234
GlobalFlags: flags,

‎cmd/pro/daemon/status.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type StatusCmd struct {
2424
}
2525

2626
// NewStatusCmd creates a new command.
27+
//
28+
//nolint:dupl // structurally similar to NewNetcheckCmd; intentional sibling factory
2729
func NewStatusCmd(flags *proflags.GlobalFlags) *cobra.Command {
2830
cmd := &StatusCmd{
2931
GlobalFlags: flags,

‎cmd/pro/provider/exec.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/url"
8+
9+
"github.com/devsy-org/devsy/pkg/platform"
10+
"github.com/devsy-org/devsy/pkg/platform/client"
11+
"github.com/devsy-org/devsy/pkg/platform/remotecommand"
12+
)
13+
14+
type dialAndExecuteParams struct {
15+
configPath string
16+
action string
17+
envFlags url.Values
18+
stdin io.Reader
19+
stdout io.Writer
20+
stderr io.Writer
21+
}
22+
23+
// dialAndExecute finds the current workspace, dials the given sub-resource
24+
// action on it, and streams the resulting connection through stdin/stdout/stderr.
25+
func dialAndExecute(ctx context.Context, params dialAndExecuteParams) error {
26+
baseClient, err := client.InitClientFromPath(ctx, params.configPath)
27+
if err != nil {
28+
return err
29+
}
30+
31+
info, err := platform.GetWorkspaceInfoFromEnv()
32+
if err != nil {
33+
return err
34+
}
35+
opts := platform.FindInstanceOptions{UID: info.UID, ProjectName: info.ProjectName}
36+
workspace, err := platform.FindInstance(ctx, baseClient, opts)
37+
if err != nil {
38+
return err
39+
} else if workspace == nil {
40+
return fmt.Errorf("couldn't find workspace")
41+
}
42+
43+
conn, err := platform.DialInstance(baseClient, workspace, params.action, params.envFlags)
44+
if err != nil {
45+
return err
46+
}
47+
48+
_, err = remotecommand.ExecuteConn(ctx, conn, params.stdin, params.stdout, params.stderr)
49+
if err != nil {
50+
return fmt.Errorf("error executing: %w", err)
51+
}
52+
53+
return nil
54+
}

‎cmd/pro/provider/ssh.go‎

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
//nolint:dupl // structurally similar to stop.go; intentional sibling command sharing dialAndExecute
12
package provider
23

34
import (
45
"context"
5-
"fmt"
66
"io"
77
"os"
88

99
"github.com/devsy-org/devsy/cmd/pro/flags"
1010
"github.com/devsy-org/devsy/pkg/config"
1111
"github.com/devsy-org/devsy/pkg/platform"
12-
"github.com/devsy-org/devsy/pkg/platform/client"
13-
"github.com/devsy-org/devsy/pkg/platform/remotecommand"
1412
"github.com/spf13/cobra"
1513
)
1614

@@ -43,43 +41,12 @@ func (cmd *SshCmd) Run(
4341
stdout io.Writer,
4442
stderr io.Writer,
4543
) error {
46-
baseClient, err := client.InitClientFromPath(ctx, cmd.Config)
47-
if err != nil {
48-
return err
49-
}
50-
51-
info, err := platform.GetWorkspaceInfoFromEnv()
52-
if err != nil {
53-
return err
54-
}
55-
opts := platform.FindInstanceOptions{UID: info.UID, ProjectName: info.ProjectName}
56-
workspace, err := platform.FindInstance(ctx, baseClient, opts)
57-
if err != nil {
58-
return err
59-
} else if workspace == nil {
60-
return fmt.Errorf("couldn't find workspace")
61-
}
62-
63-
conn, err := platform.DialInstance(
64-
baseClient,
65-
workspace,
66-
"ssh",
67-
platform.OptionsFromEnv(config.EnvFlagsSSH),
68-
)
69-
if err != nil {
70-
return err
71-
}
72-
73-
_, err = remotecommand.ExecuteConn(
74-
ctx,
75-
conn,
76-
stdin,
77-
stdout,
78-
stderr,
79-
)
80-
if err != nil {
81-
return fmt.Errorf("error executing: %w", err)
82-
}
83-
84-
return nil
44+
return dialAndExecute(ctx, dialAndExecuteParams{
45+
configPath: cmd.Config,
46+
action: "ssh",
47+
envFlags: platform.OptionsFromEnv(config.EnvFlagsSSH),
48+
stdin: stdin,
49+
stdout: stdout,
50+
stderr: stderr,
51+
})
8552
}

‎cmd/pro/provider/stop.go‎

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
//nolint:dupl // structurally similar to ssh.go; intentional sibling command sharing dialAndExecute
12
package provider
23

34
import (
45
"context"
5-
"fmt"
66
"io"
77
"os"
88

99
storagev1 "github.com/devsy-org/api/pkg/apis/storage/v1"
1010
"github.com/devsy-org/devsy/cmd/pro/flags"
1111
"github.com/devsy-org/devsy/pkg/platform"
12-
"github.com/devsy-org/devsy/pkg/platform/client"
13-
"github.com/devsy-org/devsy/pkg/platform/remotecommand"
1412
"github.com/spf13/cobra"
1513
)
1614

@@ -43,43 +41,12 @@ func (cmd *StopCmd) Run(
4341
stdout io.Writer,
4442
stderr io.Writer,
4543
) error {
46-
baseClient, err := client.InitClientFromPath(ctx, cmd.Config)
47-
if err != nil {
48-
return err
49-
}
50-
51-
info, err := platform.GetWorkspaceInfoFromEnv()
52-
if err != nil {
53-
return err
54-
}
55-
opts := platform.FindInstanceOptions{UID: info.UID, ProjectName: info.ProjectName}
56-
workspace, err := platform.FindInstance(ctx, baseClient, opts)
57-
if err != nil {
58-
return err
59-
} else if workspace == nil {
60-
return fmt.Errorf("couldn't find workspace")
61-
}
62-
63-
conn, err := platform.DialInstance(
64-
baseClient,
65-
workspace,
66-
"stop",
67-
platform.OptionsFromEnv(storagev1.DevsyFlagsStop),
68-
)
69-
if err != nil {
70-
return err
71-
}
72-
73-
_, err = remotecommand.ExecuteConn(
74-
ctx,
75-
conn,
76-
stdin,
77-
stdout,
78-
stderr,
79-
)
80-
if err != nil {
81-
return fmt.Errorf("error executing: %w", err)
82-
}
83-
84-
return nil
44+
return dialAndExecute(ctx, dialAndExecuteParams{
45+
configPath: cmd.Config,
46+
action: "stop",
47+
envFlags: platform.OptionsFromEnv(storagev1.DevsyFlagsStop),
48+
stdin: stdin,
49+
stdout: stdout,
50+
stderr: stderr,
51+
})
8552
}

‎pkg/options/resolver/resolver_test.go‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,7 @@ func (suite *ResolverTestSuite) TestResolveOptions_ExpiredCache() {
215215
}
216216

217217
func (suite *ResolverTestSuite) TestResolveOptions_PreserveChildWhenParentUnchanged() {
218-
suite.resolver.graph = graph.NewGraph[*types.Option]()
219-
220-
parentOption := &types.Option{Description: "Parent", Default: "new_parent_value"}
221-
childOption := &types.Option{Description: "Child", Default: "child_default"}
222-
223-
suite.Require().NoError(suite.resolver.graph.AddNode("parent", parentOption))
224-
suite.Require().NoError(suite.resolver.graph.AddNode("child", childOption))
225-
suite.Require().NoError(suite.resolver.graph.AddEdge("parent", "child"))
218+
suite.setupParentChildGraph()
226219

227220
existingValues := map[string]config.OptionValue{
228221
"parent": {Value: "old_parent_value", UserProvided: true},
@@ -237,14 +230,7 @@ func (suite *ResolverTestSuite) TestResolveOptions_PreserveChildWhenParentUnchan
237230
}
238231

239232
func (suite *ResolverTestSuite) TestResolveOptions_PreserveUserProvidedChild() {
240-
suite.resolver.graph = graph.NewGraph[*types.Option]()
241-
242-
parentOption := &types.Option{Description: "Parent", Default: "new_parent_value"}
243-
childOption := &types.Option{Description: "Child", Default: "child_default"}
244-
245-
suite.Require().NoError(suite.resolver.graph.AddNode("parent", parentOption))
246-
suite.Require().NoError(suite.resolver.graph.AddNode("child", childOption))
247-
suite.Require().NoError(suite.resolver.graph.AddEdge("parent", "child"))
233+
suite.setupParentChildGraph()
248234

249235
existingValues := map[string]config.OptionValue{
250236
"parent": {Value: "old_parent_value", UserProvided: true},
@@ -321,3 +307,17 @@ func (suite *ResolverTestSuite) TestAddOptionsToGraph_MultipleCalls() {
321307
nodes := g.GetNodes()
322308
suite.Len(nodes, 2, "Multiple calls to addOptionsToGraph should not duplicate nodes.")
323309
}
310+
311+
// setupParentChildGraph creates a resolver graph with a "parent" option that
312+
// has a "child" option depending on it, used by tests that verify
313+
// user-provided-value precedence during resolution.
314+
func (suite *ResolverTestSuite) setupParentChildGraph() {
315+
suite.resolver.graph = graph.NewGraph[*types.Option]()
316+
317+
parentOption := &types.Option{Description: "Parent", Default: "new_parent_value"}
318+
childOption := &types.Option{Description: "Child", Default: "child_default"}
319+
320+
suite.Require().NoError(suite.resolver.graph.AddNode("parent", parentOption))
321+
suite.Require().NoError(suite.resolver.graph.AddNode("child", childOption))
322+
suite.Require().NoError(suite.resolver.graph.AddEdge("parent", "child"))
323+
}

0 commit comments

Comments
 (0)