>
> A bin util, which would do something similar to your idea
> `goexec bin args...` might be smart enough to sort things out
> and call for the right binary in the right path,
> it is also able to provide detailed information when something wrong
> happen.
>

It would indeed be unambiguous, because a go import path is unambiguous.

More than that, if a collision happen,
> it allows to add precision about the bin to execute until the collision is
> solved,
>  something like
> `goexec user/bin args...`
> `goexec gh.com/user/bin args...`
>

This is not what I'm proposing.

In slightly more formal terms, considering the package P, I'm proposing a
go subcommand that is equivalent to:

======
package main

import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

func main() {
flag.Parse()

if v := len(flag.Args()); v == 0 {
errorf("expected at least one argument")
}

p := flag.Arg(0)

if v := mustExec("go", "list", "-f", "{{.ImportPath}}", p); v != p {
errorf("must supply unambiguous package; %v is not", p)
}

if v := mustExec("go", "list", "-f", "{{.Name}}", p); v != "main" {
errorf("must supply main package; %v is not", p)
}

target := mustExec("go", "list", "-f", "{{.Target}}", p)
targetDir := filepath.Dir(target)

specTarget := filepath.Join(targetDir, p)

vs := append([]string{specTarget}, flag.Args()[1:]...)

fmt.Printf("syscall.Exec(\"%v\")\n", strings.Join(vs, "\", \""))
}

func errorf(format string, args ...interface{}) {
format = format + "\n"
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(1)
}

func mustExec(c string, args ...string) string {
cmd := exec.Command(c, args...)

res, err := cmd.Output()
if err != nil {
panic(err)
}

return string(bytes.TrimSpace(res))
}

======

For example, in a particular environment I have, considering the main
package github.com/gopherjs/gopherjs I get the output:

syscall.Exec("/home/myitcv/gostuff/bin/github.com/gopherjs/gopherjs",
"serve", "-m")

Indeed I would also propose that we augment go list -json to include a
{{.SpecificTarget}} for main packages, a value that would be equal to
"/home/myitcv/gostuff/bin/github.com/gopherjs/gopherjs" in this example.


> And to make it awesome, auto completion support would be just awesome!
> imho.
>

This would have to be the realm of the user's environment; bash and other
shells already have good support for the go subcommands... so I suspect
this would readily follow.


Paul

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to