Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Added a github action file
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
pieterclaerhout committed May 28, 2020
1 parent dae68ef commit d93bf4a
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ go-james new --path=<target path> \
--description=<description of your project> \
--copyright=<copyright of your project> \
[--with-git] \
[--with-docker] \
[--with-github-action] \
[--overwrite]
```

Expand Down Expand Up @@ -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
Expand Down
27 changes: 18 additions & 9 deletions cmd/creator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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("")
Expand Down
35 changes: 26 additions & 9 deletions internal/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
85 changes: 85 additions & 0 deletions internal/creator/github_action.go
Original file line number Diff line number Diff line change
@@ -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/[email protected]
- 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
}

0 comments on commit d93bf4a

Please sign in to comment.