central logging and 2v service token

This commit is contained in:
Maidul Islam
2023-01-05 16:53:54 -05:00
parent 2c8c7a1777
commit 68b99b9f00
17 changed files with 299 additions and 605 deletions

View File

@@ -5,10 +5,13 @@ package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/Infisical/infisical-merge/packages/api"
"github.com/Infisical/infisical-merge/packages/models"
"github.com/Infisical/infisical-merge/packages/util"
"github.com/go-resty/resty/v2"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@@ -21,21 +24,10 @@ var initCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Example: "infisical init",
Args: cobra.ExactArgs(0),
PreRun: toggleDebug,
PreRun: func(cmd *cobra.Command, args []string) {
util.RequireLogin()
},
Run: func(cmd *cobra.Command, args []string) {
// check if user is logged
hasUserLoggedInbefore, loggedInUserEmail, err := util.IsUserLoggedIn()
if err != nil {
log.Info("Unexpected issue occurred while checking login status. To see more details, add flag --debug")
log.Debugln(err)
return
}
if !hasUserLoggedInbefore {
log.Infoln("No logged in user. To login, please run command [infisical login]")
return
}
if util.WorkspaceConfigFileExistsInCurrentPath() {
shouldOverride, err := shouldOverrideWorkspacePrompt()
if err != nil {
@@ -49,23 +41,22 @@ var initCmd = &cobra.Command{
}
}
userCreds, err := util.GetUserCredsFromKeyRing(loggedInUserEmail)
userCreds, err := util.GetCurrentLoggedInUserDetails()
if err != nil {
log.Infoln("Unable to get user creds from key ring")
log.Debug(err)
return
util.HandleError(err, "Unable to get your login details")
}
workspaces, err := util.GetWorkSpacesFromAPI(userCreds)
httpClient := resty.New()
httpClient.SetAuthToken(userCreds.UserCredentials.JTWToken)
workspaceResponse, err := api.CallGetAllWorkSpacesUserBelongsTo(httpClient)
if err != nil {
log.Errorln("Unable to pull your projects. To see more logs add the --debug flag to this command")
log.Debugln("Unable to get your projects because:", err)
return
util.HandleError(err, "Unable to pull projects that belong to you")
}
workspaces := workspaceResponse.Workspaces
if len(workspaces) == 0 {
log.Infoln("You don't have any projects created in Infisical. You must first create a project at https://infisical.com")
return
message := fmt.Sprintf("You don't have any projects created in Infisical. You must first create a project at %s", util.INFISICAL_TOKEN_NAME)
util.PrintMessageAndExit(message)
}
var workspaceNames []string
@@ -81,16 +72,12 @@ var initCmd = &cobra.Command{
index, _, err := prompt.Run()
if err != nil {
log.Errorln("Unable to parse your response")
log.Debug(err)
return
util.HandleError(err)
}
err = writeWorkspaceFile(workspaces[index])
if err != nil {
log.Errorln("Something went wrong when creating your workspace file")
log.Debug("Error while writing your workspace file:", err)
return
util.HandleError(err)
}
},
}