forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_cmd.go
81 lines (68 loc) · 2.3 KB
/
delete_cmd.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
package cmd
import (
"errors"
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/system"
biui "github.com/cloudfoundry/bosh-init/ui"
)
type deleteCmd struct {
deploymentDeleterProvider func(deploymentManifestString string) (DeploymentDeleter, error)
ui biui.UI
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
func NewDeleteCmd(
ui biui.UI,
fs boshsys.FileSystem,
logger boshlog.Logger,
deploymentDeleterProvider func(deploymentManifestString string) (DeploymentDeleter, error),
) Cmd {
return &deleteCmd{
ui: ui,
fs: fs,
deploymentDeleterProvider: deploymentDeleterProvider,
logger: logger,
logTag: "deleteCmd",
}
}
func (c *deleteCmd) Name() string {
return "delete"
}
func (c *deleteCmd) Meta() Meta {
return Meta{
Synopsis: "Delete existing deployment",
Usage: "<deployment_manifest_path>",
Env: genericEnv,
}
}
func (c *deleteCmd) Run(stage biui.Stage, args []string) error {
deploymentManifestPath, err := c.parseCmdInputs(args)
if err != nil {
return err
}
manifestAbsFilePath, err := filepath.Abs(deploymentManifestPath)
if err != nil {
c.ui.ErrorLinef("Failed getting absolute path to deployment file '%s'", deploymentManifestPath)
return bosherr.WrapErrorf(err, "Getting absolute path to deployment file '%s'", deploymentManifestPath)
}
if !c.fs.FileExists(manifestAbsFilePath) {
c.ui.ErrorLinef("Deployment '%s' does not exist", manifestAbsFilePath)
return bosherr.Errorf("Deployment manifest does not exist at '%s'", manifestAbsFilePath)
}
c.ui.PrintLinef("Deployment manifest: '%s'", manifestAbsFilePath)
deploymentDeleter, err := c.deploymentDeleterProvider(manifestAbsFilePath)
if err != nil {
return err
}
return deploymentDeleter.DeleteDeployment(stage)
}
func (c *deleteCmd) parseCmdInputs(args []string) (string, error) {
if len(args) != 1 {
c.logger.Error(c.logTag, "Invalid arguments: %#v", args)
return "", errors.New("Invalid usage - delete command requires exactly 1 argument")
}
return args[0], nil
}