diff --git a/default_config_generator.go b/default_config_generator.go index 03ea3ac6..c5ca900c 100644 --- a/default_config_generator.go +++ b/default_config_generator.go @@ -24,6 +24,21 @@ func NewDefaultConfigGenerator(logs scribe.Emitter) DefaultConfigGenerator { } func (g DefaultConfigGenerator) Generate(config Configuration) error { + g.logs.Process("Check: %s", config.NGINXConfLocation) + if info, err := os.Stat(config.NGINXConfLocation); err == nil { + g.logs.Subprocess("Configuration file already exists") + if info.Size() > 0 { + userConf, err := os.ReadFile(config.NGINXConfLocation) + if err != nil { + return err + } + + g.logs.Subprocess("Set configuration (in %s) as template", config.NGINXConfLocation) + + DefaultConfigTemplate = string(userConf) + } + } + g.logs.Process("Generating %s", config.NGINXConfLocation) t := template.Must(template.New("template.conf").Delims("$((", "))").Parse(DefaultConfigTemplate)) diff --git a/default_config_generator_test.go b/default_config_generator_test.go index 3d453033..0e72a7cc 100644 --- a/default_config_generator_test.go +++ b/default_config_generator_test.go @@ -360,6 +360,53 @@ error_log stderr; `))) }) + context("existing nginx.conf, BP_WEB_SERVER=nginx", func() { + it("doesn't overwrite an existing nginx.conf", func() { + confPath := filepath.Join(workingDir, "nginx-exists-non-empty.conf") + Expect(os.WriteFile(confPath, []byte("port {{port}};"), 0644)).To(Succeed()) + + Expect(generator.Generate(nginx.Configuration{ + NGINXConfLocation: confPath, + WebServer: "htdocs", + })).To(Succeed()) + + Expect(confPath). + To(matchers.BeAFileMatching(ContainSubstring(string("port {{port}}")))) + + Expect(os.Remove(confPath)).To(Succeed()) + }) + + it("sets variables inside the supplied nginx.conf", func() { + confPath := filepath.Join(workingDir, "nginx-exists.conf") + Expect(os.WriteFile(confPath, []byte("root $(( .WebServerRoot -));"), 0644)).To(Succeed()) + + Expect(generator.Generate(nginx.Configuration{ + NGINXConfLocation: confPath, + WebServerRoot: "/a/very/specific/path", + })).To(Succeed()) + + Expect(confPath). + To(matchers.BeAFileMatching(ContainSubstring("root /a/very/specific/path;"))) + + Expect(os.Remove(confPath)).To(Succeed()) + }) + + it("overwrites an empty nginx.conf", func() { + confPath := filepath.Join(workingDir, "nginx-exists-empty.conf") + Expect(os.WriteFile(confPath, []byte(""), 0644)).To(Succeed()) + + Expect(generator.Generate(nginx.Configuration{ + NGINXConfLocation: confPath, + })).To(Succeed()) + + // check that there's anything inthere + Expect(confPath). + To(matchers.BeAFileMatching(BeEquivalentTo(`root {{ env "APP_ROOT" }};`))) + + Expect(os.Remove(confPath)).To(Succeed()) + }) + }) + context("failure cases", func() { context("destination file already exists and it's read-only", func() { it.Before(func() {