Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pageship generate dockerfile #25

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions cmd/pageship/app/generate.go
Original file line number Diff line number Diff line change
@@ -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) {
kiootic marked this conversation as resolved.
Show resolved Hide resolved
var cfg config.Config
if len(testfs) > 0 { //for testing, copied from utils.go
loader := config.NewLoader(config.SiteConfigName)

cfg = config.DefaultConfig()
if err := loader.Load(testfs[0], &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()
if err != nil {
return err
}
_, err = f.Write([]byte(s))
if err != nil {
return err
}
return nil
},
}
56 changes: 56 additions & 0 deletions cmd/pageship/app/generate_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions cmd/pageship/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down