posh/internal/cli/utils.go
Kevin Franklin Kim e2ad376b6c initial commit
2023-01-03 15:37:15 +01:00

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
}