mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-06-30 17:22:19 +00:00
Merge pull request #274 from matthisholleville/fix/server-authentication
fix: use the aiProvider object when launching the server
This commit is contained in:
commit
bbf159455a
@ -1,12 +1,13 @@
|
|||||||
package serve
|
package serve
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"os"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
|
||||||
k8sgptserver "github.com/k8sgpt-ai/k8sgpt/pkg/server"
|
k8sgptserver "github.com/k8sgpt-ai/k8sgpt/pkg/server"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -21,30 +22,38 @@ var ServeCmd = &cobra.Command{
|
|||||||
Long: `Runs k8sgpt as a server to allow for easy integration with other applications.`,
|
Long: `Runs k8sgpt as a server to allow for easy integration with other applications.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
backendType := viper.GetString("backend_type")
|
var configAI ai.AIConfiguration
|
||||||
if backendType == "" {
|
err := viper.UnmarshalKey("ai", &configAI)
|
||||||
color.Red("No backend set. Please run k8sgpt auth")
|
if err != nil {
|
||||||
|
color.Red("Error: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if backend != "" {
|
if len(configAI.Providers) == 0 {
|
||||||
backendType = backend
|
color.Red("Error: AI provider not specified in configuration. Please run k8sgpt auth")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
token := viper.GetString(fmt.Sprintf("%s_key", backendType))
|
var aiProvider ai.AIProvider
|
||||||
// check if nil
|
for _, provider := range configAI.Providers {
|
||||||
if token == "" {
|
if backend == provider.Name {
|
||||||
color.Red("No %s key set. Please run k8sgpt auth", backendType)
|
aiProvider = provider
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if aiProvider.Name == "" {
|
||||||
|
color.Red("Error: AI provider %s not specified in configuration. Please run k8sgpt auth", backend)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
server := k8sgptserver.Config{
|
server := k8sgptserver.Config{
|
||||||
Backend: backend,
|
Backend: aiProvider.Name,
|
||||||
Port: port,
|
Port: port,
|
||||||
Token: token,
|
Token: aiProvider.Password,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := server.Serve()
|
err = server.Serve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("Error: %v", err)
|
color.Red("Error: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user