Skip to content

Commit

Permalink
Handle multiple args (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
knightpp authored Apr 30, 2023
1 parent b7aa159 commit 23d6c82
Showing 1 changed file with 79 additions and 19 deletions.
98 changes: 79 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ import (
"golang.org/x/mod/module"
)

var selectAll bool
var (
selectAll bool
selectAllNoConfirm bool
dryRun bool
)

func init() {
flag.BoolVar(&selectAll, "a", false, "selects everything by default")
flag.BoolVar(&selectAllNoConfirm, "A", false, "selects and updates everything by default without UI")
flag.BoolVar(&dryRun, "d", false, "does not run go get if true")
}

func main() {
Expand All @@ -31,19 +37,57 @@ func main() {

func run() error {
args := flag.Args()
gomodPath := "go.mod"
if len(args) != 0 {
gomodPath = args[0]
if len(args) == 0 {
return updateGoMod("go.mod")
}

content, err := os.ReadFile(gomodPath)
var errs []error
for _, arg := range args {
err := updateGoMod(arg)
if err != nil {
err = fmt.Errorf("could not update %q: %w", arg, err)
errs = append(errs, err)
}
}

return errors.Join(errs...)
}

func updateGoMod(path string) error {
modules, err := parseDirectDeps(path)
if err != nil {
return fmt.Errorf("read file: %w", err)
return fmt.Errorf("parse direct deps: %w", err)
}

if len(modules) == 0 {
return errors.New("no direct dependencies found")
}

ast, err := modfile.Parse(gomodPath, content, nil)
if selectAllNoConfirm {
return runGoGet(path, modulesToPaths(modules))
}

selected, err := runUI(modules)
if err != nil {
return fmt.Errorf("parse: %w", err)
return fmt.Errorf("ui failed: %w", err)
}

if len(selected) == 0 {
return errors.New("no modules selected")
}

return runGoGet(path, modulesToPaths(selected))
}

func parseDirectDeps(path string) ([]module.Version, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}

ast, err := modfile.Parse(path, content, nil)
if err != nil {
return nil, fmt.Errorf("parse go.mod: %w", err)
}

var modules []module.Version
Expand All @@ -55,9 +99,13 @@ func run() error {
modules = append(modules, req.Mod)
}

return modules, nil
}

func runUI(modules []module.Version) ([]module.Version, error) {
f, err := fzf.New(fzf.WithNoLimit(true))
if err != nil {
return err
return nil, fmt.Errorf("create fzf: %w", err)
}

var findOpts []fzf.FindOption
Expand All @@ -70,30 +118,42 @@ func run() error {
return mod.Path + " " + mod.Version
}, findOpts...)
if err != nil {
return fmt.Errorf("fuzzy select: %w", err)
}

if len(indices) == 0 {
return errors.New("no modules selected")
return nil, fmt.Errorf("fuzzy select: %w", err)
}

selected := make([]string, len(indices))
selected := make([]module.Version, len(indices))
for i, idx := range indices {
selected[i] = modules[idx].Path
selected[i] = modules[idx]
}

return selected, nil
}

func runGoGet(path string, selected []string) error {
fmt.Println("go get", strings.Join(selected, " "))

workDir := filepath.Dir(gomodPath)
if dryRun {
return nil
}

workDir := filepath.Dir(path)
cmd := exec.Command("go", append([]string{"get"}, selected...)...)
cmd.Dir = workDir
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

err = cmd.Run()
err := cmd.Run()
if err != nil {
return fmt.Errorf("run go get: %w", err)
}

return nil
return err
}

func modulesToPaths(modules []module.Version) []string {
paths := make([]string, len(modules))
for i := range modules {
paths[i] = modules[i].Path
}
return paths
}

0 comments on commit 23d6c82

Please sign in to comment.