HandleAPIEnablement provides an interactive flow to enable a required API
(apiErr *APIEnablementError)
| 75 | |
| 76 | // HandleAPIEnablement provides an interactive flow to enable a required API |
| 77 | func HandleAPIEnablement(apiErr *APIEnablementError) error { |
| 78 | if !isInteractiveSession() { |
| 79 | return fmt.Errorf( |
| 80 | "API '%s' must be enabled before retrying. Enable it here: %s", |
| 81 | apiErr.APITitle, |
| 82 | apiErr.ActivationURL, |
| 83 | ) |
| 84 | } |
| 85 | |
| 86 | fmt.Println() |
| 87 | fmt.Println("╔══════════════════════════════════════════════════════════════════╗") |
| 88 | fmt.Println("║ API ENABLEMENT REQUIRED ║") |
| 89 | fmt.Println("╠══════════════════════════════════════════════════════════════════╣") |
| 90 | fmt.Printf("║ API: %-58s ║\n", apiErr.APITitle) |
| 91 | fmt.Printf("║ Project: %-54s ║\n", apiErr.ProjectID) |
| 92 | fmt.Println("╚══════════════════════════════════════════════════════════════════╝") |
| 93 | fmt.Println() |
| 94 | fmt.Println("This API needs to be enabled in your Google Cloud project.") |
| 95 | fmt.Println() |
| 96 | fmt.Println("Options:") |
| 97 | fmt.Println(" [o] Open browser to enable API") |
| 98 | fmt.Println(" [c] Copy URL to clipboard") |
| 99 | fmt.Println(" [s] Show URL only") |
| 100 | fmt.Println(" [q] Quit") |
| 101 | fmt.Println() |
| 102 | fmt.Print("Choose an option [o/c/s/q]: ") |
| 103 | |
| 104 | reader := bufio.NewReader(os.Stdin) |
| 105 | input, _ := reader.ReadString('\n') |
| 106 | input = strings.TrimSpace(strings.ToLower(input)) |
| 107 | |
| 108 | switch input { |
| 109 | case "o", "open", "": |
| 110 | fmt.Println() |
| 111 | fmt.Println("Opening browser...") |
| 112 | if err := openBrowser(apiErr.ActivationURL); err != nil { |
| 113 | fmt.Printf("Could not open browser: %v\n", err) |
| 114 | fmt.Println() |
| 115 | fmt.Println("Please open this URL manually:") |
| 116 | fmt.Println(apiErr.ActivationURL) |
| 117 | } |
| 118 | return waitForEnablement() |
| 119 | |
| 120 | case "c", "copy": |
| 121 | if err := copyToClipboard(apiErr.ActivationURL); err != nil { |
| 122 | fmt.Printf("Could not copy to clipboard: %v\n", err) |
| 123 | fmt.Println() |
| 124 | fmt.Println("URL:") |
| 125 | fmt.Println(apiErr.ActivationURL) |
| 126 | } else { |
| 127 | fmt.Println() |
| 128 | fmt.Println("URL copied to clipboard!") |
| 129 | } |
| 130 | return waitForEnablement() |
| 131 | |
| 132 | case "s", "show": |
| 133 | fmt.Println() |
| 134 | fmt.Println("Enable URL:") |