resolved review concerns

This commit is contained in:
quinton11
2023-04-19 11:29:04 +00:00
parent be2cf54d6e
commit c1b97841cf
4 changed files with 59 additions and 33 deletions
+16 -16
View File
@@ -54,15 +54,6 @@ var loginCmd = &cobra.Command{
util.HandleError(err) util.HandleError(err)
} }
// addUser := false
// if currentLoggedInUserDetails.UserCredentials.Email != "" {
// addUser, err = addNewUserPrompt()
// if err != nil {
// util.HandleError(err)
// }
// }
// if !addUser {
if currentLoggedInUserDetails.IsUserLoggedIn && !currentLoggedInUserDetails.LoginExpired && len(currentLoggedInUserDetails.UserCredentials.PrivateKey) != 0 { if currentLoggedInUserDetails.IsUserLoggedIn && !currentLoggedInUserDetails.LoginExpired && len(currentLoggedInUserDetails.UserCredentials.PrivateKey) != 0 {
shouldOverride, err := userLoginMenu(currentLoggedInUserDetails.UserCredentials.Email) shouldOverride, err := userLoginMenu(currentLoggedInUserDetails.UserCredentials.Email)
if err != nil { if err != nil {
@@ -73,7 +64,6 @@ var loginCmd = &cobra.Command{
return return
} }
} }
// }
//prompt user to select domain between Infisical cloud and self hosting //prompt user to select domain between Infisical cloud and self hosting
err = askForDomain() err = askForDomain()
@@ -276,19 +266,24 @@ func init() {
func askForDomain() error { func askForDomain() error {
//query user to choose between Infisical cloud or self hosting //query user to choose between Infisical cloud or self hosting
options := []string{"Infisical Cloud", "Self Hosting"} var (
INFISICAL_CLOUD = "Infisical Cloud"
SELF_HOSTING = "Self Hosting"
)
options := []string{INFISICAL_CLOUD, SELF_HOSTING}
optionsPrompt := promptui.Select{ optionsPrompt := promptui.Select{
Label: "Select your hosting option", Label: "Select your hosting option",
Items: options, Items: options,
Size: 2, Size: 2,
} }
idx, _, err := optionsPrompt.Run() _, selectedHostingOption, err := optionsPrompt.Run()
if err != nil { if err != nil {
return err return err
} }
if idx == 0 { if selectedHostingOption == INFISICAL_CLOUD {
//cloud option //cloud option
config.INFISICAL_URL = util.INFISICAL_DEFAULT_API_URL config.INFISICAL_URL = util.INFISICAL_DEFAULT_API_URL
return nil return nil
@@ -302,10 +297,10 @@ func askForDomain() error {
return nil return nil
} }
//else run prompt to enter domain
domainPrompt := promptui.Prompt{ domainPrompt := promptui.Prompt{
Label: "Domain", Label: "Domain",
Validate: urlValidation, Validate: urlValidation,
Default: "Example - https://my-domain-example.com",
} }
domain, err := domainPrompt.Run() domain, err := domainPrompt.Run()
@@ -314,7 +309,7 @@ func askForDomain() error {
} }
//set api url //set api url
config.INFISICAL_URL = domain config.INFISICAL_URL = fmt.Sprintf("%s/api", domain)
//return nil //return nil
return nil return nil
} }
@@ -410,8 +405,13 @@ func getFreshUserCredentials(email string, password string) (*api.GetLoginOneV2R
} }
func userLoginMenu(currentLoggedInUserEmail string) (bool, error) { func userLoginMenu(currentLoggedInUserEmail string) (bool, error) {
label := fmt.Sprintf("Current logged in user email: %s", currentLoggedInUserEmail)
if config.INFISICAL_URL != "" {
label = fmt.Sprintf("%s on domain: %s", label, config.INFISICAL_URL)
}
prompt := promptui.Select{ prompt := promptui.Select{
Label: fmt.Sprintf("Current logged in user email: %s", currentLoggedInUserEmail), Label: label,
Items: []string{ADD_USER, REPLACE_USER, EXIT_USER_MENU}, Items: []string{ADD_USER, REPLACE_USER, EXIT_USER_MENU},
} }
_, result, err := prompt.Run() _, result, err := prompt.Run()
+29 -8
View File
@@ -2,6 +2,7 @@ package cmd
import ( import (
"errors" "errors"
"fmt"
"net/url" "net/url"
"github.com/Infisical/infisical-merge/packages/config" "github.com/Infisical/infisical-merge/packages/config"
@@ -79,11 +80,20 @@ var switchCmd = &cobra.Command{
}, },
} }
var updateCmd = &cobra.Command{
Use: "update",
Short: "Used to update properties of an Infisical profile",
DisableFlagsInUseLine: true,
Example: "infisical user update",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {},
}
var domainCmd = &cobra.Command{ var domainCmd = &cobra.Command{
Use: "domain", Use: "domain",
Short: "Used to update the domain of an Infisical profile", Short: "Used to update the domain of an Infisical profile",
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Example: "infisical user domain", Example: "infisical user update domain",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
util.RequireLogin() util.RequireLogin()
@@ -92,25 +102,25 @@ var domainCmd = &cobra.Command{
//prompt for profiles selection //prompt for profiles selection
loggedInProfiles, err := getLoggedInUsers() loggedInProfiles, err := getLoggedInUsers()
if err != nil { if err != nil {
util.HandleError(err, "[infisical user domain]: Unable to get logged Profiles") util.HandleError(err, "[infisical user update domain]: Unable to get logged Profiles")
} }
//prompt user //prompt user
profile, err := LoggedInUsersPrompt(loggedInProfiles) profile, err := LoggedInUsersPrompt(loggedInProfiles)
if err != nil { if err != nil {
util.HandleError(err, "[infisical user domain]: Prompt error") util.HandleError(err, "[infisical user update domain]: Prompt error")
} }
//prompt to update domain //prompt to update domain
domain, err := NewDomainPrompt() domain, err := NewDomainPrompt()
if err != nil { if err != nil {
util.HandleError(err, "[infisical user domain]: Prompt error") util.HandleError(err, "[infisical user update domain]: Prompt error")
} }
//write to config file //write to config file
configFile, err := util.GetConfigFile() configFile, err := util.GetConfigFile()
if err != nil { if err != nil {
util.HandleError(err, "[infisical user]: Unable to get config file") util.HandleError(err, "[infisical user update domain]: Unable to get config file")
} }
//check if profile in logged in profiles //check if profile in logged in profiles
@@ -125,9 +135,9 @@ var domainCmd = &cobra.Command{
}) })
} else { } else {
//exists, set logged in user domain //exists, set logged in user domain
for _, v := range configFile.LoggedInUsers { for idx, v := range configFile.LoggedInUsers {
if profile == v.Email { if profile == v.Email {
v.Domain = domain configFile.LoggedInUsers[idx].Domain = domain //inplace
break break
} }
} }
@@ -139,15 +149,23 @@ var domainCmd = &cobra.Command{
configFile.LoggedInUserDomain = domain configFile.LoggedInUserDomain = domain
} }
err = util.WriteConfigFile(&configFile)
if err != nil {
util.HandleError(err, "")
}
}, },
} }
func init() { func init() {
userCmd.AddCommand(domainCmd) updateCmd.AddCommand(domainCmd)
userCmd.AddCommand(updateCmd)
userCmd.AddCommand(switchCmd) userCmd.AddCommand(switchCmd)
rootCmd.AddCommand(userCmd) rootCmd.AddCommand(userCmd)
} }
// This returns all logged in user emails from the config file.
// If none, it returns the current logged in user in a slice
func getLoggedInUsers() ([]string, error) { func getLoggedInUsers() ([]string, error) {
loggedInProfiles := []string{} loggedInProfiles := []string{}
@@ -187,6 +205,7 @@ func NewDomainPrompt() (string, error) {
domainPrompt := promptui.Prompt{ domainPrompt := promptui.Prompt{
Label: "New Domain", Label: "New Domain",
Validate: urlValidation, Validate: urlValidation,
Default: "Example - https://my-domain-example.com",
} }
domain, err := domainPrompt.Run() domain, err := domainPrompt.Run()
@@ -194,6 +213,8 @@ func NewDomainPrompt() (string, error) {
return "", err return "", err
} }
domain = fmt.Sprintf("%s/api", domain)
return domain, nil return domain, nil
} }
+10 -6
View File
@@ -37,13 +37,17 @@ func WriteInitalConfig(userCredentials *models.UserCredentials) error {
Email: userCredentials.Email, Email: userCredentials.Email,
Domain: config.INFISICAL_URL, Domain: config.INFISICAL_URL,
} }
if len(existingConfigFile.LoggedInUsers) > 0 { //if empty or if email not in loggedinUsers
ok := ConfigContainsEmail(existingConfigFile.LoggedInUsers, userCredentials.Email) if len(existingConfigFile.LoggedInUsers) == 0 || !ConfigContainsEmail(existingConfigFile.LoggedInUsers, userCredentials.Email) {
if !ok {
existingConfigFile.LoggedInUsers = append(existingConfigFile.LoggedInUsers, loggedInUser)
}
} else {
existingConfigFile.LoggedInUsers = append(existingConfigFile.LoggedInUsers, loggedInUser) existingConfigFile.LoggedInUsers = append(existingConfigFile.LoggedInUsers, loggedInUser)
} else {
//if exists update domain of loggedin users
for idx, user := range existingConfigFile.LoggedInUsers {
if user.Email == userCredentials.Email {
existingConfigFile.LoggedInUsers[idx] = loggedInUser
}
}
} }
configFile := models.ConfigFile{ configFile := models.ConfigFile{
+4 -3
View File
@@ -63,9 +63,10 @@ func IsSecretTypeValid(s string) bool {
return false return false
} }
func ConfigContainsEmail(iter []models.LoggedInUser, elem string) bool { // Checks if the passed in email already exists in the users slice
for _, value := range iter { func ConfigContainsEmail(users []models.LoggedInUser, email string) bool {
if value.Email == elem { for _, value := range users {
if value.Email == email {
return true return true
} }
} }