Skip to content

Commit

Permalink
feat: helm list, clone and delete cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Dec 18, 2023
1 parent 2e63d9d commit 05ea887
Show file tree
Hide file tree
Showing 9 changed files with 463 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/container_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var containerDeployCmd = &cobra.Command{
}

if containerName == "" && containerNames == "" {
utils.PrintlnError(fmt.Errorf("use either --cronjob \"<container name>\" or --cronjobs \"<container1 name>, <container2 name>\" but not both at the same time"))
utils.PrintlnError(fmt.Errorf("use either --container \"<container name>\" or --containers \"<container1 name>, <container2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
Expand Down
28 changes: 28 additions & 0 deletions cmd/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"
"os"
)

var helmName string
var helmNames string
var targetHelmName string

var helmCmd = &cobra.Command{
Use: "helm",
Short: "Manage helms",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

if len(args) == 0 {
_ = cmd.Help()
os.Exit(0)
}
},
}

func init() {
rootCmd.AddCommand(helmCmd)
}
116 changes: 116 additions & 0 deletions cmd/helm_clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

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

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

var helmCloneCmd = &cobra.Command{
Use: "clone",
Short: "Clone a helm",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

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

client := utils.GetQoveryClient(tokenType, token)
organizationId, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

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

helm, err := getHelmContextResource(client, helmName, envId)

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

targetProjectId := projectId // use same project as the source project
if targetProjectName != "" {

targetProjectId, err = getProjectContextResourceId(client, targetProjectName, organizationId)

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

targetEnvironmentId := envId // use same env as the source env
if targetEnvironmentName != "" {

targetEnvironmentId, err = getEnvironmentContextResourceId(client, targetEnvironmentName, targetProjectId)

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

if targetHelmName == "" {
// use same helm name as the source helm
targetHelmName = helm.Name
}

req := qovery.CloneServiceRequest{
Name: targetHelmName,
EnvironmentId: targetEnvironmentId,
}

clonedService, res, err := client.HelmsAPI.CloneHelm(context.Background(), helm.Id).CloneServiceRequest(req).Execute()

if err != nil {
// print http body error message
if res.StatusCode != 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
}

name := ""
if clonedService != nil {
name = clonedService.Name
}

utils.Println(fmt.Sprintf("Helm %s cloned!", pterm.FgBlue.Sprintf(name)))
},
}


func init() {
helmCmd.AddCommand(helmCloneCmd)
helmCloneCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmCloneCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmCloneCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
helmCloneCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name")
helmCloneCmd.Flags().StringVarP(&targetProjectName, "target-project", "", "", "Target Project Name")
helmCloneCmd.Flags().StringVarP(&targetEnvironmentName, "target-environment", "", "", "Target Environment Name")
helmCloneCmd.Flags().StringVarP(&targetHelmName, "target-helm-name", "", "", "Target Helm Name")

_ = helmCloneCmd.MarkFlagRequired("helm")
}
146 changes: 146 additions & 0 deletions cmd/helm_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package cmd

import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var helmDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a helm",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

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

if helmName == "" && helmNames == "" {
utils.PrintlnError(fmt.Errorf("use either --helm \"<helm name>\" or --helms \"<helm1 name>, <helm2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if helmName != "" && helmNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --helm and --helms at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

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

if helmNames != "" {
// wait until service is ready
for {
if utils.IsEnvironmentInATerminalState(envId, client) {
break
}

utils.Println(fmt.Sprintf("Waiting for environment %s to be ready..", pterm.FgBlue.Sprintf(envId)))
time.Sleep(5 * time.Second)
}

helms, _, err := client.HelmsAPI.ListHelms(context.Background(), envId).Execute()

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

var serviceIds []string
for _, helmName := range strings.Split(helmNames, ",") {
trimmedHelmName := strings.TrimSpace(helmName)
helm := utils.FindByHelmName(helms.GetResults(), trimmedHelmName)
if helm == nil {
utils.PrintlnError(fmt.Errorf("helm %s not found", trimmedHelmName))
utils.PrintlnInfo("You can list all helms with: qovery helm list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

serviceIds = append(serviceIds, helm.Id)
}

_, err = utils.DeleteServices(client, envId, serviceIds, utils.HelmType)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting helms %s in progress..", pterm.FgBlue.Sprintf(helmNames)))
}

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

return
}

helms, _, err := client.HelmsAPI.ListHelms(context.Background(), envId).Execute()

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

helm := utils.FindByHelmName(helms.GetResults(), helmName)

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

msg, err := utils.DeleteService(client, envId, helm.Id, utils.HelmType, watchFlag)

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

if msg != "" {
utils.PrintlnInfo(msg)
return
}

if watchFlag {
utils.Println(fmt.Sprintf("Helm %s deleted!", pterm.FgBlue.Sprintf(helmName)))
} else {
utils.Println(fmt.Sprintf("Deleting helm %s in progress..", pterm.FgBlue.Sprintf(helmName)))
}
},
}

func init() {
helmCmd.AddCommand(helmDeleteCmd)
helmDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
helmDeleteCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name")
helmDeleteCmd.Flags().StringVarP(&helmNames, "helms", "", "", "Helm Names (comma separated) (ex: --helms \"helm1,helm2\")")
helmDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch helm status until it's ready or an error occurs")
}
70 changes: 70 additions & 0 deletions cmd/helm_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"context"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"
"os"
)

var helmListCmd = &cobra.Command{
Use: "list",
Short: "List helms",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

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

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

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

helms, _, err := client.HelmsAPI.ListHelms(context.Background(), envId).Execute()

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

statuses, _, err := client.EnvironmentMainCallsAPI.GetEnvironmentStatuses(context.Background(), envId).Execute()

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

var data [][]string

for _, helm := range helms.GetResults() {
data = append(data, []string{helm.Id, helm.Name, "Helm",
utils.FindStatusTextWithColor(statuses.GetHelms(), helm.Id), helm.UpdatedAt.String()})
}

err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Last Update"}, data)

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

func init() {
helmCmd.AddCommand(helmListCmd)
helmListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
}
Loading

0 comments on commit 05ea887

Please sign in to comment.