Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refacto to split cmd and functions #379

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 4 additions & 66 deletions cmd/application_update.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/pterm/pterm"
"github.com/qovery/qovery-client-go"
"github.com/qovery/qovery-cli/pkg/application"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
Expand Down Expand Up @@ -34,73 +33,12 @@ var applicationUpdateCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), envId).Execute()

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

application := utils.FindByApplicationName(applications.GetResults(), applicationName)

if application == nil {
utils.PrintlnError(fmt.Errorf("application %s not found", applicationName))
utils.PrintlnInfo("You can list all applications with: qovery application list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var storage []qovery.ServiceStorageRequestStorageInner
for _, s := range application.Storage {
storage = append(storage, qovery.ServiceStorageRequestStorageInner{
Id: &s.Id,
Type: s.Type,
Size: s.Size,
MountPoint: s.MountPoint,
})
}

req := qovery.ApplicationEditRequest{
Storage: storage,
Name: &application.Name,
Description: application.Description,
GitRepository: &qovery.ApplicationGitRepositoryRequest{
Branch: application.GitRepository.Branch,
GitTokenId: application.GitRepository.GitTokenId,
RootPath: application.GitRepository.RootPath,
Url: application.GitRepository.Url,
},
BuildMode: application.BuildMode,
DockerfilePath: application.DockerfilePath,
BuildpackLanguage: application.BuildpackLanguage,
Cpu: application.Cpu,
Memory: application.Memory,
MinRunningInstances: application.MinRunningInstances,
MaxRunningInstances: application.MaxRunningInstances,
Healthchecks: application.Healthchecks,
AutoPreview: application.AutoPreview,
Ports: application.Ports,
Arguments: application.Arguments,
Entrypoint: application.Entrypoint,
AutoDeploy: *qovery.NewNullableBool(application.AutoDeploy),
}

if applicationBranch != "" {
req.GitRepository.Branch = &applicationBranch
}

var changeAutoDeploy = false
if cmd.Flags().Changed("auto-deploy") {
req.AutoDeploy = *qovery.NewNullableBool(&applicationAutoDeploy)
changeAutoDeploy = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iiuc you should target &applicationAutoDeploy instead of setting to true.
The condition cmd.Flags().Changed("auto-deploy") means that the flag has been set (it can be true or false).
Also, you should use a nullable variable type to be able to keep the current behavior, see https://github.com/Qovery/qovery-cli/pull/379/files#diff-347ce49b39961c56097bf8e9b5eee84236e37636a3615f43ab6197e9293bba7eR70

}

_, _, err = client.ApplicationMainCallsAPI.EditApplication(context.Background(), application.Id).ApplicationEditRequest(req).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
application.ApplicationUpdate(client, envId, applicationName, applicationBranch, applicationAutoDeploy, changeAutoDeploy)

utils.Println(fmt.Sprintf("Application %s updated!", pterm.FgBlue.Sprintf("%s", applicationName)))
},
Expand Down
57 changes: 3 additions & 54 deletions cmd/environment_clone.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package cmd

import (
"context"
"github.com/go-errors/errors"
"io"
"os"
"strings"

"github.com/qovery/qovery-cli/pkg/environment"
"github.com/qovery/qovery-cli/utils"
"github.com/qovery/qovery-client-go"
"github.com/spf13/cobra"
)

Expand All @@ -34,56 +30,9 @@ var environmentCloneCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

req := qovery.CloneEnvironmentRequest{
Name: newEnvironmentName,
ApplyDeploymentRule: &applyDeploymentRule,
}

if clusterName != "" {
clusters, _, err := client.ClustersAPI.ListOrganizationCluster(context.Background(), orgId).Execute()

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

if err == nil {
for _, c := range clusters.GetResults() {
if strings.EqualFold(c.Name, clusterName) {
req.ClusterId = &c.Id
break
}
}
}
}

if environmentType != "" {
switch strings.ToUpper(environmentType) {
case "DEVELOPMENT":
req.Mode = qovery.EnvironmentModeEnum.Ptr(qovery.ENVIRONMENTMODEENUM_DEVELOPMENT)
case "PRODUCTION":
req.Mode = qovery.EnvironmentModeEnum.Ptr(qovery.ENVIRONMENTMODEENUM_PRODUCTION)
case "STAGING":
req.Mode = qovery.EnvironmentModeEnum.Ptr(qovery.ENVIRONMENTMODEENUM_STAGING)
}
}

_, res, err := client.EnvironmentActionsAPI.CloneEnvironment(context.Background(), envId).CloneEnvironmentRequest(req).Execute()

if err != nil {
// print http body error message
if res != nil && !strings.Contains(res.Status, "200") {
result, _ := io.ReadAll(res.Body)
utils.PrintlnError(errors.Errorf("status code: %s ; body: %s", res.Status, string(result)))
}

utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
var newEnvId = environment.EnvironmentClone(client, organizationName, projectName, environmentName, newEnvironmentName, clusterName, environmentType, applyDeploymentRule, orgId, envId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable typo:

Suggested change
var newEnvId = environment.EnvironmentClone(client, organizationName, projectName, environmentName, newEnvironmentName, clusterName, environmentType, applyDeploymentRule, orgId, envId)
var newEnvironment = environment.EnvironmentClone(client, organizationName, projectName, environmentName, newEnvironmentName, clusterName, environmentType, applyDeploymentRule, orgId, envId)


utils.Println("Environment is cloned!")
utils.Println("your new environment ID is: " + newEnvId.Id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can improve the message, e.g:

Suggested change
utils.Println("your new environment ID is: " + newEnvId.Id)
utils.Println(fmt.Sprintf("Your environment has been cloned [id: %s, name: %s]", newEnv.Id, newEnv.name))

},
}

Expand Down
Loading
Loading