Skip to content

Commit

Permalink
Pretty xgo logging
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Sep 16, 2019
1 parent 9b48705 commit 6607f4c
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions xgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ type BuildFlags struct {
}

func main() {
fmt.Printf("Start xgo %s\n", version)
defer fmt.Println("🏁 Finished!")
fmt.Printf("πŸš€ Start xgo %s\n", version)

// Retrieve the CLI flags and the execution environment
flag.Parse()
Expand All @@ -93,11 +94,11 @@ func main() {
if !xgoInXgo {
// Ensure docker is available
if err := checkDocker(); err != nil {
log.Fatalf("Failed to check docker installation: %v.", err)
log.Fatalf("❌ Failed to check docker installation: %v.", err)
}
// Validate the command line arguments
if len(flag.Args()) != 1 {
log.Fatalf("Usage: %s [options] <go import path>", os.Args[0])
log.Fatalf("πŸ‘‰ Usage: %s [options] <go import path>", os.Args[0])
}
// Select the image to use, either official or custom
image = dockerDist + *goVersion
Expand All @@ -108,47 +109,47 @@ func main() {
found, err := checkDockerImage(image)
switch {
case err != nil:
log.Fatalf("Failed to check docker image availability: %v.", err)
log.Fatalf("❌ Failed to check docker image availability: %v.", err)
case !found:
fmt.Println("not found!")
if err := pullDockerImage(image); err != nil {
log.Fatalf("Failed to pull docker image from the registry: %v.", err)
log.Fatalf("❌ Failed to pull docker image from the registry: %v.", err)
}
default:
fmt.Println("found.")
fmt.Println("πŸŽ‰ Docker image found!")
}
}
// Cache all external dependencies to prevent always hitting the internet
if *crossDeps != "" {
if err := os.MkdirAll(depsCache, 0751); err != nil {
log.Fatalf("Failed to create dependency cache: %v.", err)
log.Fatalf("❌ Failed to create dependency cache: %v.", err)
}
// Download all missing dependencies
for _, dep := range strings.Split(*crossDeps, " ") {
if url := strings.TrimSpace(dep); len(url) > 0 {
path := filepath.Join(depsCache, filepath.Base(url))

if _, err := os.Stat(path); err != nil {
fmt.Printf("Downloading new dependency: %s...\n", url)
fmt.Printf("⬇️ Downloading new dependency: %s...\n", url)

out, err := os.Create(path)
if err != nil {
log.Fatalf("Failed to create dependency file: %v.", err)
log.Fatalf("❌ Failed to create dependency file: %v.", err)
}
res, err := http.Get(url)
if err != nil {
log.Fatalf("Failed to retrieve dependency: %v.", err)
log.Fatalf("❌ Failed to retrieve dependency: %v.", err)
}
defer res.Body.Close()

if _, err := io.Copy(out, res.Body); err != nil {
log.Fatalf("Failed to download dependency: %v", err)
log.Fatalf("❌ Failed to download dependency: %v", err)
}
out.Close()

fmt.Printf("New dependency cached: %s.\n", path)
fmt.Printf("✨ New dependency cached: %s.\n", path)
} else {
fmt.Printf("Dependency already cached: %s.\n", path)
fmt.Printf("🀝 Dependency already cached: %s.\n", path)
}
}
}
Expand All @@ -174,12 +175,12 @@ func main() {
}
folder, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to retrieve the working directory: %v.", err)
log.Fatalf("❌ Failed to retrieve the working directory: %v.", err)
}
if *outFolder != "" {
folder, err = filepath.Abs(*outFolder)
if err != nil {
log.Fatalf("Failed to resolve destination path (%s): %v.", *outFolder, err)
log.Fatalf("❌ Failed to resolve destination path (%s): %v.", *outFolder, err)
}
}
// Execute the cross compilation, either in a container or the current system
Expand All @@ -189,13 +190,13 @@ func main() {
err = compileContained(config, flags, folder)
}
if err != nil {
log.Fatalf("Failed to cross compile package: %v.", err)
log.Fatalf("❌ Failed to cross compile package: %v.", err)
}
}

// Checks whether a docker installation can be found and is functional.
func checkDocker() error {
fmt.Println("Checking docker installation...")
fmt.Println("🐳 Checking docker installation...")
if err := run(exec.Command("docker", "version")); err != nil {
return err
}
Expand All @@ -205,7 +206,7 @@ func checkDocker() error {

// Checks whether a required docker image is available locally.
func checkDockerImage(image string) (bool, error) {
fmt.Printf("Checking for required docker image %s... ", image)
fmt.Printf("🐳 Checking for required docker image %s... ", image)
out, err := exec.Command("docker", "images", "--no-trunc").Output()
if err != nil {
return false, err
Expand All @@ -215,7 +216,7 @@ func checkDockerImage(image string) (bool, error) {

// Pulls an image from the docker registry.
func pullDockerImage(image string) error {
fmt.Printf("Pulling %s from docker registry...\n", image)
fmt.Printf("🐳 Pulling %s from docker registry...\n", image)
return run(exec.Command("docker", "pull", image))
}

Expand All @@ -236,7 +237,7 @@ func compile(image string, config *ConfigFlags, flags *BuildFlags, folder string

// Iterate over all the local libs and export the mount points
if os.Getenv("GOPATH") == "" && !usesModules {
log.Fatalf("No $GOPATH is set or forwarded to xgo")
log.Fatalf("❌ No $GOPATH is set or forwarded to xgo")
}
if !usesModules {
for _, gopath := range strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator)) {
Expand All @@ -245,7 +246,7 @@ func compile(image string, config *ConfigFlags, flags *BuildFlags, folder string
filepath.Walk(sources, func(path string, info os.FileInfo, err error) error {
// Skip any folders that errored out
if err != nil {
log.Printf("Failed to access GOPATH element %s: %v", path, err)
log.Printf("❌ Failed to access GOPATH element %s: %v", path, err)
return nil
}
// Skip anything that's not a symlink
Expand Down Expand Up @@ -279,7 +280,7 @@ func compile(image string, config *ConfigFlags, flags *BuildFlags, folder string
}
}
// Assemble and run the cross compilation command
fmt.Printf("Cross compiling %s...\n", config.Repository)
fmt.Printf("πŸƒ Cross compiling %s...\n", config.Repository)

args := []string{
"run", "--rm",
Expand All @@ -306,18 +307,18 @@ func compile(image string, config *ConfigFlags, flags *BuildFlags, folder string
// Map this repository to the /source folder
absRepository, err := filepath.Abs(config.Repository)
if err != nil {
log.Fatalf("Failed to locate requested module repository: %v.", err)
log.Fatalf("❌ Failed to locate requested module repository: %v.", err)
}
args = append(args, []string{"-v", absRepository + ":/source"}...)

fmt.Printf("Enabled Go module support\n")
fmt.Printf("βœ… Enabled Go module support\n")

// Check whether it has a vendor folder, and if so, use it
vendorPath := absRepository + "/vendor"
vendorfolder, err := os.Stat(vendorPath)
if !os.IsNotExist(err) && vendorfolder.Mode().IsDir() {
args = append(args, []string{"-e", "FLAG_MOD=vendor"}...)
fmt.Printf("Using vendored Go module dependencies\n")
fmt.Printf("βœ… Using vendored Go module dependencies\n")
}
} else {
for i := 0; i < len(locals); i++ {
Expand Down Expand Up @@ -360,7 +361,7 @@ func compileContained(config *ConfigFlags, flags *BuildFlags, folder string) err
env = append(env, "EXT_GOPATH=/non-existent-path-to-signal-local-build")
}
// Assemble and run the local cross compilation command
fmt.Printf("Cross compiling %s...\n", config.Repository)
fmt.Printf("πŸƒ Cross compiling %s...\n", config.Repository)

cmd := exec.Command("/build.sh", config.Repository)
cmd.Env = append(os.Environ(), env...)
Expand All @@ -373,15 +374,15 @@ func compileContained(config *ConfigFlags, flags *BuildFlags, folder string) err
func resolveImportPath(path string) string {
abs, err := filepath.Abs(path)
if err != nil {
log.Fatalf("Failed to locate requested package: %v.", err)
log.Fatalf("❌ Failed to locate requested package: %v.", err)
}
stat, err := os.Stat(abs)
if err != nil || !stat.IsDir() {
log.Fatalf("Requested path invalid.")
log.Fatalf("❌ Requested path invalid.")
}
pack, err := build.ImportDir(abs, build.FindOnly)
if err != nil {
log.Fatalf("Failed to resolve import path: %v.", err)
log.Fatalf("❌ Failed to resolve import path: %v.", err)
}
return pack.ImportPath
}
Expand Down

0 comments on commit 6607f4c

Please sign in to comment.