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 4 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
64 changes: 64 additions & 0 deletions cmd/pageship/app/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package app

import (
"os"
"strings"

"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 = "dev"
var dockerfileFrom = "FROM ghcr.io/oursky/pageship:v"
var dockerfileExpose = "EXPOSE 8000"
var dockerfileCopy = "COPY . /var/pageship"
var dockerfileInstructions = `
# INSTRUCTIONS:
# 1. install docker (if it is not installed yet)
# 2. open a terminal and navigate to your static page folder
# 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`
toshinari123 marked this conversation as resolved.
Show resolved Hide resolved

func generateContent() string {
dockerfileFromVersion := dockerfileFrom + Version
return strings.Join([]string{dockerfileFromVersion, dockerfileExpose, dockerfileCopy, dockerfileInstructions}, "\n")
}

var generateDockerfileCmd = &cobra.Command{
Use: "dockerfile",
Short: "Generate dockerfile",
RunE: func(cmd *cobra.Command, args []string) error {
f, err := os.Create("dockerfile")
toshinari123 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
defer f.Close()

Info("generating dockerfile...")
_, err = f.Write([]byte(generateContent()))
if err != nil {
return err
}
return nil
},
}
26 changes: 26 additions & 0 deletions cmd/pageship/app/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerate(t *testing.T) {
Version = "1.2.3"
assert.Equal(t, `FROM ghcr.io/oursky/pageship:v1.2.3
EXPOSE 8000
COPY . /var/pageship

# INSTRUCTIONS:
# 1. install docker (if it is not installed yet)
# 2. open a terminal and navigate to your static page folder
# 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`, generateContent())
}
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