From ce117468ab6a3603a1a32821336cf3e4b41e29be Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 11 Apr 2024 15:32:32 +0200 Subject: [PATCH] feat: load existing sites --- app.go | 22 +++++++++++++++-- components/templates.templ | 17 +++++++++++-- components/templates_templ.go | 46 +++++++++++++++++++++++++++++++++-- frontend/index.html | 11 +++++++-- 4 files changed, 88 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index ada5fb3..65a6db4 100644 --- a/app.go +++ b/app.go @@ -91,6 +91,21 @@ func httpGetFile(filepath, url string) error { return nil } +func existingSites() ([]string, error) { + var sites []string + + siteNameFiles, err := os.ReadDir(SitesDir) + if err != nil { + return sites, err + } + + for _, siteNameFile := range siteNameFiles { + sites = append(sites, siteNameFile.Name()) + } + + return sites, nil +} + func initialise(w http.ResponseWriter, r *http.Request) { if err := ensureDataDir(); err != nil { w.Write([]byte(fmt.Sprintf("unable to create data directory: %s", err))) @@ -100,9 +115,12 @@ func initialise(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf("unable to download hugo binary: %s", err))) } - // TODO(d1): is there a Hugo site already? + sites, err := existingSites() + if err != nil { + w.Write([]byte(fmt.Sprintf("unable to list existing sites: %s", err))) + } - templ.Handler(components.NewSiteInput()).ServeHTTP(w, r) + templ.Handler(components.Homepage(sites)).ServeHTTP(w, r) } func hugoNewSite(w http.ResponseWriter, r *http.Request) { diff --git a/components/templates.templ b/components/templates.templ index 97c4852..7aa67b7 100644 --- a/components/templates.templ +++ b/components/templates.templ @@ -1,7 +1,20 @@ package components -templ NewSiteInput() { -
Please enter name of your new Hugo site below 👇
+import ( + "fmt" +) + +templ Homepage(sites []string) { + if len(sites) != 0 { +
Configure an existing Hugo site below 👇
+ + } + +
Create a new Hugo site below 👇
diff --git a/components/templates_templ.go b/components/templates_templ.go index d4fd032..bd8484d 100644 --- a/components/templates_templ.go +++ b/components/templates_templ.go @@ -10,7 +10,11 @@ import "context" import "io" import "bytes" -func NewSiteInput() templ.Component { +import ( + "fmt" +) + +func Homepage(sites []string) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -23,7 +27,45 @@ func NewSiteInput() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Please enter name of your new Hugo site below 👇
") + if len(sites) != 0 { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Configure an existing Hugo site below 👇
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Create a new Hugo site below 👇
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/frontend/index.html b/frontend/index.html index d6f0da0..14bcd09 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -8,7 +8,14 @@ -
-

Initialising Hugo...

+
+

Bootstrapping snackbar...

+