Skip to content

Commit

Permalink
DEV: parameterize config filename
Browse files Browse the repository at this point in the history
Parameterize the config filename for writing config to and expected file to be
copied from in resulting dockerfiles
  • Loading branch information
featheredtoast committed Sep 30, 2024
1 parent 20e33fb commit 16480ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions launcher_go/v2/cli_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (r *DockerBuildCmd) Run(cli *Cli, ctx *context.Context) error {
if err := os.MkdirAll(dir, 0755); err != nil && !os.IsExist(err) {
return err
}
if err := config.WriteYamlConfig(dir); err != nil {
configFile := "config.yaml"
if err := config.WriteYamlConfig(dir, configFile); err != nil {
return err
}

Expand All @@ -48,7 +49,7 @@ func (r *DockerBuildCmd) Run(cli *Cli, ctx *context.Context) error {
builder := docker.DockerBuilder{
Config: config,
Ctx: ctx,
Stdin: strings.NewReader(config.Dockerfile(pupsArgs, r.BakeEnv)),
Stdin: strings.NewReader(config.Dockerfile(pupsArgs, r.BakeEnv, configFile)),
Dir: dir,
Namespace: namespace,
ImageTag: r.Tag,
Expand Down
14 changes: 10 additions & 4 deletions launcher_go/v2/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ func (config *Config) Yaml() string {
return strings.Join(config.rawYaml, "_FILE_SEPERATOR_")
}

func (config *Config) Dockerfile(pupsArgs string, bakeEnv bool) string {
func (config *Config) Dockerfile(pupsArgs string, bakeEnv bool, configFile string) string {
if configFile == "" {
configFile = "config.yaml"
}
builder := strings.Builder{}
builder.WriteString("ARG dockerfile_from_image=" + config.Base_Image + "\n")
builder.WriteString("FROM ${dockerfile_from_image}\n")
Expand All @@ -139,16 +142,19 @@ func (config *Config) Dockerfile(pupsArgs string, bakeEnv bool) string {
builder.WriteString(config.dockerfileEnvs() + "\n")
}
builder.WriteString(config.dockerfileExpose() + "\n")
builder.WriteString("COPY config.yaml /temp-config.yaml\n")
builder.WriteString("COPY " + configFile + " /temp-config.yaml\n")
builder.WriteString("RUN " +
"cat /temp-config.yaml | /usr/local/bin/pups " + pupsArgs + " --stdin " +
"&& rm /temp-config.yaml\n")
builder.WriteString("CMD [\"" + config.BootCommand() + "\"]")
return builder.String()
}

func (config *Config) WriteYamlConfig(dir string) error {
file := strings.TrimRight(dir, "/") + "/config.yaml"
func (config *Config) WriteYamlConfig(dir string, configFile string) error {
if configFile == "" {
configFile = "config.yaml"
}
file := strings.TrimRight(dir, "/") + "/" + configFile
if err := os.WriteFile(file, []byte(config.Yaml()), 0660); err != nil {
return errors.New("error writing config file " + file)
}
Expand Down
4 changes: 2 additions & 2 deletions launcher_go/v2/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("Config", func() {
})

It("can write raw yaml config", func() {
err := conf.WriteYamlConfig(testDir)
err := conf.WriteYamlConfig(testDir, "config.yaml")
Expect(err).To(BeNil())
out, err := os.ReadFile(testDir + "/config.yaml")
Expect(err).To(BeNil())
Expand All @@ -38,7 +38,7 @@ var _ = Describe("Config", func() {
})

It("can convert pups config to dockerfile format", func() {
dockerfile := conf.Dockerfile("", false)
dockerfile := conf.Dockerfile("", false, "config.yaml")
Expect(dockerfile).To(ContainSubstring("ARG DISCOURSE_DEVELOPER_EMAILS"))
Expect(dockerfile).To(ContainSubstring("RUN cat /temp-config.yaml"))
Expect(dockerfile).To(ContainSubstring("EXPOSE 80"))
Expand Down

0 comments on commit 16480ce

Please sign in to comment.