From d93bf4a82994fa52eb8661854f181bfb4ca27a5d Mon Sep 17 00:00:00 2001 From: Pieter Claerhout Date: Thu, 28 May 2020 17:45:15 +0200 Subject: [PATCH] Added a github action file Fixes #99 --- .vscode/tasks.json | 2 +- README.md | 3 ++ cmd/creator/main.go | 27 ++++++---- internal/creator/creator.go | 35 +++++++++---- internal/creator/github_action.go | 85 +++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 19 deletions(-) create mode 100644 internal/creator/github_action.go diff --git a/.vscode/tasks.json b/.vscode/tasks.json index be388d0..5626f15 100755 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -33,7 +33,7 @@ }, { "label": "go-james | create sample app", - "command": "go build -o build/go-james github.com/pieterclaerhout/go-james/cmd/go-james && rm -rf go-example && ./build/go-james new --path go-example --package github.com/pieterclaerhout/go-example --description 'My Project Description'", + "command": "go build -o build/go-james github.com/pieterclaerhout/go-james/cmd/go-james && rm -rf go-example && ./build/go-james new --path go-example --package github.com/pieterclaerhout/go-example --description 'My Project Description' --with-git --with-docker --with-github-action", "type": "shell", "group": "build", "problemMatcher": [ diff --git a/README.md b/README.md index 8ccf208..276ddff 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,8 @@ go-james new --path= \ --description= \ --copyright= \ [--with-git] \ + [--with-docker] \ + [--with-github-action] \ [--overwrite] ``` @@ -177,6 +179,7 @@ You can specify the following options: * `--copyright`: the copyright of the project, used for the readme * `--with-git`: if specified, a local Git repository will be created for the project and the source files will automatically be committed. * `--with-docker`: if specified, a sample Dockerfile and .dockerignore file will be created. +* `--with-github-action`: if specified, a sample Github Actions file will be created. * `--overwrite`: if the destination path already exists, overwrite it (be careful, the original folder will be replaced) ## Initializing an existing project diff --git a/cmd/creator/main.go b/cmd/creator/main.go index 899d875..6c538ac 100644 --- a/cmd/creator/main.go +++ b/cmd/creator/main.go @@ -68,6 +68,13 @@ var NewCmd = climax.Command{ Help: `Create a sample Dockerfile`, Variable: false, }, + { + Name: "with-github-action", + Short: "", + Usage: `--with-github-action`, + Help: `Create a sample Github Action workflow`, + Variable: false, + }, }, Handle: func(ctx climax.Context) int { @@ -79,17 +86,19 @@ var NewCmd = climax.Command{ overwrite := ctx.Is("overwrite") withGit := ctx.Is("with-git") withDocker := ctx.Is("with-docker") + withGithubAction := ctx.Is("with-github-action") tool := creator.Creator{ - Mode: creator.NewProject, - Path: path, - Package: packageName, - Name: name, - Description: description, - Copyright: copyright, - Overwrite: overwrite, - WithGit: withGit, - WithDocker: withDocker, + Mode: creator.NewProject, + Path: path, + Package: packageName, + Name: name, + Description: description, + Copyright: copyright, + Overwrite: overwrite, + WithGit: withGit, + WithDocker: withDocker, + WithGithubAction: withGithubAction, } executor := internal.NewExecutor("") diff --git a/internal/creator/creator.go b/internal/creator/creator.go index 7a6120d..cac1827 100644 --- a/internal/creator/creator.go +++ b/internal/creator/creator.go @@ -34,15 +34,16 @@ type Creator struct { common.Template common.Logging - Mode Mode - Path string - Package string - Name string - Description string - Copyright string - Overwrite bool - WithGit bool - WithDocker bool + Mode Mode + Path string + Package string + Name string + Description string + Copyright string + Overwrite bool + WithGit bool + WithDocker bool + WithGithubAction bool } // Execute executes the command @@ -120,6 +121,13 @@ func (creator Creator) Execute(project common.Project, cfg config.Config) error ) } + if creator.WithGithubAction { + steps = append( + steps, + creator.createGithubAction, + ) + } + if creator.WithGit { steps = append( steps, @@ -254,6 +262,15 @@ func (creator Creator) createDockerFile(project common.Project, cfg config.Confi } +func (creator Creator) createGithubAction(project common.Project, cfg config.Config) error { + + githubAction := newGithubAction(cfg) + + path := project.RelPath(".github", "workflows", "build.yaml") + return creator.WriteTextFileIfNotExists(path, githubAction.string()) + +} + func (creator Creator) createReadme(project common.Project, cfg config.Config) error { readme := newReadme(project, cfg) diff --git a/internal/creator/github_action.go b/internal/creator/github_action.go new file mode 100644 index 0000000..685308a --- /dev/null +++ b/internal/creator/github_action.go @@ -0,0 +1,85 @@ +package creator + +import ( + "github.com/pieterclaerhout/go-james/internal/config" +) + +const githubActionTemplate = `name: Build and Publish + +on: [push] + +jobs: + + build-test-staticcheck: + name: Build, Test and Check + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.14 + uses: actions/setup-go@v1 + with: + go-version: 1.14 + id: go + + - name: Environment Variables + uses: FranzDiebold/github-env-vars-action@v1.0.0 + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + with: + lfs: true + + - name: Restore Cache + uses: actions/cache@preview + id: cache + with: + path: ~/go/pkg + key: 1.14-${{ runner.os }}-${{ hashFiles('**/go.sum') }} + + - name: Get go-james + run: | + go get -u github.com/pieterclaerhout/go-james/cmd/go-james + + - name: Get dependencies + run: | + go get -v -t -d ./... + + - name: Build + run: | + export PATH=${PATH}:` + "`" + `go env GOPATH` + "`" + `/bin + go-james build + + - name: Test + run: | + export PATH=${PATH}:` + "`" + `go env GOPATH` + "`" + `/bin + go-james test + + - name: Staticcheck + run: | + export PATH=${PATH}:` + "`" + `go env GOPATH` + "`" + `/bin + go-james staticcheck + + - name: Package + run: | + export PATH=${PATH}:` + "`" + `go env GOPATH` + "`" + `/bin + go-james package + + - uses: actions/upload-artifact@v2 + name: Publish + with: + name: ${{ env.GITHUB_REPOSITORY_NAME }}-${{ env.GITHUB_SHA_SHORT }}-${{ env.GITHUB_REF_NAME }} + path: build/*.*` + +type githubAction struct { + text string +} + +func newGithubAction(cfg config.Config) githubAction { + return githubAction{ + text: githubActionTemplate, + } +} + +func (g githubAction) string() string { + return g.text +}