github.com/git-ogawa/go-dbyml@v1.2.1/dbyml/template.go (about) 1 package dbyml 2 3 import ( 4 "fmt" 5 "os" 6 "text/template" 7 ) 8 9 // ConfigurationTemplate is a template of dbyml settings. 10 const ConfigurationTemplate = `--- 11 # file: dbyml.yml 12 # This is a configuration file used by go-dbyml. 13 # The settings related to image build are written in yaml syntax. 14 # The values set in each field, so you can edit them according to your build settings. 15 # This is automatically generated. 16 17 18 # The image section manages docker image attributes. 19 image: 20 # name: Image name. This field is required. 21 name: {{ or .ImageInfo.Basename "go-dbyml-sample" }} 22 23 # tag: Image tag. 24 tag: {{ or .ImageInfo.Tag "latest" }} 25 26 # path: Path to a directory containing Dockerfile. 27 path: {{ or .ImageInfo.Context "." }} 28 29 # dockerfile: Filename of Dockerfile. 30 dockerfile: {{ or .ImageInfo.Dockerfile "Dockerfile" }} 31 32 # build_args: Arguments corresponding to build-args of docker build. 33 # Set list of key:value 34 build_args: 35 {{- range $k, $v := .ImageInfo.BuildArgs }} 36 {{ $k }}: {{ $v }} 37 {{- end }} 38 39 # label: Arguments corresponding to label of docker build. 40 # Use quotation when the key name includes "." such as com.test.name 41 # Set list of key:value 42 label: 43 {{- range $k, $v := .ImageInfo.Labels }} 44 {{ $k }}: {{ $v }} 45 {{- end }} 46 47 # docker_host: URL to the Docker server. 48 # Set protocol:hostname:port for example unix:///var/run/docker.sock or tcp://127.0.0.1:1234. 49 # Default to unix:///var/run/docker.sock 50 docker_host: {{ or .ImageInfo.DockerHost "unix:/var/run/docker.sock" }} 51 52 53 # The build section manages some options on build such as using build-cache or showing build information. 54 build: 55 # target: Name of the build-stage to build in a multi-stage Dockerfile. 56 target: {{ or .BuildInfo.Target "''" }} 57 58 # no_cache: Set true not to use build cache when build the image. 59 # default: false 60 no_cache: {{ or .BuildInfo.NoCache false }} 61 62 # verbose: Set true to show build settings on build. 63 # default: true 64 verbose: {{ or .BuildInfo.Verbose true }} 65 66 # The registry section manages the information about registry to which the image push. 67 registry: 68 # enabled: Enable push to a registry. Set false not to push the image 69 # to the registry even if these fields are set. 70 enabled: {{ or .RegistryInfo.Enabled false }} 71 72 # host: Registry name or ip address and port. 73 host: {{ or .RegistryInfo.Host "myregistry.com:5000" }} 74 75 # project: 76 # When set the project, the image to be pushed to the registry will be {host}:{port}/{project}/{name}:{tag}, 77 # otherwise {host}:{port}/{name}:{tag} 78 project: {{ or .RegistryInfo.Project "" }} 79 80 # auth: Credentials for a registry. This will be used when push a image to basic-auth registry. 81 auth: 82 {{- range $k, $v := .RegistryInfo.Auth }} 83 {{ $k }}: {{ $v }} 84 {{- end }} 85 86 # The buildkit section manages the settings when build with buildkit. 87 buildkit: 88 # enabled: Set true to enable build with buildkit. 89 enabled: {{ or .BuildkitInfo.Enabled false }} 90 # output: The output field sets output format of the image to be built 91 output: 92 # type: Type of output image. 93 type: {{ or .BuildkitInfo.Output.Type "image" }} 94 # name: Set registry and image. e.g. [registry]:[port]/[project]/[image]:[tag] 95 name: {{ or .BuildkitInfo.Output.Name "myregistry.com/go-dbyml-sample:latest" }} 96 # insecure: Set true if push the image insecure registry such as insecure private registry 97 insecure: {{ or .BuildkitInfo.Output.Insecure false }} 98 # The cache field sets import and export build cache. 99 cache: 100 # export: Export settings of build cache. 101 # Type must be either inline or registry. Set inline to export the cache embed with the image and pushing them to registry together. 102 # Set registry to export build cache to the specified registry. In this case, set the registry in value field. e.g. [registry]:[port]/[project]/[image]:[tag] 103 export: 104 type: {{ or .BuildkitInfo.Cache.Export.Type "inline" }} 105 value: {{ or .BuildkitInfo.Cache.Export.Value "''" }} 106 # import: Import settings of build cache. 107 # Type must be registry. Set registry to import build cache from the specified registry. In this case, set the registry in value field. e.g. [registry]:[port]/[project]/[image]:[tag]. 108 import: 109 type: {{ or .BuildkitInfo.Cache.Import.Type "registry" }} 110 value: {{ or .BuildkitInfo.Cache.Import.Value "myregistry.com/go-dbyml-sample:latest" }} 111 # platform: Set the list of architectures if want to build a image that support multi-platform. 112 platform: 113 # remove: Set true to remove a builder container after build is successfully completed. 114 remove: {{ or .BuildkitInfo.Remove true }} 115 ` 116 117 // MakeTemplate makes a dbyml setting file from a template. 118 func MakeTemplate(config *Configuration) { 119 tmpl := template.Must(template.New("ConfigurationTemplate").Parse(ConfigurationTemplate)) 120 121 file, _ := os.Create("dbyml.yml") 122 err := tmpl.Execute(file, config) 123 if err != nil { 124 panic(err) 125 } 126 fmt.Println("Create dbyml.yml. Check the contents and edit it according to your docker image.") 127 } 128 129 // BuildkitdTomlTemplate is a template of buildkit settings. 130 const BuildkitdTomlTemplate = `[registry."{{ .Host }}"] 131 {{- if .Insecure }} 132 insecure = true 133 {{- end }} 134 {{- if .Auth.ca_cert }} 135 ca_cert = ["{{ .Auth.ca_cert }}"] 136 {{- end }} 137 ` 138 139 // MakeBuildkitToml makes buildkitd.toml from a template. 140 func MakeBuildkitToml(config *RegistryInfo) (string, error) { 141 tmpl := template.Must(template.New("BuildkitdTomlTemplate").Parse(BuildkitdTomlTemplate)) 142 143 file, _ := os.Create("buildkitd.toml") 144 err := tmpl.Execute(file, config) 145 if err != nil { 146 return "", err 147 } 148 return "buildkitd.toml", nil 149 }