-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclean.go
129 lines (108 loc) · 2.79 KB
/
clean.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"errors"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/spf13/cobra"
"github.com/tarantool/tt/cli/cmd/internal"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/process_utils"
"github.com/tarantool/tt/cli/running"
"github.com/tarantool/tt/cli/util"
)
var forceRemove bool
var ErrCanceledByUser = errors.New("canceled by user")
// NewCleanCmd creates clean command.
func NewCleanCmd() *cobra.Command {
var cleanCmd = &cobra.Command{
Use: "clean [INSTANCE_NAME]",
Short: "Clean instance(s) files",
Run: TtModuleCmdRun(internalCleanModule),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
return internal.ValidArgsFunction(
cliOpts, &cmdCtx, cmd, toComplete,
running.ExtractAppNames,
running.ExtractInstanceNames)
},
}
cleanCmd.Flags().BoolVarP(&forceRemove, "force", "f", false, "do not ask for confirmation")
return cleanCmd
}
func collectFiles(files map[string]bool, dirname string) (map[string]bool, error) {
err := filepath.Walk(dirname,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files[path] = true
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
func clean(run *running.InstanceCtx) error {
removeFiles := map[string]bool{}
confirm := false
var err error
for _, dir := range [...]string{run.LogDir, run.WalDir, run.VinylDir, run.MemtxDir} {
removeFiles, err = collectFiles(removeFiles, dir)
if err != nil {
return err
}
}
if !forceRemove {
confirm, err = util.AskConfirm(os.Stdin, "\nConfirm")
if err != nil {
return err
}
}
if confirm || forceRemove {
for file := range removeFiles {
err = os.Remove(file)
if err != nil {
return err
}
log.Debugf("removed %q", file)
}
return nil
}
return ErrCanceledByUser
}
// internalCleanModule is a default clean module.
func internalCleanModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
if !isConfigExist(cmdCtx) {
return errNoConfig
}
var runningCtx running.RunningCtx
err := running.FillCtx(cliOpts, cmdCtx, &runningCtx, args, running.ConfigLoadCluster)
if err != nil {
return err
}
for _, run := range runningCtx.Instances {
status := running.Status(&run)
if status.Code == process_utils.ProcessStoppedCode {
var statusMsg string
err := clean(&run)
if errors.Is(err, ErrCanceledByUser) {
statusMsg = ErrCanceledByUser.Error()
} else if err != nil {
statusMsg = "[ERR] " + err.Error()
} else {
statusMsg = "[OK]"
}
log.Infof("%s%c%s...\t%s", run.AppName, running.InstanceDelimiter, run.InstName,
statusMsg)
} else {
log.Infof("instance `%s` must be stopped", run.InstName)
}
}
return nil
}