From 20f84c6bf1c11b420a59a7450d506bdf3a848036 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Tue, 30 Aug 2022 00:42:47 +0200 Subject: [PATCH] print go install command by default --- compile.go | 2 +- main.go | 19 ++++++++++++++++--- metadata.go | 22 ++++++++++++---------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/compile.go b/compile.go index 4500071..f15938d 100644 --- a/compile.go +++ b/compile.go @@ -35,7 +35,7 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { ch <- &CompileReport{Config: cfg, State: StateCompiling} - args := []string{"build", "-o", filePath, BuildLdFlags(cfg.OS, cfg.Arch)} + args := []string{"build", "-o", filePath, "-ldflags=" + BuildLdFlags(cfg.OS, cfg.Arch)} compileCmd := exec.Command("go", args...) compileCmd.Dir = ModulePath diff --git a/main.go b/main.go index 98d1d74..e2f8d04 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,9 @@ import ( var ( OutputFileTmpl = template.New("output-file") - ModulePath string + ModulePath string + + ProjectPath string ProjectName string // meta data for executables @@ -62,6 +64,8 @@ var ( IgnoreConfigFile = flag.Bool("ignoreconfig", false, "dont read any config file") FindConfigFile = flag.Bool("findconfig", false, "print config file path and exit") + DontPrintInstallCmd = flag.Bool("p", false, "print 'go install' command") + ShowVersion = flag.Bool("v", false, "show version and exit") ) @@ -69,6 +73,15 @@ func main() { Init() GatherMetaData() + if !*Silent && !*DontPrintInstallCmd { + tag := VersionTag + if tag == "" || strings.HasPrefix(tag, "(devel") { + tag = "latest" + } + Println(ColorDone.Sprintf("go install -ldflags=\"%s\" %s@%s", BuildLdFlags("", ""), ProjectPath, tag)) + Println() + } + ch := make(chan *CompileReport, len(CompileConfigs)) wg := new(sync.WaitGroup) @@ -105,8 +118,8 @@ func DetermineProjectName() error { for _, line := range strings.Split(string(data), "\n") { if strings.HasPrefix(line, "module ") { - fullName := strings.TrimPrefix(line, "module ") - parts := strings.Split(fullName, "/") + ProjectPath = strings.TrimPrefix(line, "module ") + parts := strings.Split(ProjectPath, "/") ProjectName = parts[len(parts)-1] return nil } diff --git a/metadata.go b/metadata.go index 47d4dfb..9f364cf 100644 --- a/metadata.go +++ b/metadata.go @@ -119,8 +119,6 @@ func GatherMetaData() { func BuildLdFlags(os, arch string) string { b := &strings.Builder{} - b.WriteString("-ldflags=") - if !*KeepDebugFlags { b.WriteString("-s -w") } @@ -149,15 +147,19 @@ func BuildLdFlags(os, arch string) string { b.WriteString("=") b.WriteString(BuildTime) - b.WriteString(" -X ") - b.WriteString("git.milar.in/milarin/buildinfo.OS") - b.WriteString("=") - b.WriteString(os) + if os != "" { + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.OS") + b.WriteString("=") + b.WriteString(os) + } - b.WriteString(" -X ") - b.WriteString("git.milar.in/milarin/buildinfo.Arch") - b.WriteString("=") - b.WriteString(arch) + if arch != "" { + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.Arch") + b.WriteString("=") + b.WriteString(arch) + } return b.String() }