Skip to content

Commit

Permalink
Merge pull request #1382 from rsteube/go-mod-print
Browse files Browse the repository at this point in the history
go: ActionModules - use `edit -json`
  • Loading branch information
rsteube authored Nov 13, 2022
2 parents 8b8de24 + 87e2fbc commit 1c30110
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 59 deletions.
5 changes: 2 additions & 3 deletions completers/go_completer/cmd/mod_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ func init() {
mod_editCmd.Flags().StringS("require", "require", "", "add a requirement")
modCmd.AddCommand(mod_editCmd)

// TODO complete more flags
carapace.Gen(mod_editCmd).FlagCompletion(carapace.ActionMap{
"dropexclude": golang.ActionModules(golang.ModuleOpts{Exclude: true}),
"dropexclude": golang.ActionModules(golang.ModuleOpts{Exclude: true, IncludeVersion: true}),
"dropreplace": golang.ActionModules(golang.ModuleOpts{Replace: true}),
"droprequire": golang.ActionModules(golang.ModuleOpts{Direct: true, IncludeVersion: false}),
"exclude": golang.ActionModuleSearch(),
"module": carapace.ActionFiles(),
"replace": carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return golang.ActionModules(golang.ModuleOpts{Direct: true, Indirect: true})
return golang.ActionModules(golang.ModuleOpts{Direct: true, Indirect: true}).Invoke(c).Suffix("=").ToA()
case 1:
if util.HasPathPrefix(c.CallbackValue) {
path, err := util.FindReverse(c.Dir, "go.mod")
Expand Down
96 changes: 40 additions & 56 deletions pkg/actions/tools/golang/module.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package golang

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/util"
"github.com/rsteube/carapace/pkg/style"
)

type module struct {
Path string
Version string
Indirect bool
Replace *struct {
Path string
Version string
type mod struct {
Module struct {
Path string
}
Go string
Require []struct {
Path string
Version string
Indirect bool
}
Exclude *struct {
Exclude []struct {
Path string
Version string
}
Replace []struct {
Old struct {
Path string
}
New struct {
Path string
Version string
}
}
Retract interface{}
}

type ModuleOpts struct {
Expand All @@ -46,57 +53,34 @@ func (o ModuleOpts) Default() ModuleOpts {
// github.com/rsteube/carapace
// github.com/rsteube/carapace-spec@v0.0.1
func ActionModules(opts ModuleOpts) carapace.Action {
return carapace.ActionExecCommand("go", "list", "-m", "-json", "all")(func(output []byte) carapace.Action {
dec := json.NewDecoder(bytes.NewReader(output))
return carapace.ActionExecCommand("go", "mod", "edit", "-json")(func(output []byte) carapace.Action {
var m mod
if err := json.Unmarshal(output, &m); err != nil {
return carapace.ActionMessage(err.Error())
}

vals := make([]string, 0)
for {
var m module
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
return carapace.ActionMessage(err.Error())

}
if opts.Direct && !m.Indirect && m.Replace == nil { // TODO Exclude
vals = append(vals, formatModule(m.Path, m.Version, opts.IncludeVersion), style.Blue)
} else if opts.Indirect && m.Indirect && m.Replace == nil { // TODO Exclude
vals = append(vals, formatModule(m.Path, m.Version, opts.IncludeVersion), style.Gray)
} else if opts.Replace && m.Replace != nil { // TODO Exclude
vals = append(vals, formatModule(m.Path, m.Version, opts.IncludeVersion), style.Magenta)
if opts.Direct && m.Require != nil {
for _, r := range m.Require {
if opts.Direct && !r.Indirect {
vals = append(vals, formatModule(r.Path, r.Version, opts.IncludeVersion), style.Blue)
} else if opts.Indirect && r.Indirect {
vals = append(vals, formatModule(r.Path, r.Version, opts.IncludeVersion), style.Gray)
}
}
}

batch := carapace.Batch(
carapace.ActionStyledValues(vals...),
)
if opts.Exclude {
batch = append(batch, actionModuleExcludes(opts.IncludeVersion))
}

return batch.ToA()
})
}

func actionModuleExcludes(includeVersion bool) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
path, err := util.FindReverse(c.Dir, "go.mod")
if err != nil {
return carapace.ActionMessage(err.Error())
}

content, err := os.ReadFile(path)
if err != nil {
return carapace.ActionMessage(err.Error())
if opts.Replace && m.Replace != nil {
for _, r := range m.Replace {
vals = append(vals, r.Old.Path, style.Magenta)
}
}

vals := make([]string, 0)
for _, line := range strings.Split(string(content), "\n") {
if strings.HasPrefix(line, "exclude ") {
if fields := strings.Fields(line); len(fields) == 3 {
vals = append(vals, formatModule(fields[1], fields[2], includeVersion), style.Red)
}
if opts.Exclude && m.Exclude != nil {
for _, r := range m.Exclude {
vals = append(vals, formatModule(r.Path, r.Version, opts.IncludeVersion), style.Red)
}

}
return carapace.ActionStyledValues(vals...)
})
Expand Down

0 comments on commit 1c30110

Please sign in to comment.