Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions cmd/entire/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"runtime"
"time"

"github.com/atotto/clipboard"
"github.com/entireio/cli/cmd/entire/cli/auth"
"github.com/entireio/cli/cmd/entire/cli/interactive"
"github.com/spf13/cobra"
Expand All @@ -26,6 +27,9 @@ const maxTransientErrors = 5
// browserOpenFunc is the signature for opening a URL in the user's browser.
type browserOpenFunc func(ctx context.Context, url string) error

// clipboardWriteFunc is the signature for copying text to the user's clipboard.
type clipboardWriteFunc func(text string) error

// deviceAuthClient abstracts the auth client so runLogin and waitForApproval can be unit-tested.
type deviceAuthClient interface {
StartDeviceAuth(ctx context.Context) (*auth.DeviceAuthStart, error)
Expand All @@ -42,20 +46,23 @@ func newLoginCmd() *cobra.Command {
if err := requireSecureBaseURL(insecureHTTPAuth); err != nil {
return err
}
return runLogin(cmd.Context(), cmd.OutOrStdout(), cmd.ErrOrStderr(), auth.NewClient(nil), openBrowser)
return runLogin(cmd.Context(), cmd.OutOrStdout(), cmd.ErrOrStderr(), auth.NewClient(nil), openBrowser, copyToClipboard)
},
}
addInsecureHTTPAuthFlag(cmd, &insecureHTTPAuth)
return cmd
}

func runLogin(ctx context.Context, outW, errW io.Writer, client deviceAuthClient, openURL browserOpenFunc) error {
func runLogin(ctx context.Context, outW, errW io.Writer, client deviceAuthClient, openURL browserOpenFunc, writeClipboard clipboardWriteFunc) error {
start, err := client.StartDeviceAuth(ctx)
if err != nil {
return fmt.Errorf("start login: %w", err)
}

fmt.Fprintf(outW, "Device code: %s\n", start.UserCode)
if copied := copyDeviceCodeToClipboard(errW, start.UserCode, writeClipboard); copied {
fmt.Fprintln(outW, "Device code copied to clipboard.")
}

approvalURL := start.VerificationURI

Expand Down Expand Up @@ -94,6 +101,24 @@ func runLogin(ctx context.Context, outW, errW io.Writer, client deviceAuthClient
return nil
}

func copyDeviceCodeToClipboard(errW io.Writer, userCode string, writeClipboard clipboardWriteFunc) bool {
if writeClipboard == nil {
return false
}
if err := writeClipboard(userCode); err != nil {
fmt.Fprintf(errW, "Warning: failed to copy device code to clipboard: %v\n", err)
return false
}
return true
}

func copyToClipboard(text string) error {
if err := clipboard.WriteAll(text); err != nil {
return fmt.Errorf("write clipboard: %w", err)
}
return nil
}

func waitForApproval(ctx context.Context, poller deviceAuthClient, deviceCode string, expiresIn int, interval, slowDownBackoff time.Duration) (string, error) {
expiry := time.Duration(expiresIn) * time.Second
if expiry <= 0 || expiry > maxExpiresIn {
Expand Down
38 changes: 38 additions & 0 deletions cmd/entire/cli/login_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"bytes"
"context"
"errors"
"strings"
Expand Down Expand Up @@ -38,6 +39,43 @@ func (m *mockClient) PollDeviceAuth(_ context.Context, _ string) (*auth.DeviceAu
return r.result, r.err
}

func TestCopyDeviceCodeToClipboard_Success(t *testing.T) {
t.Parallel()

var errBuf bytes.Buffer
var copied string
ok := copyDeviceCodeToClipboard(&errBuf, "ABCD-1234", func(text string) error {
copied = text
return nil
})

if !ok {
t.Fatal("copyDeviceCodeToClipboard() = false, want true")
}
if copied != "ABCD-1234" {
t.Fatalf("copied = %q, want device code", copied)
}
if errBuf.Len() != 0 {
t.Fatalf("stderr = %q, want empty", errBuf.String())
}
}

func TestCopyDeviceCodeToClipboard_Failure(t *testing.T) {
t.Parallel()

var errBuf bytes.Buffer
ok := copyDeviceCodeToClipboard(&errBuf, "ABCD-1234", func(_ string) error {
return errors.New("clipboard unavailable")
})

if ok {
t.Fatal("copyDeviceCodeToClipboard() = true, want false")
}
if !strings.Contains(errBuf.String(), "failed to copy device code to clipboard") {
t.Fatalf("stderr = %q, want clipboard warning", errBuf.String())
}
}

func TestWaitForApproval_ImmediateSuccess(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
charm.land/glamour/v2 v2.0.0
charm.land/huh/v2 v2.0.3
charm.land/lipgloss/v2 v2.0.3
github.com/atotto/clipboard v0.1.4
github.com/betterleaks/betterleaks v1.1.2
github.com/creack/pty v1.1.24
github.com/denisbrodbeck/machineid v1.0.1
Expand Down Expand Up @@ -40,7 +41,6 @@ require (
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
github.com/andybalholm/brotli v1.2.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bodgit/plumbing v1.3.0 // indirect
Expand Down
Loading