Skip to content

Commit

Permalink
Auto ask for qovery context if needed (#288)
Browse files Browse the repository at this point in the history
* chore: auto ask for qovery context if needed

* chore: auto ask for qovery context if needed

* chore: auto ask for qovery context if needed

* chore: auto ask for qovery context if needed
  • Loading branch information
evoxmusic committed May 14, 2024
1 parent 3b7d5e7 commit ef7f0ea
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 117 deletions.
12 changes: 7 additions & 5 deletions cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ var consoleCmd = &cobra.Command{
Short: "Opens the application in Qovery Console in your browser",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)
organization, _, err := utils.CurrentOrganization()
organization, _, err := utils.CurrentOrganization(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
}
project, _, err := utils.CurrentProject()

project, _, err := utils.CurrentProject(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
}
environment, _, err := utils.CurrentEnvironment()

environment, _, err := utils.CurrentEnvironment(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
}
service, err := utils.CurrentService()
service, err := utils.CurrentService(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
}

url := fmt.Sprintf("https://console.qovery.com/platform/organization/%v/projects/%v/environments/%v/%vs/%v/summary", organization, project, environment, service.Type, service.ID)
url := fmt.Sprintf("https://console.qovery.com/organization/%v/project/%v/environment/%v/%v/%v/general", organization, project, environment, service.Type, service.ID)
utils.PrintlnInfo("Opening " + url)
err = browser.OpenURL(url)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var contextCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)
utils.PrintlnInfo("Current context:")
err := utils.PrintlnContext()
err := utils.PrintContext()
if err != nil {
fmt.Println("Context not yet configured. ")
}
Expand Down
38 changes: 1 addition & 37 deletions cmd/context_set.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"
)
Expand All @@ -12,41 +10,7 @@ var setCmd = &cobra.Command{
Short: "Set Qovery CLI context",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)
utils.PrintlnInfo("Current context:")
err := utils.PrintlnContext()
if err != nil {
fmt.Println("Context not yet configured. ")
}
println()
_ = utils.ResetApplicationContext()
utils.PrintlnInfo("Select new context")
orga, err := utils.SelectAndSetOrganization()
if err != nil {
utils.PrintlnError(err)
return
}

project, err := utils.SelectAndSetProject(orga.ID)
if err != nil {
utils.PrintlnError(err)
return
}

env, err := utils.SelectAndSetEnvironment(project.ID)
if err != nil {
utils.PrintlnError(err)
return
}

