Skip to content

Commit a16eebd

Browse files
committed
More safetey in overriding default template files in non-empty spaces
1 parent baeb688 commit a16eebd

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

server/cmd/server.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
_ "embed"
5-
"errors"
65
"fmt"
76
"html/template"
87
"io/fs"
@@ -183,6 +182,10 @@ func buildConfig(bundledFiles fs.FS, args []string, buildTime string) *server.Se
183182
bundlePathDate = time.Now()
184183
}
185184

185+
// Ensure template files exist before wrapping with base_fs fallthrough,
186+
// because the fallthrough layer includes .md files that would make the space appear non-empty.
187+
ensureIndexAndConfig(spacePrimitives, rootSpaceConfig.IndexPage)
188+
186189
serverConfig.ClientBundle = server.NewReadOnlyFallthroughSpacePrimitives(bundledFiles, "client", bundlePathDate, nil)
187190
rootSpaceConfig.SpacePrimitives = server.NewReadOnlyFallthroughSpacePrimitives(bundledFiles, "base_fs", bundlePathDate, spacePrimitives)
188191

@@ -236,9 +239,6 @@ func buildConfig(bundledFiles fs.FS, args []string, buildTime string) *server.Se
236239
rootSpaceConfig.ShellBackend = server.NewNotSupportedShell()
237240
}
238241

239-
// Ensure at least the index page and config page exist
240-
ensureIndexAndConfig(rootSpaceConfig)
241-
242242
return serverConfig
243243
}
244244

@@ -248,25 +248,28 @@ var indexPageContent []byte
248248
//go:embed space_template/CONFIG.md
249249
var configPageContent []byte
250250

251-
func ensureIndexAndConfig(rootSpaceConfig *server.SpaceConfig) {
252-
// Index page first
253-
indexPagePath := fmt.Sprintf("%s.md", rootSpaceConfig.IndexPage)
254-
_, err := rootSpaceConfig.SpacePrimitives.GetFileMeta(indexPagePath)
255-
if err == server.ErrNotFound {
256-
log.Printf("Index page %s does not yet exist, creating...", indexPagePath)
257-
if _, err := rootSpaceConfig.SpacePrimitives.WriteFile(indexPagePath, indexPageContent, nil); err != nil {
258-
log.Fatalf("Could not write index page %s: %v", indexPagePath, err)
259-
}
251+
func ensureIndexAndConfig(sp server.SpacePrimitives, indexPage string) {
252+
// Only create template files in truly empty spaces (no .md files yet)
253+
files, err := sp.FetchFileList()
254+
if err != nil {
255+
log.Printf("Warning: could not check space state: %v", err)
256+
return
260257
}
261-
// Now let's check for a CONFIG.md
262-
configPagePath := "CONFIG.md"
263-
_, err = rootSpaceConfig.SpacePrimitives.GetFileMeta(configPagePath)
264-
if errors.Is(err, server.ErrNotFound) {
265-
log.Printf("Config page %s does not yet exist, creating...", configPagePath)
266-
if _, err := rootSpaceConfig.SpacePrimitives.WriteFile(configPagePath, configPageContent, nil); err != nil {
267-
log.Fatalf("Could not write config page %s: %v", configPagePath, err)
258+
for _, f := range files {
259+
if strings.HasSuffix(f.Name, ".md") {
260+
// Space has existing content, don't inject templates
261+
return
268262
}
269263
}
264+
265+
indexPagePath := fmt.Sprintf("%s.md", indexPage)
266+
log.Printf("Empty space detected, creating %s and CONFIG.md", indexPagePath)
267+
if _, err := sp.WriteFile(indexPagePath, indexPageContent, nil); err != nil {
268+
log.Fatalf("Could not write index page %s: %v", indexPagePath, err)
269+
}
270+
if _, err := sp.WriteFile("CONFIG.md", configPageContent, nil); err != nil {
271+
log.Fatalf("Could not write config page: %v", err)
272+
}
270273
}
271274

272275
func findChrome() string {

0 commit comments

Comments
 (0)