diff --git a/cmd/pageship/app/generate.go b/cmd/pageship/app/generate.go new file mode 100644 index 0000000..770d1c2 --- /dev/null +++ b/cmd/pageship/app/generate.go @@ -0,0 +1,111 @@ +package app + +import ( + "bytes" + "io/fs" + "os" + "text/template" + + "github.com/oursky/pageship/internal/config" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(generateCmd) + generateCmd.AddCommand(generateDockerfileCmd) +} + +var generateCmd = &cobra.Command{ + Use: "generate [command]", + Short: "Generate files", + Args: cobra.NoArgs, //if unknown command, will return error just like main pageship command + RunE: func(cmd *cobra.Command, args []string) error { + cmd.Help() //show help if no subcommand supplied + return nil + }, +} + +var Version = "" + +type DockerfileTemplate struct { + Version string + PublicPath string +} + +var dockerfileTemplateString = `FROM ghcr.io/oursky/pageship:{{if .Version}}v{{.Version}}{{else}}dev{{end}} +EXPOSE 8000 +COPY ./pageship.toml /var/pageship +COPY ./{{.PublicPath}} /var/pageship/{{.PublicPath}} + +# INSTRUCTIONS: +# 1. install docker (if it is not installed yet) +# 2. open a terminal and navigate to folder containing pageship.toml +# 3. run in terminal: +# pageship generate dockerfile +# 4. build the image: +# docker build -t IMAGETAG . +# 5. run the container: +# docker run -d --name CONTAINERNAME -p PORT:8000 IMAGETAG +# 6. visit in browser (URL): +# localhost:PORT` + +func generateContent(testfs fs.FS) (string, error) { + var cfg config.Config + if testfs != nil { //for testing, copied from utils.go + loader := config.NewLoader(config.SiteConfigName) + + cfg = config.DefaultConfig() + if err := loader.Load(testfs, &cfg); err != nil { + return "", err + } + + cfg.SetDefaults() + } else { + config, err := loadConfig(".") + cfg = *config + if err != nil { + return "", err + } + } + + df := DockerfileTemplate{Version: Version, PublicPath: cfg.Site.Public} + + tmpl, err := template.New("Dockerfile").Parse(dockerfileTemplateString) + if err != nil { + return "", err + } + + var b bytes.Buffer + err = tmpl.Execute(&b, df) + if err != nil { + return "", err + } + return b.String(), nil +} + +var generateDockerfileCmd = &cobra.Command{ + Use: "dockerfile", + Short: "Generate Dockerfile", + RunE: func(cmd *cobra.Command, args []string) error { + if Version == "dev" { + Version = "" + } + + f, err := os.Create("Dockerfile") + if err != nil { + return err + } + defer f.Close() + + Info("generating Dockerfile...") + s, err := generateContent(nil) + if err != nil { + return err + } + _, err = f.Write([]byte(s)) + if err != nil { + return err + } + return nil + }, +} diff --git a/cmd/pageship/app/generate_test.go b/cmd/pageship/app/generate_test.go new file mode 100644 index 0000000..83544d8 --- /dev/null +++ b/cmd/pageship/app/generate_test.go @@ -0,0 +1,56 @@ +package app + +import ( + "testing" + "testing/fstest" + + "github.com/stretchr/testify/assert" +) + +func TestGenerate(t *testing.T) { + Version = "1.2.3" + + var testFiles fstest.MapFS = make(map[string]*fstest.MapFile) + testFiles["pageship.toml"] = &fstest.MapFile{Data: []byte(`[app] +id = "pageship-test" + +team = [] + +[app.deployments] +# ttl = "24h" +# access = [] + +[[app.sites]] +name = "main" + +# [[app.sites]] +# name = "dev" + +# [[app.sites]] +# name = "staging" + +[site] +public = "dist" + +# access = [] +`)} + + s, err := generateContent(testFiles) + assert.Empty(t, err) + assert.Equal(t, `FROM ghcr.io/oursky/pageship:v1.2.3 +EXPOSE 8000 +COPY ./pageship.toml /var/pageship +COPY ./dist /var/pageship/dist + +# INSTRUCTIONS: +# 1. install docker (if it is not installed yet) +# 2. open a terminal and navigate to folder containing pageship.toml +# 3. run in terminal: +# pageship generate dockerfile +# 4. build the image: +# docker build -t IMAGETAG . +# 5. run the container: +# docker run -d --name CONTAINERNAME -p PORT:8000 IMAGETAG +# 6. visit in browser (URL): +# localhost:PORT`, s) +} diff --git a/cmd/pageship/main.go b/cmd/pageship/main.go index 62ee8a0..360a603 100644 --- a/cmd/pageship/main.go +++ b/cmd/pageship/main.go @@ -6,7 +6,10 @@ import ( "github.com/oursky/pageship/cmd/pageship/app" ) +var version = "dev" //https://goreleaser.com/cookbooks/using-main.version/ + func main() { + app.Version = version if err := app.Execute(); err != nil { app.Error("%s", err) os.Exit(1) diff --git a/go.mod b/go.mod index e3a4e30..45da941 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,12 @@ go 1.20 require ( github.com/MicahParks/keyfunc/v2 v2.1.0 + github.com/andybalholm/brotli v1.1.0 github.com/caddyserver/certmagic v0.17.2 github.com/carlmjohnson/versioninfo v0.22.4 github.com/dustin/go-humanize v1.0.1 github.com/fatih/color v1.15.0 + github.com/foxcpp/go-mockdns v1.0.0 github.com/fsnotify/fsnotify v1.6.0 github.com/go-chi/chi/v5 v5.0.8 github.com/go-playground/validator/v10 v10.14.0 @@ -50,7 +52,6 @@ require ( github.com/Azure/go-autorest v14.2.0+incompatible // indirect github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect - github.com/andybalholm/brotli v1.1.0 // indirect github.com/aws/aws-sdk-go v1.44.314 // indirect github.com/aws/aws-sdk-go-v2 v1.20.0 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.11 // indirect @@ -73,7 +74,6 @@ require ( github.com/aws/smithy-go v1.14.0 // indirect github.com/chzyer/readline v1.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/foxcpp/go-mockdns v1.0.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/go.sum b/go.sum index ad3070b..d1ba5d6 100644 --- a/go.sum +++ b/go.sum @@ -227,7 +227,6 @@ github.com/aws/smithy-go v1.14.0 h1:+X90sB94fizKjDmwb4vyl2cTTPXTE5E2G/1mjByb0io= github.com/aws/smithy-go v1.14.0/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=