make handler generation use module name of mod file

This commit is contained in:
Shane C. 2024-11-06 15:50:13 -05:00
parent c8cc15eb35
commit 0ef25382b0
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
5 changed files with 35 additions and 22 deletions

View file

@ -14,6 +14,7 @@ import (
"gitlab.com/omnibill/linux" "gitlab.com/omnibill/linux"
"gitlab.com/omnibill/tui/confirmation" "gitlab.com/omnibill/tui/confirmation"
"gitlab.com/omnibill/tui/textinput" "gitlab.com/omnibill/tui/textinput"
"golang.org/x/mod/modfile"
"golang.org/x/text/cases" "golang.org/x/text/cases"
"golang.org/x/text/language" "golang.org/x/text/language"
"io/fs" "io/fs"
@ -26,6 +27,7 @@ import (
type templateData struct { type templateData struct {
PackagePath string PackagePath string
ModulePath string
UpperName string UpperName string
Name string Name string
Path string Path string
@ -77,6 +79,22 @@ var handlerCmd = &cobra.Command{
tmplData := templateData{} tmplData := templateData{}
cwd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
handlerDir := filepath.Join(cwd, "web/handlers")
viewDir := filepath.Join(cwd, "web/views")
modFile, err := os.ReadFile(cwd + "/go.mod")
if err != nil {
log.Fatalln(err)
}
modulePath := modfile.ModulePath(modFile)
tmplData.ModulePath = modulePath
inputHandlerPath, err := textinput.New(textinput.InputData{ inputHandlerPath, err := textinput.New(textinput.InputData{
Question: "Path of handler?", Question: "Path of handler?",
}) })
@ -171,14 +189,6 @@ var handlerCmd = &cobra.Command{
} }
tmplData.Patch = *hasPatch tmplData.Patch = *hasPatch
cwd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
handlerDir := filepath.Join(cwd, "web/handlers")
viewDir := filepath.Join(cwd, "web/views")
if *hasView { if *hasView {
for i, _ := range pathSplit { for i, _ := range pathSplit {
isLast := i == len(pathSplit)-1 isLast := i == len(pathSplit)-1
@ -295,13 +305,13 @@ var handlerCmd = &cobra.Command{
output, _ := strings.CutPrefix(folder, cwd) output, _ := strings.CutPrefix(folder, cwd)
for _, handlerImport := range imports { for _, handlerImport := range imports {
if handlerImport == "omnibill.net/omnibill"+output { if handlerImport == modulePath+output {
isFound = true isFound = true
} }
} }
if !isFound { if !isFound {
imports = append(imports, "omnibill.net/omnibill"+output) imports = append(imports, modulePath+output)
} }
} }

View file

@ -13,9 +13,9 @@ import (
"github.com/gofiber/fiber/v2/middleware/session" "github.com/gofiber/fiber/v2/middleware/session"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"go.uber.org/zap" "go.uber.org/zap"
"omnibill.net/omnibill/web/utils" "{{.ModulePath}}/web/utils"
{{- if .GetView }} {{- if .GetView }}
PAGE_VIEW "omnibill.net/omnibill/web/views/{{.Path}}" PAGE_VIEW "{{.ModulePath}}/web/views/{{.Path}}"
{{ end }} {{ end }}
) )

View file

@ -1,6 +1,6 @@
package {{.PackagePath}} package {{.PackagePath}}
import "omnibill.net/omnibill/web/views/layouts" import "{{.ModulePath}}/web/views/layouts"
templ Show() { templ Show() {
@layouts.Base(nil) { @layouts.Base(nil) {

19
go.mod
View file

@ -3,24 +3,32 @@ module omnibill.net/omnibill
go 1.23.2 go 1.23.2
require ( require (
github.com/evanw/esbuild v0.24.0
github.com/go-webauthn/webauthn v0.11.2 github.com/go-webauthn/webauthn v0.11.2
github.com/goccy/go-json v0.10.3
github.com/gofiber/fiber/v2 v2.52.5 github.com/gofiber/fiber/v2 v2.52.5
github.com/jackc/pgx/v5 v5.7.1
github.com/kr/pretty v0.3.1 github.com/kr/pretty v0.3.1
github.com/nicksnyder/go-i18n/v2 v2.4.1
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0 github.com/spf13/viper v1.19.0
github.com/uptrace/bun v1.2.5 github.com/uptrace/bun v1.2.5
github.com/uptrace/bun/dialect/pgdialect v1.2.5
gitlab.com/omnibill/linux v1.0.0
gitlab.com/omnibill/tui v1.0.1
go.uber.org/zap v1.27.0 go.uber.org/zap v1.27.0
golang.org/x/crypto v0.28.0
golang.org/x/mod v0.20.0
golang.org/x/text v0.19.0
) )
require ( require (
github.com/a-h/templ v0.2.793 // indirect github.com/a-h/templ v0.2.793 // indirect
github.com/andybalholm/brotli v1.1.1 // indirect github.com/andybalholm/brotli v1.1.1 // indirect
github.com/evanw/esbuild v0.24.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-git/go-billy/v5 v5.6.0 // indirect github.com/go-git/go-billy/v5 v5.6.0 // indirect
github.com/go-webauthn/x v0.1.14 // indirect github.com/go-webauthn/x v0.1.14 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gofiber/storage/postgres/v3 v3.0.0 // indirect github.com/gofiber/storage/postgres/v3 v3.0.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/go-tpm v0.9.1 // indirect github.com/google/go-tpm v0.9.1 // indirect
@ -29,7 +37,6 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect github.com/klauspost/compress v1.17.11 // indirect
@ -39,7 +46,6 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nicksnyder/go-i18n/v2 v2.4.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect github.com/rivo/uniseg v0.4.7 // indirect
@ -53,7 +59,6 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/ulikunitz/xz v0.5.12 // indirect github.com/ulikunitz/xz v0.5.12 // indirect
github.com/uptrace/bun/dialect/pgdialect v1.2.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.57.0 // indirect github.com/valyala/fasthttp v1.57.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect
@ -61,16 +66,12 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect github.com/x448/float16 v0.8.4 // indirect
gitlab.com/omnibill/archiver v1.0.0 // indirect gitlab.com/omnibill/archiver v1.0.0 // indirect
gitlab.com/omnibill/linux v1.0.0 // indirect
gitlab.com/omnibill/tui v1.0.1 // indirect
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/sync v0.8.0 // indirect golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

2
go.sum
View file

@ -147,6 +147,8 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjs
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=