forked from k8sgpt-ai/k8sgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add generation of api-keys to cli (k8sgpt-ai#87)
* feat: add generate option in cli and the documentation Signed-off-by: Thomas Schuetz <[email protected]> * chore: release 0.1.0 Release-As: 0.1.0 Signed-off-by: Thomas Schuetz <[email protected]> * fix: apply suggestions from code review Co-authored-by: Alex Jones <[email protected]> Signed-off-by: Thomas Schuetz <[email protected]> --------- Signed-off-by: Thomas Schuetz <[email protected]> Co-authored-by: Alex Jones <[email protected]>
- Loading branch information
1 parent
9c5523f
commit bb2db5c
Showing
3 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package generate | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"os/exec" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
var ( | ||
backend string | ||
) | ||
|
||
// generateCmd represents the auth command | ||
var GenerateCmd = &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate Key for your chosen backend (opens browser)", | ||
Long: `Opens your browser to generate a key for your chosen backend.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
backendType := viper.GetString("backend_type") | ||
if backendType == "" { | ||
// Set the default backend | ||
backend = "openai" | ||
} | ||
// override the default backend if a flag is provided | ||
if backend != "" { | ||
backendType = backend | ||
} | ||
fmt.Println("") | ||
color.Green("Opening: https://beta.openai.com/account/api-keys to generate a key for %s", backendType) | ||
color.Green("Please copy the generated key and run `k8sgpt auth` to add it to your config file") | ||
fmt.Println("") | ||
time.Sleep(5 * time.Second) | ||
openbrowser("https://beta.openai.com/account/api-keys") | ||
}, | ||
} | ||
|
||
func init() { | ||
// add flag for backend | ||
GenerateCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") | ||
} | ||
|
||
func openbrowser(url string) { | ||
var err error | ||
switch runtime.GOOS { | ||
case "linux": | ||
err = exec.Command("xdg-open", url).Start() | ||
case "windows": | ||
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() | ||
case "darwin": | ||
err = exec.Command("open", url).Start() | ||
default: | ||
err = fmt.Errorf("unsupported platform") | ||
} | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters