fix(run): handle case where we require a login

This commit is contained in:
Mahyar Mirrashed
2025-03-13 11:43:00 -07:00
parent fdfc020efc
commit aecfa268ae
+23 -4
View File
@@ -138,7 +138,7 @@ var runCmd = &cobra.Command{
log.Debug().Msgf("Confirming selected environment is valid: %s", environmentName) log.Debug().Msgf("Confirming selected environment is valid: %s", environmentName)
hasEnvironment, err := confirmProjectHasEnvironment(projectId, environmentName, token.Token) hasEnvironment, err := confirmProjectHasEnvironment(projectId, environmentName, token)
if err != nil { if err != nil {
util.HandleError(err, "Could not confirm project has environment") util.HandleError(err, "Could not confirm project has environment")
} }
@@ -449,13 +449,32 @@ func executeCommandWithWatchMode(commandFlag string, args []string, watchModeInt
} }
} }
func confirmProjectHasEnvironment(environmentName, workspaceId, accessToken string) (bool, error) { func confirmProjectHasEnvironment(environmentName, workspaceId string, token *models.TokenDetails) (bool, error) {
res, err := util.GetProjectDetails(accessToken, workspaceId) var accessToken string
if token != nil && (token.Type == util.SERVICE_TOKEN_IDENTIFIER || token.Type == util.UNIVERSAL_AUTH_TOKEN_IDENTIFIER) {
accessToken = token.Token
} else {
util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil {
util.HandleError(err, "Unable to authenticate")
}
if loggedInUserDetails.LoginExpired {
util.PrintErrorMessageAndExit("Your login session has expired, please run [infisical login] and try again")
}
accessToken = loggedInUserDetails.UserCredentials.JTWToken
}
project, err := util.GetProjectDetails(accessToken, workspaceId)
if err != nil { if err != nil {
return false, err return false, err
} }
for _, env := range res.Environments { for _, env := range project.Environments {
if env.Name == environmentName { if env.Name == environmentName {
return true, nil return true, nil
} }