This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
/
remove.go
112 lines (99 loc) · 2.55 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
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
package cmd
import (
"context"
"fmt"
"os"
"github.com/hashicorp/go-multierror"
"github.com/ktr0731/dept/deptfile"
"github.com/ktr0731/dept/filegen"
"github.com/ktr0731/dept/gocmd"
"github.com/ktr0731/dept/logger"
"github.com/mitchellh/cli"
"github.com/pkg/errors"
)
// removeCommand removes a passed Go tool from gotool.mod as follows.
//
// 1. load deptfile.
// 2. check whether the passed tool is vendored, or not.
// 3. generate Go code from updated deptfile.
// 4. run 'go mod tidy' to remove unnecessary dependencies.
//
type removeCommand struct {
ui cli.Ui
gocmd gocmd.Command
workspace deptfile.Workspacer
}
func (c *removeCommand) UI() cli.Ui {
return c.ui
}
func (c *removeCommand) Help() string {
return "Usage: dept remove <path [path ...]>"
}
func (c *removeCommand) Synopsis() string {
return fmt.Sprintf("Remove a tool from %s", deptfile.FileName)
}
func (c *removeCommand) Run(args []string) int {
return run(c, func(ctx context.Context) error {
if len(args) < 1 {
return errShowHelp
}
err := c.workspace.Do(func(projRoot string, df *deptfile.File) error {
repoMap := map[string]bool{}
for _, path := range args {
repo, _, err := normalizePath(path)
if err != nil {
return err
}
if _, found := repoMap[repo]; found {
continue
}
repoMap[repo] = false
}
requires := make([]string, 0, len(df.Require))
for _, r := range df.Require {
forTools(r, func(path string) bool {
if _, found := repoMap[path]; found {
repoMap[path] = true
} else {
requires = append(requires, path)
}
return true
})
}
var err error
for repo, found := range repoMap {
if !found {
err = multierror.Append(err, errors.Errorf("%s not found in gotool.mod", repo))
}
}
if err != nil {
return err
}
f, err := os.Create("tools.go")
if err != nil {
return errors.Wrap(err, "failed to create a temp file which contains required Go tools in the import statement")
}
defer os.Remove("tools.go")
defer f.Close()
filegen.Generate(f, requires)
logger.Println("removing unnecessary tools and indirection dependencies")
if err := c.gocmd.ModTidy(ctx); err != nil {
return errors.Wrap(err, "failed to remove the tool from gotool.mod")
}
return nil
})
return err
})
}
// NewRemove returns an initialized removeCommand instance.
func NewRemove(
ui cli.Ui,
gocmd gocmd.Command,
workspace deptfile.Workspacer,
) cli.Command {
return &removeCommand{
ui: ui,
gocmd: gocmd,
workspace: workspace,
}
}