-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added the ability to set a user default AI provider (#427)
* feat: added the ability to set a user default AI provider Signed-off-by: Alex Jones <[email protected]> * feat: added the ability to set a user default AI provider Signed-off-by: Alex Jones <[email protected]> * chore: added provider to json output Signed-off-by: Alex Jones <[email protected]> --------- Signed-off-by: Alex Jones <[email protected]>
- Loading branch information
1 parent
032576c
commit cbe2fb4
Showing
7 changed files
with
180 additions
and
26 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
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,79 @@ | ||
/* | ||
Copyright 2023 The K8sGPT Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package auth | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
providerName string | ||
) | ||
|
||
var defaultCmd = &cobra.Command{ | ||
Use: "default", | ||
Short: "Set your default AI backend provider", | ||
Long: "The command to set your new default AI backend provider (default is openai)", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := viper.UnmarshalKey("ai", &configAI) | ||
if err != nil { | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
if providerName == "" { | ||
if configAI.DefaultProvider != "" { | ||
color.Yellow("Your default provider is %s", configAI.DefaultProvider) | ||
} else { | ||
color.Yellow("Your default provider is openai") | ||
} | ||
os.Exit(0) | ||
} | ||
// lowercase the provider name | ||
providerName = strings.ToLower(providerName) | ||
|
||
// Check if the provider is in the provider list | ||
providerExists := false | ||
for _, provider := range configAI.Providers { | ||
if provider.Name == providerName { | ||
providerExists = true | ||
} | ||
} | ||
if !providerExists { | ||
color.Red("Error: Provider %s does not exist", providerName) | ||
os.Exit(1) | ||
} | ||
// Set the default provider | ||
configAI.DefaultProvider = providerName | ||
|
||
viper.Set("ai", configAI) | ||
// Viper write config | ||
err = viper.WriteConfig() | ||
if err != nil { | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
// Print acknowledgement | ||
color.Green("Default provider set to %s", providerName) | ||
}, | ||
} | ||
|
||
func init() { | ||
// provider name flag | ||
defaultCmd.Flags().StringVarP(&providerName, "provider", "p", "", "The name of the provider to set as default") | ||
} |
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
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