feat: support build Dockerfile from current dir (#2021)
This commit is contained in:
@@ -3,6 +3,7 @@ package docker
|
|||||||
import "github.com/spf13/cobra"
|
import "github.com/spf13/cobra"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
varExeName string
|
||||||
varStringGo string
|
varStringGo string
|
||||||
varStringBase string
|
varStringBase string
|
||||||
varIntPort int
|
varIntPort int
|
||||||
@@ -21,6 +22,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
Cmd.Flags().StringVar(&varExeName, "exe", "", "The executable name in the built image")
|
||||||
Cmd.Flags().StringVar(&varStringGo, "go", "", "The file that contains main function")
|
Cmd.Flags().StringVar(&varStringGo, "go", "", "The file that contains main function")
|
||||||
Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch")
|
Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch")
|
||||||
Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none")
|
Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -24,6 +25,7 @@ const (
|
|||||||
// Docker describes a dockerfile
|
// Docker describes a dockerfile
|
||||||
type Docker struct {
|
type Docker struct {
|
||||||
Chinese bool
|
Chinese bool
|
||||||
|
GoMainFrom string
|
||||||
GoRelPath string
|
GoRelPath string
|
||||||
GoFile string
|
GoFile string
|
||||||
ExeFile string
|
ExeFile string
|
||||||
@@ -65,11 +67,7 @@ func dockerCommand(_ *cobra.Command, _ []string) (err error) {
|
|||||||
pathx.RegisterGoctlHome(home)
|
pathx.RegisterGoctlHome(home)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(goFile) == 0 {
|
if len(goFile) > 0 && !pathx.FileExists(goFile) {
|
||||||
return errors.New("-go can't be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !pathx.FileExists(goFile) {
|
|
||||||
return fmt.Errorf("file %q not found", goFile)
|
return fmt.Errorf("file %q not found", goFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,9 +124,13 @@ func findConfig(file, dir string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateDockerfile(goFile, base string, port int, version, timezone string, args ...string) error {
|
func generateDockerfile(goFile, base string, port int, version, timezone string, args ...string) error {
|
||||||
projPath, err := getFilePath(filepath.Dir(goFile))
|
var projPath string
|
||||||
if err != nil {
|
var err error
|
||||||
return err
|
if len(goFile) > 0 {
|
||||||
|
projPath, err = getFilePath(filepath.Dir(goFile))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(projPath) == 0 {
|
if len(projPath) == 0 {
|
||||||
@@ -151,12 +153,27 @@ func generateDockerfile(goFile, base string, port int, version, timezone string,
|
|||||||
builder.WriteString(`, "` + arg + `"`)
|
builder.WriteString(`, "` + arg + `"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var exeName string
|
||||||
|
if len(varExeName) > 0 {
|
||||||
|
exeName = varExeName
|
||||||
|
} else if len(goFile) > 0 {
|
||||||
|
exeName = pathx.FileNameWithoutExt(filepath.Base(goFile))
|
||||||
|
} else {
|
||||||
|
absPath, err := filepath.Abs(projPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
exeName = filepath.Base(absPath)
|
||||||
|
}
|
||||||
|
|
||||||
t := template.Must(template.New("dockerfile").Parse(text))
|
t := template.Must(template.New("dockerfile").Parse(text))
|
||||||
return t.Execute(out, Docker{
|
return t.Execute(out, Docker{
|
||||||
Chinese: env.InChina(),
|
Chinese: env.InChina(),
|
||||||
|
GoMainFrom: path.Join(projPath, goFile),
|
||||||
GoRelPath: projPath,
|
GoRelPath: projPath,
|
||||||
GoFile: goFile,
|
GoFile: goFile,
|
||||||
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
|
ExeFile: exeName,
|
||||||
BaseImage: base,
|
BaseImage: base,
|
||||||
HasPort: port > 0,
|
HasPort: port > 0,
|
||||||
Port: port,
|
Port: port,
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ ADD go.sum .
|
|||||||
RUN go mod download
|
RUN go mod download
|
||||||
COPY . .
|
COPY . .
|
||||||
{{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
|
{{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
|
||||||
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
|
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoMainFrom}}
|
||||||
|
|
||||||
|
|
||||||
FROM {{.BaseImage}}
|
FROM {{.BaseImage}}
|
||||||
|
|||||||
Reference in New Issue
Block a user