Skip to content

Commit

Permalink
Fix(?) error while building Go module app via docker
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed Jul 6, 2019
1 parent 23c26f3 commit e18af7b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
15 changes: 7 additions & 8 deletions internal/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func buildCmd() *cobra.Command {
cmd.Flags().StringP("profile", "p", "", "profile that used for building app")
cmd.Flags().StringSliceP("tags", "t", []string{}, "space-separated list of build tags to satisfied during the build")
cmd.Flags().Bool("copy-deps", false, "copy dependencies for app with dynamic linking")
cmd.Flags().Bool("skip-vendoring", false, "if uses Go module, skip updating project's vendor")

return cmd
}
Expand All @@ -38,6 +39,7 @@ func buildHandler(cmd *cobra.Command, args []string) {
outputPath, _ := cmd.Flags().GetString("output")
profileName, _ := cmd.Flags().GetString("profile")
copyDependencies, _ := cmd.Flags().GetBool("copy-deps")
skipVendoring, _ := cmd.Flags().GetBool("skip-vendoring")

// Load config file
fmt.Print("Load config file...")
Expand Down Expand Up @@ -73,10 +75,11 @@ func buildHandler(cmd *cobra.Command, args []string) {
// only, which make it impossible to generate binding code there.
// Therefore, as workaround, Qamel in Go module *MUST* be used in
// vendor by using `go mod vendor`.
vendorDir := fp.Join(projectDir, "vendor", "github.com", "RadhiFadlillah", "qamel")
goModFile := fp.Join(projectDir, "go.mod")
usesGoModule := fileExists(goModFile)

if usesGoModule {
if usesGoModule && (!dirExists(vendorDir) || !skipVendoring) {
fmt.Print("Generating vendor files...")

cmdModVendor := exec.Command("go", "mod", "vendor")
Expand All @@ -91,13 +94,9 @@ func buildHandler(cmd *cobra.Command, args []string) {
cGreen.Println("done")
}

// Get Qamel directory, depending on whether vendoring is used
qamelDir := fp.Join("github.com", "RadhiFadlillah", "qamel")
vendorDir := fp.Join(projectDir, "vendor", qamelDir)

if dirExists(vendorDir) {
qamelDir = vendorDir
} else {
// If vendor doesn't exist, uses Qamel dir in GOPATH
qamelDir := vendorDir
if !dirExists(qamelDir) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
Expand Down
35 changes: 23 additions & 12 deletions internal/cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func dockerHandler(cmd *cobra.Command, args []string) {
os.Exit(1)
}

// Get gopath
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}

// Get project directory from current working dir
projectDir, err := os.Getwd()
if err != nil {
Expand All @@ -64,14 +70,6 @@ func dockerHandler(cmd *cobra.Command, args []string) {
os.Exit(1)
}

// Get relative path for project dir from $GOPATH
gopath := build.Default.GOPATH
projectDir, err = fp.Rel(gopath, projectDir)
if err != nil {
cRedBold.Println("Failed to get relative path of project dir:", err)
os.Exit(1)
}

// Create docker user
currentUser, err := user.Current()
if err != nil {
Expand All @@ -90,23 +88,36 @@ func dockerHandler(cmd *cobra.Command, args []string) {

// Prepare docker arguments
dockerGopath := unixJoinPath("/", "home", "user", "go")
dockerProjectDir := unixJoinPath(dockerGopath, projectDir)
dockerProjectDir := unixJoinPath(dockerGopath, "src", fp.Base(projectDir))

dockerBindProject := fmt.Sprintf(`type=bind,src=%s,dst=%s`,
projectDir, dockerProjectDir)
dockerBindGoSrc := fmt.Sprintf(`type=bind,src=%s,dst=%s`,
unixJoinPath(gopath, "src"),
unixJoinPath(dockerGopath, "src"))
dockerBindGoCache := fmt.Sprintf(`type=bind,src=%s,dst=%s`,
unixJoinPath(cacheDir),
unixJoinPath("/", "home", "user", ".cache", "go-build"))

dockerImageName := fmt.Sprintf("radhifadlillah/qamel:%s", target)
dockerArgs := []string{"run", "--rm",
"--attach", "stdout",
"--attach", "stderr",
"--user", dockerUser,
"--workdir", dockerProjectDir,
"--mount", dockerBindProject,
"--mount", dockerBindGoSrc,
"--mount", dockerBindGoCache,
dockerImageName}
"--mount", dockerBindGoCache}

goModFile := fp.Join(projectDir, "go.mod")
if fileExists(goModFile) {
dockerArgs = append(dockerArgs, "--env", "GO111MODULE=on")
}

dockerImageName := fmt.Sprintf("radhifadlillah/qamel:%s", target)
dockerArgs = append(dockerArgs, dockerImageName)

// Add qamel arguments
dockerArgs = append(dockerArgs, "--skip-vendoring")

if outputPath != "" {
dockerArgs = append(dockerArgs, "-o", outputPath)
Expand Down

0 comments on commit e18af7b

Please sign in to comment.