From 5cb7882a4b333ac21743fce149b2facb841030d7 Mon Sep 17 00:00:00 2001 From: FFFergie Date: Sun, 15 Feb 2026 02:48:33 +0800 Subject: [PATCH] feat: improve setup with optional key and verification --- cmd/setup.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/cmd/setup.go b/cmd/setup.go index e108a08..9d3a0f5 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -8,6 +8,7 @@ import ( "runtime" "strings" + "github.com/ashrafali/craft-cli/internal/api" "github.com/spf13/cobra" ) @@ -129,11 +130,21 @@ func runSetup() error { fmt.Printf(" Using name: %s\n", profileName) } + fmt.Println() + fmt.Print("Optional API key (press Enter to skip): ") + setupAPIKey, err := reader.ReadString('\n') + if err != nil { + return fmt.Errorf("failed to read input: %w", err) + } + setupAPIKey = strings.TrimSpace(setupAPIKey) + // Save profile - if err := cfgManager.AddProfile(profileName, apiURL); err != nil { + if err := cfgManager.AddProfileWithKey(profileName, apiURL, setupAPIKey); err != nil { return fmt.Errorf("failed to save profile: %w", err) } + printSetupVerification(apiURL, setupAPIKey) + // Success! printSuccess(profileName) @@ -192,6 +203,32 @@ func printSuccess(profileName string) { fmt.Println() } +func printSetupVerification(apiURL, apiKey string) { + fmt.Println() + fmt.Println("Verifying connection and permissions...") + fmt.Println() + + client := api.NewClient(apiURL) + if apiKey != "" { + client = api.NewClientWithKey(apiURL, apiKey) + } + + if _, err := client.GetConnection(); err != nil { + fmt.Printf("✗ Connectivity: Failed (%v)\n", err) + } else { + fmt.Println("✓ Connectivity: OK") + } + + if _, err := client.GetDocuments(); err != nil { + fmt.Printf("✗ Read: Denied (%v)\n", err) + } else { + fmt.Println("✓ Read: Allowed") + } + + fmt.Println(" Write: Use 'craft create --dry-run' to test") + fmt.Println(" Delete: Use 'craft delete --dry-run' to test") +} + func promptYesNo(reader *bufio.Reader, question string, defaultYes bool) bool { defaultHint := "Y/n" if !defaultYes {