_, err = utils.SelectAndSetService(env.ID)
if err != nil {
utils.PrintlnError(err)
return
}
_, _ = utils.CurrentService()
println()
utils.PrintlnInfo("New context:")
err = utils.PrintlnContext()
err := utils.SetContext(true, true, true, true)
if err != nil {
utils.PrintlnError(err)
}
Expand Down
18 changes: 6 additions & 12 deletions cmd/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@ var demoCmd = &cobra.Command{
Short: "Create a demo kubernetes cluster with Qovery installed on your local machine",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
currentContext, err := utils.CurrentContext()
_, token, err := utils.GetAccessToken()
if err != nil {
log.Errorf("Qovery context is not set. Use `qovery context set` first")
os.Exit(1)
}

organizationId := string(currentContext.OrganizationId)
if organizationId == "" {
log.Errorf("Qovery context is not set. Use `qovery context set` first")
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

_, token, err := utils.GetAccessToken()
orgId, _, err := utils.CurrentOrganization(true)
if err != nil {
log.Errorf("Cannot get Bearer or Token to access Qovery API. Please use `qovery auth` first: %s", err)
utils.PrintlnError(err)
os.Exit(1)
}
Expand All @@ -52,7 +46,7 @@ var demoCmd = &cobra.Command{
os.Exit(1)
}

cmd := exec.Command("/bin/sh", "create_demo_cluster.sh", demoClusterName, strings.ToUpper(runtime.GOARCH), organizationId, string(token))
cmd := exec.Command("/bin/sh", "create_demo_cluster.sh", demoClusterName, strings.ToUpper(runtime.GOARCH), string(orgId), string(token))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand All @@ -68,7 +62,7 @@ var demoCmd = &cobra.Command{
os.Exit(1)
}

cmd := exec.Command("/bin/sh", "destroy_demo_cluster.sh", demoClusterName, organizationId, string(token), strconv.FormatBool(demoDeleteQoveryConfig))
cmd := exec.Command("/bin/sh", "destroy_demo_cluster.sh", demoClusterName, string(orgId), string(token), strconv.FormatBool(demoDeleteQoveryConfig))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/env_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var envImportCmd = &cobra.Command{
return
}

service, err := utils.CurrentService()
service, err := utils.CurrentService(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
Expand Down
10 changes: 5 additions & 5 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ var logCmd = &cobra.Command{
}

func getLogs() string {
service, err := utils.CurrentService()
service, err := utils.CurrentService(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
}
orga, _, _ := utils.CurrentOrganization()
project, _, _ := utils.CurrentProject()
env, _, _ := utils.CurrentEnvironment()
org, _, _ := utils.CurrentOrganization(true)
project, _, _ := utils.CurrentProject(true)
env, _, _ := utils.CurrentEnvironment(true)

tokenType, token, err := utils.GetAccessToken()
if err != nil {
Expand All @@ -52,7 +52,7 @@ func getLogs() string {

req := pkg.LogRequest{
ServiceID: service.ID,
OrganizationID: orga,
OrganizationID: org,
ProjectID: project,
EnvironmentID: env,
ClusterID: utils.Id(e.ClusterId),
Expand Down
12 changes: 6 additions & 6 deletions cmd/port-forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var (

func portForwardRequestWithoutArg() (*pkg.PortForwardRequest, error) {
useContext := false
currentContext, err := utils.CurrentContext()
currentContext, err := utils.GetCurrentContext()
if err != nil {
return nil, err
}
Expand All @@ -89,7 +89,7 @@ func portForwardRequestWithoutArg() (*pkg.PortForwardRequest, error) {
currentContext.EnvironmentId != "" && currentContext.EnvironmentName != "" &&
currentContext.ProjectId != "" && currentContext.ProjectName != "" &&
currentContext.OrganizationId != "" && currentContext.OrganizationName != "" {
if err := utils.PrintlnContext(); err != nil {
if err := utils.PrintContext(); err != nil {
fmt.Println("Context not yet configured.")
}
fmt.Println()
Expand All @@ -98,7 +98,7 @@ func portForwardRequestWithoutArg() (*pkg.PortForwardRequest, error) {
useContext = utils.Validate("context")
fmt.Println()
} else {
if err := utils.PrintlnContext(); err != nil {
if err := utils.PrintContext(); err != nil {
fmt.Println("Context not yet configured.")
fmt.Println("Unable to use current context for `port-forward` command.")
fmt.Println()
Expand All @@ -120,13 +120,13 @@ func portForwardRequestWithoutArg() (*pkg.PortForwardRequest, error) {

func portForwardRequestFromSelect() (*pkg.PortForwardRequest, error) {
utils.PrintlnInfo("Select organization")
orga, err := utils.SelectOrganization()
org, err := utils.SelectOrganization()
if err != nil {
return nil, err
}

utils.PrintlnInfo("Select project")
project, err := utils.SelectProject(orga.ID)
project, err := utils.SelectProject(org.ID)
if err != nil {
return nil, err
}
Expand All @@ -147,7 +147,7 @@ func portForwardRequestFromSelect() (*pkg.PortForwardRequest, error) {
ServiceID: service.ID,
ServiceType: strings.ToUpper(string(service.Type)),
ProjectID: project.ID,
OrganizationID: orga.ID,
OrganizationID: org.ID,
EnvironmentID: env.ID,
ClusterID: env.ClusterID,
PodName: podName,
Expand Down
4 changes: 2 additions & 2 deletions cmd/project_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ var projectListCmd = &cobra.Command{
}

client := utils.GetQoveryClient(tokenType, token)
organizationID, err := getOrganizationContextResourceId(client, organizationName)
organizationId, err := getOrganizationContextResourceId(client, organizationName)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

projects, _, err := client.ProjectsAPI.ListProject(context.Background(), organizationID).Execute()
projects, _, err := client.ProjectsAPI.ListProject(context.Background(), organizationId).Execute()

if err != nil {
utils.PrintlnError(err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func getOrganizationProjectContextResourcesIds(qoveryAPIClient *qovery.APIClient

func getOrganizationContextResourceId(qoveryAPIClient *qovery.APIClient, organizationName string) (string, error) {
if strings.TrimSpace(organizationName) == "" {
id, _, err := utils.CurrentOrganization()
id, _, err := utils.CurrentOrganization(true)
if err != nil {
return "", err
}
Expand All @@ -212,7 +212,7 @@ func getOrganizationContextResourceId(qoveryAPIClient *qovery.APIClient, organiz

func getProjectContextResourceId(qoveryAPIClient *qovery.APIClient, projectName string, organizationId string) (string, error) {
if strings.TrimSpace(projectName) == "" {
id, _, err := utils.CurrentProject()
id, _, err := utils.CurrentProject(true)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func getProjectContextResourceId(qoveryAPIClient *qovery.APIClient, projectName

func getEnvironmentContextResourceId(qoveryAPIClient *qovery.APIClient, environmentName string, projectId string) (string, error) {
if strings.TrimSpace(environmentName) == "" {
id, _, err := utils.CurrentEnvironment()
id, _, err := utils.CurrentEnvironment(true)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (

func shellRequestWithoutArg() (*pkg.ShellRequest, error) {
useContext := false
currentContext, err := utils.CurrentContext()
currentContext, err := utils.GetCurrentContext()
if err != nil {
return nil, err
}
Expand All @@ -53,7 +53,7 @@ func shellRequestWithoutArg() (*pkg.ShellRequest, error) {
currentContext.EnvironmentId != "" && currentContext.EnvironmentName != "" &&
currentContext.ProjectId != "" && currentContext.ProjectName != "" &&
currentContext.OrganizationId != "" && currentContext.OrganizationName != "" {
if err := utils.PrintlnContext(); err != nil {
if err := utils.PrintContext(); err != nil {
fmt.Println("Context not yet configured.")
}
fmt.Println()
Expand All @@ -62,7 +62,7 @@ func shellRequestWithoutArg() (*pkg.ShellRequest, error) {
useContext = utils.Validate("context")
fmt.Println()
} else {
if err := utils.PrintlnContext(); err != nil {
if err := utils.PrintContext(); err != nil {
fmt.Println("Context not yet configured.")
fmt.Println("Unable to use current context for `shell` command.")
fmt.Println()
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var statusCmd = &cobra.Command{
utils.PrintlnError(err)
os.Exit(0)
}
service, err := utils.CurrentService()
service, err := utils.CurrentService(true)
if err != nil {
utils.PrintlnError(err)
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion pkg/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func GetCurrentVersion() string {
return "0.92.4" // ci-version-check
return "0.92.5" // ci-version-check
}

func GetLatestOnlineVersionUrl() (string, error) {
Expand Down
Loading

0 comments on commit ef7f0ea

Please sign in to comment.