-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
work.go
101 lines (93 loc) · 2.56 KB
/
work.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
package golang
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace/pkg/util"
)
type work struct {
Go string
Use []struct {
DiskPath string
}
Replace []struct {
Old struct {
Path string
}
New struct {
Path string
Version string
}
}
}
func actionWork(path string, f func(w work) carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"work", "edit", "-json"}
if path != "" {
args = append(args, path)
}
return carapace.ActionExecCommand("go", args...)(func(output []byte) carapace.Action {
var w work
if err := json.Unmarshal(output, &w); err != nil {
return carapace.ActionMessage(err.Error())
}
return f(w)
})
})
}
// ActionWorkUses completes workspace uses
//
// ./carapace
// ./carapace-bin
func ActionWorkUses(path string) carapace.Action {
return actionWork(path, func(w work) carapace.Action {
vals := make([]string, 0)
for _, use := range w.Use {
vals = append(vals, use.DiskPath)
}
return carapace.ActionValues(vals...)
})
}
// ActionWorkReplacements completes workspace replacements
// github.com/carapace-sh/carapace-spec (github.com/carapace-sh/carapace-spec@v0.3.0
// github.com/spf13/pflag (../carapace-pflag/)
func ActionWorkReplacements(path string) carapace.Action {
return actionWork(path, func(w work) carapace.Action {
vals := make([]string, 0)
for _, replace := range w.Replace {
if replace.New.Version != "" {
vals = append(vals, replace.Old.Path, fmt.Sprintf("%v@%v", replace.New.Path, replace.New.Version))
} else {
vals = append(vals, replace.Old.Path, replace.New.Path)
}
}
return carapace.ActionValuesDescribed(vals...)
})
}
// ActionWorkModules completes workspace modules
//
// github.com/pelletier/go-toml
// github.com/carapace-sh/carapace
func ActionWorkModules(path string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return actionWork(path, func(w work) carapace.Action {
if path == "" {
var err error
if path, err = util.FindReverse(path, "go.work"); err != nil {
return carapace.ActionMessage(err.Error())
}
}
abs, err := c.Abs(path)
if err != nil {
return carapace.ActionMessage(err.Error())
}
batch := carapace.Batch()
for _, use := range w.Use {
dir := fmt.Sprintf("%v/%v", filepath.Dir(abs), use.DiskPath)
batch = append(batch, ActionModules(ModuleOpts{Direct: true, Indirect: true}).Chdir(dir))
}
return batch.ToA()
})
})
}