go-zero/tools/goctl/gen/dockerfile.go

43 lines
900 B
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gen
import (
2020-08-31 12:27:38 +08:00
"path/filepath"
2020-07-29 17:11:41 +08:00
"strings"
"text/template"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/vars"
2020-07-29 17:11:41 +08:00
)
func GenerateDockerfile(goFile string, args ...string) error {
2020-08-31 12:27:38 +08:00
projPath, err := getFilePath(filepath.Dir(goFile))
2020-07-29 17:11:41 +08:00
if err != nil {
return err
}
2020-08-31 12:27:38 +08:00
pos := strings.IndexByte(projPath, '/')
if pos >= 0 {
projPath = projPath[pos+1:]
}
2020-07-29 17:11:41 +08:00
out, err := util.CreateIfNotExist("Dockerfile")
if err != nil {
return err
}
defer out.Close()
var builder strings.Builder
for _, arg := range args {
builder.WriteString(`, "` + arg + `"`)
}
t := template.Must(template.New("dockerfile").Parse(dockerTemplate))
return t.Execute(out, map[string]string{
"projectName": vars.ProjectName,
2020-08-30 23:52:51 +08:00
"goRelPath": projPath,
2020-07-29 17:11:41 +08:00
"goFile": goFile,
"exeFile": util.FileNameWithoutExt(goFile),
"argument": builder.String(),
})
}