/
remove.go
81 lines (65 loc) · 1.68 KB
/
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
package remove
import (
"context"
"flag"
"github.com/evg4b/fisherman/internal/constants"
"github.com/evg4b/fisherman/internal/utils"
"github.com/evg4b/fisherman/pkg/guards"
"github.com/evg4b/fisherman/pkg/log"
"path/filepath"
"github.com/go-git/go-billy/v5"
)
type Command struct {
flagSet *flag.FlagSet
usage string
fs billy.Filesystem
cwd string
configFiles map[string]string
}
func NewCommand(options ...RemoveOption) *Command {
command := &Command{
flagSet: flag.NewFlagSet("remove", flag.ExitOnError),
usage: "removes fisherman from git repository",
configFiles: map[string]string{},
}
for _, option := range options {
option(command)
}
guards.ShouldBeDefined(command.fs, "FileSystem should be configured")
guards.ShouldBeNotEmpty(command.cwd, "Cwd should be configured")
return command
}
func (c *Command) Run(_ context.Context, args []string) error {
err := c.flagSet.Parse(args)
if err != nil {
return err
}
filesToDelete := make([]string, 0, len(c.configFiles)+len(constants.HooksNames))
for _, config := range c.configFiles {
filesToDelete = append(filesToDelete, config)
}
for _, hookName := range constants.HooksNames {
path := filepath.Join(c.cwd, ".git", "hooks", hookName)
exist, err := utils.Exists(c.fs, path)
if err != nil {
return err
}
if exist {
filesToDelete = append(filesToDelete, path)
}
}
for _, hookPath := range filesToDelete {
err := c.fs.Remove(hookPath)
if err != nil {
return err
}
log.Infof("File '%s' was removed", hookPath)
}
return nil
}
func (c *Command) Name() string {
return c.flagSet.Name()
}
func (c *Command) Description() string {
return c.usage
}