/
container_clone.go
115 lines (92 loc) · 3.69 KB
/
container_clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 containerCloneCmd = &cobra.Command{
Use: "clone",
Short: "Clone a container",
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
}
container, err := getContainerContextResource(client, containerName, envId)
if err != nil {
utils.PrintlnError(fmt.Errorf("container %s not found", containerName))
utils.PrintlnInfo("You can list all containers with: qovery container 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 targetContainerName == "" {
// use same container name as the source container
targetContainerName = container.Name
}
req := qovery.CloneServiceRequest{
Name: targetContainerName,
EnvironmentId: targetEnvironmentId,
}
clonedService, res, err := client.ContainersAPI.CloneContainer(context.Background(), container.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("Container %s cloned!", pterm.FgBlue.Sprintf(name)))
},
}
func init() {
containerCmd.AddCommand(containerCloneCmd)
containerCloneCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
containerCloneCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
containerCloneCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
containerCloneCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerCloneCmd.Flags().StringVarP(&targetProjectName, "target-project", "", "", "Target Project Name")
containerCloneCmd.Flags().StringVarP(&targetEnvironmentName, "target-environment", "", "", "Target Environment Name")
containerCloneCmd.Flags().StringVarP(&targetContainerName, "target-container-name", "", "", "Target Container Name")
_ = containerCloneCmd.MarkFlagRequired("container")
}