package main import ( "os/exec" "path/filepath" "strings" "sync" "time" ) func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { start := time.Now() // calculate filepath b := new(strings.Builder) data := &OutputFileTmplData{ Name: ProjectName, OS: cfg.OS, Arch: cfg.Arch, Ext: cfg.FileExt, } if err := OutputFileTmpl.Execute(b, data); err != nil { ch <- &CompileReport{Config: cfg, State: StateCompileError} wg.Done() return } filePath := filepath.Join(*OutputDir, b.String()) filePath, err := filepath.Abs(filePath) if err != nil { ch <- &CompileReport{Config: cfg, State: StateCompileError} wg.Done() return } ch <- &CompileReport{Config: cfg, State: StateCompiling} args := []string{"build", "-o", filePath, "-ldflags=" + BuildLdFlags(cfg.OS, cfg.Arch)} compileCmd := exec.Command("go", args...) compileCmd.Dir = ModulePath if err := compileCmd.Start(); err != nil { ch <- &CompileReport{Config: cfg, State: StateCompileError} wg.Done() return } if err := compileCmd.Wait(); err != nil { ch <- &CompileReport{Config: cfg, State: StateCompileError} wg.Done() return } Compress(filePath, start, cfg, ch, wg) } func Compress(filePath string, start time.Time, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { defer wg.Done() ch <- &CompileReport{Config: cfg, State: StateCompressing} if !*NoCompress && cfg.Compress { compressCmd := exec.Command("upx", "--best", filePath) compressCmd.Dir = ModulePath if err := compressCmd.Start(); err != nil { ch <- &CompileReport{Config: cfg, State: StateCompressError} return } if err := compressCmd.Wait(); err != nil { ch <- &CompileReport{Config: cfg, State: StateCompressError} return } } ch <- &CompileReport{Config: cfg, State: StateDone, Duration: time.Since(start)} }