From 10ad82eee72deb0e3ae385e352e59c539433d333 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 11 Apr 2024 13:29:40 +0200 Subject: [PATCH] wip: feat: download hugo --- app.go | 104 +++++++++++++++++++++++-- components/templates.templ | 38 ++------- components/templates_templ.go | 141 +--------------------------------- frontend/index.html | 3 +- go.mod | 8 ++ go.sum | 32 ++++++++ main.go | 4 - wails.json | 3 + 8 files changed, 154 insertions(+), 179 deletions(-) diff --git a/app.go b/app.go index 7fec710..62b8f7b 100644 --- a/app.go +++ b/app.go @@ -1,11 +1,21 @@ package main import ( + "bytes" "context" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "os/user" + "path/filepath" "varia.zone/snackbar/components" "github.com/a-h/templ" + "github.com/codeclysm/extract/v3" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) @@ -24,23 +34,107 @@ func (a *App) startup(ctx context.Context) { a.ctx = ctx } -func (a App) domReady(ctx context.Context) { +func ensureDataDir(homeDir string) error { + paths := []string{ + filepath.Join(homeDir, ".local"), + filepath.Join(homeDir, ".local", "snackbar"), + filepath.Join(homeDir, ".local", "snackbar", "hugo"), + filepath.Join(homeDir, ".local", "snackbar", "sites"), + } + + for _, fpath := range paths { + if _, err := os.Stat(fpath); err != nil && os.IsNotExist(err) { + if err := os.Mkdir(fpath, 0764); err != nil { + return err + } + } + } + + return nil +} + +func ensureHugoBin(homeDir string) error { + hugoVersion := "0.124.1" + dataDir := filepath.Join(homeDir, ".local", "snackbar") + hugoDir := filepath.Join(dataDir, "hugo") + hugoBinPath := filepath.Join(hugoDir, "hugo") + hugoTarPath := filepath.Join(hugoDir, fmt.Sprintf("hugo-%s.tar.gz", hugoVersion)) + url := fmt.Sprintf("https://github.com/gohugoio/hugo/releases/download/v%s/hugo_extended_%s_linux-amd64.tar.gz", hugoVersion, hugoVersion) + + if _, err := os.Stat(hugoBinPath); err != nil && os.IsNotExist(err) { + if err := httpGetFile(hugoTarPath, url); err != nil { + return err + } + } + + fpath, err := ioutil.ReadFile(hugoTarPath) + if err != nil { + return err + } + if err := extract.Gz(context.TODO(), bytes.NewBuffer(fpath), hugoDir, nil); err != nil { + return err + } + + return nil +} + +func httpGetFile(filepath, url string) error { + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return err + } + + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + + return nil } -func (a *App) beforeClose(ctx context.Context) (prevent bool) { - return false +func initialise(w http.ResponseWriter, r *http.Request) { + usr, err := user.Current() + if err != nil { + w.Write([]byte(fmt.Sprintf("unable to determine user: %s", err))) + } + + if err := ensureDataDir(usr.HomeDir); err != nil { + w.Write([]byte(fmt.Sprintf("unable to create data directory: %s", err))) + } + + if err := ensureHugoBin(usr.HomeDir); err != nil { + w.Write([]byte(fmt.Sprintf("unable to download hugo binary: %s", err))) + } + + // TODO: is there a Hugo site already? + + templ.Handler(components.NewSiteInput()).ServeHTTP(w, r) } -func (a *App) shutdown(ctx context.Context) { +func hugoNewSite(w http.ResponseWriter, r *http.Request) { + log.Printf("NEW SITE BEING CREATED: %s", r.FormValue("site-name")) } // NewRouter creates a new web router. func NewRouter() *chi.Mux { router := chi.NewRouter() + router.Use(middleware.Logger) router.Use(middleware.Recoverer) - router.Get("/", templ.Handler(components.NewSiteForm("/")).ServeHTTP) + router.Get("/init", initialise) + router.Post("/hugo/new", hugoNewSite) return router } diff --git a/components/templates.templ b/components/templates.templ index 4614303..4a6e5a4 100644 --- a/components/templates.templ +++ b/components/templates.templ @@ -1,33 +1,11 @@ package components -import "net/http" - -templ TextInput(name string, placeholder string, method string, endpoint string, hxTrigger string) { - -} - -templ NewSiteForm(endpoint string) { -
Please enter name of your new Hugo site below 👇
- @TextInput("name","Enter Name","post",endpoint,"keyup changed delay:100ms") -} - -func NewSite(w http.ResponseWriter, r *http.Request) { - if r.FormValue("name") != "" { - w.Write([]byte("New site created: " + r.FormValue("name") + " ✅" )) - return - } - w.Write([]byte("Please enter name of your new Hugo site below 👇")) +templ NewSiteInput() { +
Please enter name of your new Hugo site below 👇
+
+ +
} diff --git a/components/templates_templ.go b/components/templates_templ.go index 6635334..acd284e 100644 --- a/components/templates_templ.go +++ b/components/templates_templ.go @@ -10,9 +10,7 @@ import "context" import "io" import "bytes" -import "net/http" - -func TextInput(name string, placeholder string, method string, endpoint string, hxTrigger string) templ.Component { +func NewSiteInput() 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 { @@ -25,134 +23,7 @@ func TextInput(name string, placeholder string, method string, endpoint string, templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - var templ_7745c5c3_Var2 = []any{"input join-item input-bordered input-primary w-full max-w-xs px-4 py-3 my-8"} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var2...) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} - -func NewSiteForm(endpoint 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 { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var9 := templ.GetChildren(ctx) - if templ_7745c5c3_Var9 == nil { - templ_7745c5c3_Var9 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Please enter name of your new Hugo site below 👇
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = TextInput("name", "Enter Name", "post", endpoint, "keyup changed delay:100ms").Render(ctx, templ_7745c5c3_Buffer) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Please enter name of your new Hugo site below 👇
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -162,11 +33,3 @@ func NewSiteForm(endpoint string) templ.Component { return templ_7745c5c3_Err }) } - -func NewSite(w http.ResponseWriter, r *http.Request) { - if r.FormValue("name") != "" { - w.Write([]byte("New site created: " + r.FormValue("name") + " ✅")) - return - } - w.Write([]byte("Please enter name of your new Hugo site below 👇")) -} diff --git a/frontend/index.html b/frontend/index.html index e4a03ef..8a9ca36 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -8,6 +8,7 @@ -
+ +
diff --git a/go.mod b/go.mod index a6002df..3ce0c87 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ toolchain go1.21.1 require ( github.com/a-h/templ v0.2.648 + github.com/codeclysm/extract/v3 v3.1.1 github.com/go-chi/chi/v5 v5.0.12 github.com/wailsapp/wails/v2 v2.8.0 ) @@ -15,7 +16,12 @@ require ( github.com/go-ole/go-ole v1.3.0 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/h2non/filetype v1.1.3 // indirect github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect + github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect + github.com/juju/loggo v1.0.0 // indirect + github.com/klauspost/compress v1.15.13 // indirect + github.com/kr/text v0.2.0 // indirect github.com/labstack/echo/v4 v4.11.4 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/leaanthony/go-ansi-parser v1.6.1 // indirect @@ -29,6 +35,7 @@ require ( github.com/rivo/uniseg v0.4.7 // indirect github.com/samber/lo v1.39.0 // indirect github.com/tkrajina/go-reflector v0.5.6 // indirect + github.com/ulikunitz/xz v0.5.11 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect github.com/wailsapp/go-webview2 v1.0.10 // indirect @@ -38,6 +45,7 @@ require ( golang.org/x/net v0.24.0 // indirect golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect + gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect ) // replace github.com/wailsapp/wails/v2 v2.8.0 => /home/decentral1se/go/pkg/mod diff --git a/go.sum b/go.sum index 674da7c..b80de28 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,12 @@ github.com/a-h/templ v0.2.648 h1:A1ggHGIE7AONOHrFaDTM8SrqgqHL6fWgWCijQ21Zy9I= github.com/a-h/templ v0.2.648/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +github.com/arduino/go-paths-helper v1.2.0 h1:qDW93PR5IZUN/jzO4rCtexiwF8P4OIcOmcSgAYLZfY4= +github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck= github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY= github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= +github.com/codeclysm/extract/v3 v3.1.1 h1:iHZtdEAwSTqPrd+1n4jfhr1qBhUWtHlMTjT90+fJVXg= +github.com/codeclysm/extract/v3 v3.1.1/go.mod h1:ZJi80UG2JtfHqJI+lgJSCACttZi++dHxfWuPaMhlOfQ= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= @@ -14,8 +19,21 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= +github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck= github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU= +github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 h1:rhqTjzJlm7EbkELJDKMTU7udov+Se0xZkWmugr6zGok= +github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q= +github.com/juju/loggo v1.0.0 h1:Y6ZMQOGR9Aj3BGkiWx7HBbIx6zNwNkxhVNOHU2i1bl0= +github.com/juju/loggo v1.0.0/go.mod h1:NIXFioti1SmKAlKNuUwbMenNdef59IF52+ZzuOmHYkg= +github.com/juju/testing v0.0.0-20200510222523-6c8c298c77a0 h1:+WWUkhnTjV6RNOxkcwk79qrjeyHEHvBzlneueBsatX4= +github.com/juju/testing v0.0.0-20200510222523-6c8c298c77a0/go.mod h1:hpGvhGHPVbNBraRLZEhoQwFLMrjK8PSlO4D3nDjKYXo= +github.com/klauspost/compress v1.15.13 h1:NFn1Wr8cfnenSJSA46lLq4wHCcBzKTSjnBIexDMMOV0= +github.com/klauspost/compress v1.15.13/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= @@ -31,13 +49,18 @@ github.com/leaanthony/slicer v1.6.0 h1:1RFP5uiPJvT93TAHi+ipd3NACobkW53yUiBqZheE/ github.com/leaanthony/slicer v1.6.0/go.mod h1:o/Iz29g7LN0GqH3aMjWAe90381nyZlDNquK+mtH2Fj8= github.com/leaanthony/u v1.1.1 h1:TUFjwDGlNX+WuwVEzDqQwC2lOv0P4uhTQw7CMFdiK7M= github.com/leaanthony/u v1.1.1/go.mod h1:9+o6hejoRljvZ3BzdYlVL0JYCwtnAsVuN9pVTQcaRfI= +github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= +github.com/mattn/go-colorable v0.0.6/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -53,6 +76,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tkrajina/go-reflector v0.5.6 h1:hKQ0gyocG7vgMD2M3dRlYN6WBBOmdoOzJ6njQSepKdE= github.com/tkrajina/go-reflector v0.5.6/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4= +github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= +github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= @@ -83,5 +108,12 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v1.0.0-20160105164936-4f90aeace3a2/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= +gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 0c62eed..d9eb81a 100644 --- a/main.go +++ b/main.go @@ -33,10 +33,6 @@ func main() { return router }, }, - OnStartup: app.startup, - OnDomReady: app.domReady, - OnBeforeClose: app.beforeClose, - OnShutdown: app.shutdown, Bind: []interface{}{ app, }, diff --git a/wails.json b/wails.json index 04d0e3e..afcb446 100644 --- a/wails.json +++ b/wails.json @@ -6,6 +6,9 @@ "frontend:build": "npm run build", "frontend:dev:watcher": "npm run dev", "frontend:dev:serverUrl": "auto", + "preBuildHooks": { + "*/*": "templ generate" + }, "author": { "name": "decentral1se", "email": "cellarspoon@riseup.net"