-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chris Koch <[email protected]>
- Loading branch information
Showing
4 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
goanywhere |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/u-root/gobusybox/src/pkg/bb/findpkg" | ||
"github.com/u-root/gobusybox/src/pkg/golang" | ||
"github.com/u-root/uio/ulog" | ||
"golang.org/x/exp/maps" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
var ( | ||
debug = flag.Bool("d", false, "Show debug string") | ||
tmp = flag.Bool("tmp", true, "Create workspace in temp dir") | ||
) | ||
|
||
func run(dir string, args []string, paths []string) { | ||
var relPaths []string | ||
absDir, err := filepath.Abs(dir) | ||
if err != nil { | ||
relPaths = paths | ||
} else { | ||
for _, p := range paths { | ||
rp, err := filepath.Rel(absDir, p) | ||
if err != nil || len(rp) >= len(p) { | ||
relPaths = append(relPaths, p) | ||
} else { | ||
relPaths = append(relPaths, "./"+rp) | ||
} | ||
} | ||
} | ||
if *debug { | ||
if dir != "" { | ||
fmt.Printf("+ cd %s\n", dir) | ||
} | ||
fmt.Printf("+ %s %s\n", strings.Join(args, " "), strings.Join(relPaths, " ")) | ||
} | ||
c := exec.Command(args[0], append(args[1:], relPaths...)...) | ||
c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr | ||
c.Dir = dir | ||
if err := c.Run(); err != nil { | ||
log.Fatalf("Exited with %v", err) | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
log.SetPrefix("[goanywhere] ") | ||
|
||
args := flag.Args() | ||
if len(args) == 0 { | ||
log.Fatalf("Usage: goanywhere $pkg... -- <command> ...") | ||
} | ||
dashIndex := slices.Index(args, "--") | ||
if dashIndex == -1 { | ||
log.Fatalf("Must use -- to distinguish Go packages from command. Usage: goanywhere $pkg... -- <command> ...") | ||
} | ||
if len(args) == dashIndex+1 { | ||
log.Fatalf("Must provide command to run. Usage: goanywhere $pkg... -- <command> ...") | ||
} | ||
pkgs := args[:dashIndex] | ||
args = args[dashIndex+1:] | ||
|
||
env := golang.Default() | ||
paths := findpkg.GlobPaths(ulog.Log, findpkg.DefaultEnv(), pkgs...) | ||
mods, noModulePaths := findpkg.Modules(paths) | ||
|
||
if env.GO111MODULE == "off" { | ||
run("", args, paths) | ||
} | ||
|
||
if len(noModulePaths) > 0 { | ||
log.Fatalf("No modules found for %v", noModulePaths) | ||
} | ||
|
||
var dir string | ||
if *tmp { | ||
var err error | ||
dir, err = os.MkdirTemp("", "goanywhere-") | ||
if err != nil { | ||
log.Fatalf("Could not create temp dir: %v", err) | ||
} | ||
defer os.RemoveAll(dir) | ||
} | ||
|
||
tpl := `go {{.Version}} | ||
use ({{range .Modules}} | ||
{{.}}{{end}} | ||
) | ||
` | ||
|
||
vars := struct { | ||
Version string | ||
Modules []string | ||
}{ | ||
Version: "1.22", | ||
Modules: maps.Keys(mods), | ||
} | ||
t := template.Must(template.New("tpl").Parse(tpl)) | ||
var b bytes.Buffer | ||
if err := t.Execute(&b, vars); err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := os.WriteFile(filepath.Join(dir, "go.work"), b.Bytes(), 0o644); err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove(filepath.Join(dir, "go.work")) | ||
|
||
run(dir, args, paths) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters