mirror of
https://github.com/cline/cline.git
synced 2026-09-19 02:05:44 +08:00
* super sketchy big merge with main * gitignore * gitignore * Delete cli/bin/air * Delete cli/bin directory * Delete cli/cline-host * Fix missing package.json in cli Copy the package JSON into the dist-standalone dir during compilation. Remove workaround for missing package.json * Update scripts/build-cli.sh Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * Remove reference to watchservice, it has been removed * Update scripts/build-go-proto.mjs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix timestamp to string conversion * diff.go ellipsis fix * COMMON_TYPES --------- Co-authored-by: Sarah Fortune <sarah.fortune@gmail.com> Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
126 lines
2.6 KiB
Go
126 lines
2.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/cline/cli/pkg/cli/global"
|
|
"github.com/cline/grpc-go/cline"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var isSessionAuthenticated bool
|
|
|
|
func NewAuthCommand() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "auth",
|
|
Short: "Sign in to Cline",
|
|
Long: `Complete the authentication flow in browser to sign in to Cline.`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return handleAuthCommand(cmd.Context())
|
|
},
|
|
}
|
|
}
|
|
|
|
func handleAuthCommand(ctx context.Context) error {
|
|
fmt.Print("Authenticating with Cline...\n")
|
|
if IsAuthenticated(ctx) {
|
|
return signOutDialog(ctx)
|
|
}
|
|
|
|
if err := signIn(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println("You are signed in!")
|
|
return nil
|
|
}
|
|
|
|
func signOut(ctx context.Context) error {
|
|
client, err := global.GetDefaultClient(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = client.Account.AccountLogoutClicked(ctx, &cline.EmptyRequest{}); err != nil {
|
|
return err
|
|
}
|
|
|
|
isSessionAuthenticated = false
|
|
fmt.Println("You have been signed out of Cline.")
|
|
return nil
|
|
}
|
|
|
|
func signOutDialog(ctx context.Context) error {
|
|
fmt.Print("You are already signed in to Cline.\nWould you like to sign out? (y/N): ")
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
if !scanner.Scan() {
|
|
return nil
|
|
}
|
|
|
|
response := strings.ToLower(strings.TrimSpace(scanner.Text()))
|
|
if response == "y" || response == "yes" {
|
|
if err := signOut(ctx); err != nil {
|
|
fmt.Printf("Failed to sign out: %v\n", err)
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func signIn(ctx context.Context) error {
|
|
if IsAuthenticated(ctx) {
|
|
return nil
|
|
}
|
|
|
|
verboseLog("Ensuring default instance exists...")
|
|
if err := ensureDefaultInstance(ctx); err != nil {
|
|
verboseLog("Failed to ensure default instance: %v", err)
|
|
return err
|
|
}
|
|
|
|
verboseLog("Default instance ensured successfully.")
|
|
time.Sleep(2 * time.Second) // Allow services to start
|
|
|
|
client, err := global.GetDefaultClient(ctx)
|
|
if err != nil {
|
|
verboseLog("Failed to obtain client: %v", err)
|
|
return err
|
|
}
|
|
|
|
_, err = client.Account.AccountLoginClicked(ctx, &cline.EmptyRequest{})
|
|
if err != nil {
|
|
verboseLog("Failed to login: %v", err)
|
|
return err
|
|
}
|
|
|
|
isSessionAuthenticated = true
|
|
verboseLog("Login successful")
|
|
return nil
|
|
}
|
|
|
|
func IsAuthenticated(ctx context.Context) bool {
|
|
if isSessionAuthenticated {
|
|
return true
|
|
}
|
|
|
|
client, err := global.GetDefaultClient(ctx)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
_, err = client.Account.GetUserCredits(ctx, &cline.EmptyRequest{})
|
|
return err == nil
|
|
}
|
|
|
|
func verboseLog(format string, args ...interface{}) {
|
|
if global.Config != nil && global.Config.Verbose {
|
|
fmt.Printf("[VERBOSE] "+format+"\n", args...)
|
|
}
|
|
}
|