Skip to content

Commit b713372

Browse files
committed
359 problems left...
Started refactoring production code too 🥴
1 parent 5613d3b commit b713372

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+582
-553
lines changed

cli/agent.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"time"
1616

1717
"cloud.google.com/go/compute/metadata"
18-
"github.com/spf13/cobra"
1918
"golang.org/x/xerrors"
2019
"gopkg.in/natefinch/lumberjack.v2"
2120

@@ -24,23 +23,24 @@ import (
2423
"github.com/coder/coder/agent"
2524
"github.com/coder/coder/agent/reaper"
2625
"github.com/coder/coder/buildinfo"
26+
"github.com/coder/coder/cli/clibase"
2727
"github.com/coder/coder/cli/cliflag"
2828
"github.com/coder/coder/codersdk/agentsdk"
2929
)
3030

31-
func workspaceAgent() *cobra.Command {
31+
func workspaceAgent() *clibase.Command {
3232
var (
3333
auth string
3434
logDir string
3535
pprofAddress string
3636
noReap bool
3737
)
38-
cmd := &cobra.Command{
38+
cmd := &clibase.Command{
3939
Use: "agent",
4040
// This command isn't useful to manually execute.
4141
Hidden: true,
42-
RunE: func(cmd *cobra.Command, _ []string) error {
43-
ctx, cancel := context.WithCancel(cmd.Context())
42+
Handler: func(inv *clibase.Invokation) error {
43+
ctx, cancel := context.WithCancel(inv.Context())
4444
defer cancel()
4545

4646
rawURL, err := cmd.Flags().GetString(varAgentURL)
@@ -62,7 +62,7 @@ func workspaceAgent() *cobra.Command {
6262
MaxSize: 5, // MB
6363
}
6464
defer logWriter.Close()
65-
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
65+
logger := slog.Make(sloghuman.Sink(inv.Stderr), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
6666

6767
logger.Info(ctx, "spawning reaper process")
6868
// Do not start a reaper on the child process. It's important
@@ -104,7 +104,7 @@ func workspaceAgent() *cobra.Command {
104104
logWriter := &closeWriter{w: ljLogger}
105105
defer logWriter.Close()
106106

107-
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
107+
logger := slog.Make(sloghuman.Sink(inv.Stderr), sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
108108

109109
version := buildinfo.Version()
110110
logger.Info(ctx, "starting agent",

cli/cliflag/cliflag.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import (
1616
"strings"
1717
"time"
1818

19-
"github.com/spf13/cobra"
2019
"github.com/spf13/pflag"
2120

21+
"github.com/coder/coder/cli/clibase"
2222
"github.com/coder/coder/cli/cliui"
2323
)
2424

2525
// IsSetBool returns the value of the boolean flag if it is set.
2626
// It returns false if the flag isn't set or if any error occurs attempting
2727
// to parse the value of the flag.
28-
func IsSetBool(cmd *cobra.Command, name string) bool {
28+
func IsSetBool(cmd *clibase.Command, name string) bool {
2929
val, ok := IsSet(cmd, name)
3030
if !ok {
3131
return false
@@ -36,7 +36,7 @@ func IsSetBool(cmd *cobra.Command, name string) bool {
3636
}
3737

3838
// IsSet returns the string value of the flag and whether it was set.
39-
func IsSet(cmd *cobra.Command, name string) (string, bool) {
39+
func IsSet(cmd *clibase.Command, name string) (string, bool) {
4040
flag := cmd.Flag(name)
4141
if flag == nil {
4242
return "", false

cli/cliui/agent_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/spf13/cobra"
98
"github.com/stretchr/testify/assert"
109
"github.com/stretchr/testify/require"
1110
"go.uber.org/atomic"
@@ -67,9 +66,9 @@ func TestAgent_TimeoutWithTroubleshootingURL(t *testing.T) {
6766
wantURL := "https://coder.com/troubleshoot"
6867

6968
var connected, timeout atomic.Bool
70-
cmd := &cobra.Command{
71-
RunE: func(cmd *cobra.Command, _ []string) error {
72-
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
69+
cmd := &clibase.Command{
70+
Handler: func(inv *clibase.Invokation) error {
71+
err := cliui.Agent(inv.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
7372
WorkspaceName: "example",
7473
Fetch: func(_ context.Context) (codersdk.WorkspaceAgent, error) {
7574
agent := codersdk.WorkspaceAgent{
@@ -116,9 +115,9 @@ func TestAgent_StartupTimeout(t *testing.T) {
116115
var status, state atomic.String
117116
setStatus := func(s codersdk.WorkspaceAgentStatus) { status.Store(string(s)) }
118117
setState := func(s codersdk.WorkspaceAgentLifecycle) { state.Store(string(s)) }
119-
cmd := &cobra.Command{
120-
RunE: func(cmd *cobra.Command, _ []string) error {
121-
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
118+
cmd := &clibase.Command{
119+
Handler: func(inv *clibase.Invokation) error {
120+
err := cliui.Agent(inv.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
122121
WorkspaceName: "example",
123122
Fetch: func(_ context.Context) (codersdk.WorkspaceAgent, error) {
124123
agent := codersdk.WorkspaceAgent{
@@ -174,9 +173,9 @@ func TestAgent_StartErrorExit(t *testing.T) {
174173
var status, state atomic.String
175174
setStatus := func(s codersdk.WorkspaceAgentStatus) { status.Store(string(s)) }
176175
setState := func(s codersdk.WorkspaceAgentLifecycle) { state.Store(string(s)) }
177-
cmd := &cobra.Command{
178-
RunE: func(cmd *cobra.Command, _ []string) error {
179-
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
176+
cmd := &clibase.Command{
177+
Handler: func(inv *clibase.Invokation) error {
178+
err := cliui.Agent(inv.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
180179
WorkspaceName: "example",
181180
Fetch: func(_ context.Context) (codersdk.WorkspaceAgent, error) {
182181
agent := codersdk.WorkspaceAgent{
@@ -229,9 +228,9 @@ func TestAgent_NoWait(t *testing.T) {
229228
var status, state atomic.String
230229
setStatus := func(s codersdk.WorkspaceAgentStatus) { status.Store(string(s)) }
231230
setState := func(s codersdk.WorkspaceAgentLifecycle) { state.Store(string(s)) }
232-
cmd := &cobra.Command{
233-
RunE: func(cmd *cobra.Command, _ []string) error {
234-
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
231+
cmd := &clibase.Command{
232+
Handler: func(inv *clibase.Invokation) error {
233+
err := cliui.Agent(inv.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
235234
WorkspaceName: "example",
236235
Fetch: func(_ context.Context) (codersdk.WorkspaceAgent, error) {
237236
agent := codersdk.WorkspaceAgent{
@@ -298,9 +297,9 @@ func TestAgent_LoginBeforeReadyEnabled(t *testing.T) {
298297
var status, state atomic.String
299298
setStatus := func(s codersdk.WorkspaceAgentStatus) { status.Store(string(s)) }
300299
setState := func(s codersdk.WorkspaceAgentLifecycle) { state.Store(string(s)) }
301-
cmd := &cobra.Command{
302-
RunE: func(cmd *cobra.Command, _ []string) error {
303-
err := cliui.Agent(cmd.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
300+
cmd := &clibase.Command{
301+
Handler: func(inv *clibase.Invokation) error {
302+
err := cliui.Agent(inv.Context(), cmd.OutOrStdout(), cliui.AgentOptions{
304303
WorkspaceName: "example",
305304
Fetch: func(_ context.Context) (codersdk.WorkspaceAgent, error) {
306305
agent := codersdk.WorkspaceAgent{

cli/cliui/gitauth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/spf13/cobra"
1110
"github.com/stretchr/testify/assert"
1211

12+
"github.com/coder/coder/cli/clibase"
1313
"github.com/coder/coder/cli/cliui"
1414
"github.com/coder/coder/codersdk"
1515
"github.com/coder/coder/pty/ptytest"
@@ -23,10 +23,10 @@ func TestGitAuth(t *testing.T) {
2323
defer cancel()
2424

2525
ptty := ptytest.New(t)
26-
cmd := &cobra.Command{
27-
RunE: func(cmd *cobra.Command, args []string) error {
26+
cmd := &clibase.Command{
27+
Handler: func(inv *clibase.Invokation) error {
2828
var fetched atomic.Bool
29-
return cliui.GitAuth(cmd.Context(), cmd.OutOrStdout(), cliui.GitAuthOptions{
29+
return cliui.GitAuth(inv.Context(), cmd.OutOrStdout(), cliui.GitAuthOptions{
3030
Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionGitAuth, error) {
3131
defer fetched.Store(true)
3232
return []codersdk.TemplateVersionGitAuth{{

cli/cliui/output.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"reflect"
77
"strings"
88

9-
"github.com/spf13/cobra"
9+
"github.com/coder/coder/cli/clibase"
1010
"golang.org/x/xerrors"
1111
)
1212

1313
type OutputFormat interface {
1414
ID() string
15-
AttachFlags(cmd *cobra.Command)
15+
AttachFlags(cmd *clibase.Command)
1616
Format(ctx context.Context, data any) (string, error)
1717
}
1818

@@ -47,7 +47,7 @@ func NewOutputFormatter(formats ...OutputFormat) *OutputFormatter {
4747

4848
// AttachFlags attaches the --output flag to the given command, and any
4949
// additional flags required by the output formatters.
50-
func (f *OutputFormatter) AttachFlags(cmd *cobra.Command) {
50+
func (f *OutputFormatter) AttachFlags(cmd *clibase.Command) {
5151
for _, format := range f.formats {
5252
format.AttachFlags(cmd)
5353
}
@@ -119,7 +119,7 @@ func (*tableFormat) ID() string {
119119
}
120120

121121
// AttachFlags implements OutputFormat.
122-
func (f *tableFormat) AttachFlags(cmd *cobra.Command) {
122+
func (f *tableFormat) AttachFlags(cmd *clibase.Command) {
123123
cmd.Flags().StringSliceVarP(&f.columns, "column", "c", f.defaultColumns, "Columns to display in table output. Available columns: "+strings.Join(f.allColumns, ", "))
124124
}
125125

@@ -143,7 +143,7 @@ func (jsonFormat) ID() string {
143143
}
144144

145145
// AttachFlags implements OutputFormat.
146-
func (jsonFormat) AttachFlags(_ *cobra.Command) {}
146+
func (jsonFormat) AttachFlags(_ *clibase.Command) {}
147147

148148
// Format implements OutputFormat.
149149
func (jsonFormat) Format(_ context.Context, data any) (string, error) {

cli/cliui/output_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"sync/atomic"
77
"testing"
88

9-
"github.com/spf13/cobra"
109
"github.com/stretchr/testify/require"
1110

11+
"github.com/coder/coder/cli/clibase"
1212
"github.com/coder/coder/cli/cliui"
1313
)
1414

1515
type format struct {
1616
id string
17-
attachFlagsFn func(cmd *cobra.Command)
17+
attachFlagsFn func(cmd *clibase.Command)
1818
formatFn func(ctx context.Context, data any) (string, error)
1919
}
2020

@@ -24,7 +24,7 @@ func (f *format) ID() string {
2424
return f.id
2525
}
2626

27-
func (f *format) AttachFlags(cmd *cobra.Command) {
27+
func (f *format) AttachFlags(cmd *clibase.Command) {
2828
if f.attachFlagsFn != nil {
2929
f.attachFlagsFn(cmd)
3030
}
@@ -82,7 +82,7 @@ func Test_OutputFormatter(t *testing.T) {
8282
cliui.JSONFormat(),
8383
&format{
8484
id: "foo",
85-
attachFlagsFn: func(cmd *cobra.Command) {
85+
attachFlagsFn: func(cmd *clibase.Command) {
8686
cmd.Flags().StringP("foo", "f", "", "foo flag 1234")
8787
},
8888
formatFn: func(_ context.Context, _ any) (string, error) {
@@ -92,7 +92,7 @@ func Test_OutputFormatter(t *testing.T) {
9292
},
9393
)
9494

95-
cmd := &cobra.Command{}
95+
cmd := &clibase.Command{}
9696
f.AttachFlags(cmd)
9797

9898
selected, err := cmd.Flags().GetString("output")

cli/cliui/parameter.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/spf13/cobra"
8-
7+
"github.com/coder/coder/cli/clibase"
98
"github.com/coder/coder/coderd/parameter"
109
"github.com/coder/coder/codersdk"
1110
)
1211

13-
func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.ParameterSchema) (string, error) {
12+
func ParameterSchema(cmd *clibase.Command, parameterSchema codersdk.ParameterSchema) (string, error) {
1413
_, _ = fmt.Fprintln(cmd.OutOrStdout(), Styles.Bold.Render("var."+parameterSchema.Name))
1514
if parameterSchema.Description != "" {
1615
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+strings.TrimSpace(strings.Join(strings.Split(parameterSchema.Description, "\n"), "\n "))+"\n")
@@ -61,7 +60,7 @@ func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.ParameterSchem
6160
return value, nil
6261
}
6362

64-
func RichParameter(cmd *cobra.Command, templateVersionParameter codersdk.TemplateVersionParameter) (string, error) {
63+
func RichParameter(cmd *clibase.Command, templateVersionParameter codersdk.TemplateVersionParameter) (string, error) {
6564
_, _ = fmt.Fprintln(cmd.OutOrStdout(), Styles.Bold.Render(templateVersionParameter.Name))
6665
if templateVersionParameter.DescriptionPlaintext != "" {
6766
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+strings.TrimSpace(strings.Join(strings.Split(templateVersionParameter.DescriptionPlaintext, "\n"), "\n "))+"\n")

cli/cliui/prompt.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"strings"
1111

1212
"github.com/bgentry/speakeasy"
13+
"github.com/coder/coder/cli/clibase"
1314
"github.com/mattn/go-isatty"
14-
"github.com/spf13/cobra"
1515
"golang.org/x/xerrors"
1616
)
1717

@@ -26,7 +26,7 @@ type PromptOptions struct {
2626

2727
const skipPromptFlag = "yes"
2828

29-
func AllowSkipPrompt(cmd *cobra.Command) {
29+
func AllowSkipPrompt(cmd *clibase.Command) {
3030
cmd.Flags().BoolP(skipPromptFlag, "y", false, "Bypass prompts")
3131
}
3232

@@ -36,7 +36,7 @@ const (
3636
)
3737

3838
// Prompt asks the user for input.
39-
func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
39+
func Prompt(cmd *clibase.Command, opts PromptOptions) (string, error) {
4040
// If the cmd has a "yes" flag for skipping confirm prompts, honor it.
4141
// If it's not a "Confirm" prompt, then don't skip. As the default value of
4242
// "yes" makes no sense.
@@ -114,8 +114,8 @@ func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
114114
}
115115
}
116116
return line, nil
117-
case <-cmd.Context().Done():
118-
return "", cmd.Context().Err()
117+
case <-inv.Context().Done():
118+
return "", inv.Context().Err()
119119
case <-interrupt:
120120
// Print a newline so that any further output starts properly on a new line.
121121
_, _ = fmt.Fprintln(cmd.OutOrStdout())

cli/cliui/prompt_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"os/exec"
99
"testing"
1010

11-
"github.com/spf13/cobra"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
1413

14+
"github.com/coder/coder/cli/clibase"
1515
"github.com/coder/coder/cli/cliui"
1616
"github.com/coder/coder/pty"
1717
"github.com/coder/coder/pty/ptytest"
@@ -77,7 +77,7 @@ func TestPrompt(t *testing.T) {
7777
resp, err := newPrompt(ptty, cliui.PromptOptions{
7878
Text: "ShouldNotSeeThis",
7979
IsConfirm: true,
80-
}, func(cmd *cobra.Command) {
80+
}, func(cmd *clibase.Command) {
8181
cliui.AllowSkipPrompt(cmd)
8282
cmd.SetArgs([]string{"-y"})
8383
})
@@ -145,10 +145,10 @@ func TestPrompt(t *testing.T) {
145145
})
146146
}
147147

148-
func newPrompt(ptty *ptytest.PTY, opts cliui.PromptOptions, cmdOpt func(cmd *cobra.Command)) (string, error) {
148+
func newPrompt(ptty *ptytest.PTY, opts cliui.PromptOptions, cmdOpt func(cmd *clibase.Command)) (string, error) {
149149
value := ""
150-
cmd := &cobra.Command{
151-
RunE: func(cmd *cobra.Command, args []string) error {
150+
cmd := &clibase.Command{
151+
Handler: func(inv *clibase.Invokation) error {
152152
var err error
153153
value, err = cliui.Prompt(cmd, opts)
154154
return err
@@ -208,8 +208,8 @@ func TestPasswordTerminalState(t *testing.T) {
208208

209209
// nolint:unused
210210
func passwordHelper() {
211-
cmd := &cobra.Command{
212-
Run: func(cmd *cobra.Command, args []string) {
211+
cmd := &clibase.Command{
212+
Run: func(inv *clibase.Invokation) {
213213
cliui.Prompt(cmd, cliui.PromptOptions{
214214
Text: "Password:",
215215
Secret: true,

0 commit comments

Comments
 (0)