- Notifications
You must be signed in to change notification settings - Fork905
feat: add coder connect exists hidden subcommand#17418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cli | ||
import ( | ||
"github.com/coder/serpent" | ||
"github.com/coder/coder/v2/codersdk/workspacesdk" | ||
) | ||
func (r *RootCmd) connectCmd() *serpent.Command { | ||
cmd := &serpent.Command{ | ||
Use: "connect", | ||
Short: "Commands related to Coder Connect (OS-level tunneled connection to workspaces).", | ||
Handler: func(i *serpent.Invocation) error { | ||
return i.Command.HelpHandler(i) | ||
}, | ||
Hidden: true, | ||
Children: []*serpent.Command{ | ||
r.existsCmd(), | ||
}, | ||
} | ||
return cmd | ||
} | ||
func (*RootCmd) existsCmd() *serpent.Command { | ||
cmd := &serpent.Command{ | ||
Use: "exists <hostname>", | ||
Short: "Checks if the given hostname exists via Coder Connect.", | ||
Long: "This command is designed to be used in scripts to check if the given hostname exists via Coder " + | ||
"Connect. It prints no output. It returns exit code 0 if it does exist and code 1 if it does not.", | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(1), | ||
), | ||
Handler: func(inv *serpent.Invocation) error { | ||
hostname := inv.Args[0] | ||
exists, err := workspacesdk.ExistsViaCoderConnect(inv.Context(), hostname) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
// we don't want to print any output, since this command is designed to be a check in scripts / SSH config. | ||
return ErrSilent | ||
} | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cli_test | ||
import ( | ||
"bytes" | ||
"context" | ||
"net" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"tailscale.com/net/tsaddr" | ||
"github.com/coder/serpent" | ||
"github.com/coder/coder/v2/cli" | ||
"github.com/coder/coder/v2/codersdk/workspacesdk" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
func TestConnectExists_Running(t *testing.T) { | ||
t.Parallel() | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
var root cli.RootCmd | ||
cmd, err := root.Command(root.AGPL()) | ||
require.NoError(t, err) | ||
inv := (&serpent.Invocation{ | ||
Command: cmd, | ||
Args: []string{"connect", "exists", "test.example"}, | ||
}).WithContext(withCoderConnectRunning(ctx)) | ||
stdout := new(bytes.Buffer) | ||
stderr := new(bytes.Buffer) | ||
inv.Stdout = stdout | ||
inv.Stderr = stderr | ||
err = inv.Run() | ||
require.NoError(t, err) | ||
} | ||
func TestConnectExists_NotRunning(t *testing.T) { | ||
t.Parallel() | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
var root cli.RootCmd | ||
cmd, err := root.Command(root.AGPL()) | ||
require.NoError(t, err) | ||
inv := (&serpent.Invocation{ | ||
Command: cmd, | ||
Args: []string{"connect", "exists", "test.example"}, | ||
}).WithContext(withCoderConnectNotRunning(ctx)) | ||
stdout := new(bytes.Buffer) | ||
stderr := new(bytes.Buffer) | ||
inv.Stdout = stdout | ||
inv.Stderr = stderr | ||
err = inv.Run() | ||
require.ErrorIs(t, err, cli.ErrSilent) | ||
} | ||
type fakeResolver struct { | ||
shouldReturnSuccess bool | ||
} | ||
func (f *fakeResolver) LookupIP(_ context.Context, _, _ string) ([]net.IP, error) { | ||
if f.shouldReturnSuccess { | ||
return []net.IP{net.ParseIP(tsaddr.CoderServiceIPv6().String())}, nil | ||
} | ||
return nil, &net.DNSError{IsNotFound: true} | ||
} | ||
func withCoderConnectRunning(ctx context.Context) context.Context { | ||
return workspacesdk.WithTestOnlyCoderContextResolver(ctx, &fakeResolver{shouldReturnSuccess: true}) | ||
} | ||
func withCoderConnectNotRunning(ctx context.Context) context.Context { | ||
return workspacesdk.WithTestOnlyCoderContextResolver(ctx, &fakeResolver{shouldReturnSuccess: false}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -31,14 +31,15 @@ import ( | ||
"github.com/coder/pretty" | ||
"github.com/coder/serpent" | ||
"github.com/coder/coder/v2/buildinfo" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/cli/config" | ||
"github.com/coder/coder/v2/cli/gitauth" | ||
"github.com/coder/coder/v2/cli/telemetry" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/codersdk/agentsdk" | ||
) | ||
var ( | ||
@@ -49,6 +50,10 @@ var ( | ||
workspaceCommand = map[string]string{ | ||
"workspaces": "", | ||
} | ||
// ErrSilent is a sentinel error that tells the command handler to just exit with a non-zero error, but not print | ||
// anything. | ||
ErrSilent = xerrors.New("silent error") | ||
) | ||
const ( | ||
@@ -122,6 +127,7 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command { | ||
r.whoami(), | ||
// Hidden | ||
r.connectCmd(), | ||
r.expCmd(), | ||
r.gitssh(), | ||
r.support(), | ||
@@ -175,6 +181,10 @@ func (r *RootCmd) RunWithSubcommands(subcommands []*serpent.Command) { | ||
//nolint:revive,gocritic | ||
os.Exit(code) | ||
} | ||
if errors.Is(err, ErrSilent) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||
//nolint:revive,gocritic | ||
os.Exit(code) | ||
} | ||
f := PrettyErrorFormatter{w: os.Stderr, verbose: r.verbose} | ||
if err != nil { | ||
f.Format(err) | ||
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.