-
Notifications
You must be signed in to change notification settings - Fork 4
/
compose_remove.go
94 lines (79 loc) · 3.11 KB
/
compose_remove.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
package subcommands
import (
"fmt"
"os"
"path"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func CreateComposeRemoveCommand() *cobra.Command {
var networkFlag string
var serviceFlag string
composeDeleteCmd := &cobra.Command{
Use: "remove",
Short: "Remove services or networks in docker-compose.yml",
Aliases: []string{"rm"},
Run: func(cmd *cobra.Command, args []string) {
workingDir, getWdError := os.Getwd()
if getWdError != nil {
fmt.Println(text.FgRed.Sprint("oops! could not get current working directory."))
os.Exit(1)
}
dockerComposePath := path.Join(workingDir, "docker-compose.yml")
dockerComposeFileInfo, statComposeError := os.Stat(dockerComposePath)
if statComposeError != nil {
fmt.Println(text.FgRed.Sprint("for some reason, it failed to read docker-compose.yml file."))
os.Exit(1)
}
dockerComposeFileContents, readComposeError := os.ReadFile(dockerComposePath)
if readComposeError != nil {
fmt.Println(text.FgRed.Sprint("for some reason, it failed to read docker-compose.yml file."))
os.Exit(1)
}
var composeYml DockerCompose
yamlParseError := yaml.Unmarshal(dockerComposeFileContents, &composeYml)
if yamlParseError != nil {
fmt.Println(text.FgRed.Sprint("can't manage docker-compose.yml, the contents of the file are invalid."))
os.Exit(1)
}
newCompose, err := remove(&composeYml, serviceFlag, networkFlag)
if err != nil {
fmt.Println(text.FgRed.Sprint(err))
return
}
newComposeYmlAsBytes, marshalError := yaml.Marshal(newCompose)
if marshalError != nil {
fmt.Println(text.FgRed.Sprint("can't manage docker-compose.yml, the contents of the file are invalid."))
return
}
os.WriteFile(dockerComposePath, newComposeYmlAsBytes, dockerComposeFileInfo.Mode())
fmt.Println(text.FgGreen.Sprint("Removed succesfully!"))
},
}
composeDeleteCmd.Flags().StringVarP(&networkFlag, "network", "n", "", "Specify the network name to remove")
composeDeleteCmd.Flags().StringVarP(&serviceFlag, "service", "s", "", "Speficy the service name to remove")
return composeDeleteCmd
}
func remove(compose *DockerCompose, serviceName string, networkName string) (*DockerCompose, error) {
newCompose := compose
if serviceName != "" {
if len(newCompose.Services) == 0 {
return nil, fmt.Errorf("Cannot remove '%s'. Seems like you have no service defined yet. If would like to create one consider use 'gorvus compose add' command", serviceName)
}
if _, ok := newCompose.Services[serviceName]; !ok {
return nil, fmt.Errorf("Cannot remove '%s'. This service doens't exists", serviceName)
}
delete(compose.Services, serviceName)
}
if networkName != "" {
if len(newCompose.Networks) == 0 {
return nil, fmt.Errorf("Cannot remove %s. Seems like you have no network defined yet. If would like to create one consider use 'gorvus compose add' command", networkName)
}
if _, ok := newCompose.Networks[networkName]; !ok {
return nil, fmt.Errorf("Cannot remove '%s'. This network doens't exists", networkName)
}
delete(compose.Networks, networkName)
}
return newCompose, nil
}