mirror of
https://github.com/foomo/posh.git
synced 2025-10-16 12:45:38 +00:00
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type (
|
|
Mod struct {
|
|
Path, Dir, GoMod string
|
|
}
|
|
CurDir struct {
|
|
Dir string
|
|
}
|
|
)
|
|
|
|
func GetModImportPath() string {
|
|
mod, cd := parseModInfo()
|
|
return path.Join(mod.Path, fileToURL(strings.TrimPrefix(cd.Dir, mod.Dir)))
|
|
}
|
|
|
|
func parseModInfo() (Mod, CurDir) {
|
|
var mod Mod
|
|
var dir CurDir
|
|
|
|
m := modInfoJSON("-m")
|
|
cobra.CheckErr(json.Unmarshal(m, &mod))
|
|
|
|
// Unsure why, but if no module is present Path is set to this string.
|
|
if mod.Path == "command-line-arguments" {
|
|
cobra.CheckErr("Please run `go mod init <MODNAME>` before `cobra-cli init`")
|
|
}
|
|
|
|
e := modInfoJSON("-e")
|
|
cobra.CheckErr(json.Unmarshal(e, &dir))
|
|
|
|
return mod, dir
|
|
}
|
|
|
|
func fileToURL(in string) string {
|
|
i := strings.Split(in, string(filepath.Separator))
|
|
return path.Join(i...)
|
|
}
|
|
|
|
func modInfoJSON(args ...string) []byte {
|
|
cmdArgs := append([]string{"list", "-json"}, args...)
|
|
out, err := exec.Command("go", cmdArgs...).Output()
|
|
cobra.CheckErr(err)
|
|
return out
|
|
}
|