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

feat: new template golang-cli for repo-repo-scaffolding plugin #1231

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 35 additions & 12 deletions docs/plugins/repo-scaffolding.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,26 @@ This configuration is used for template render, It has default variables listed
}
```

## Outputs

This plugin has three outputs:

- `owner`
- `repo`
- `repoURL`


## Examples

### official scaffolding repo config

These repos are official scaffolding repo to use for `sourceRepo` config, You can use these repo directly or just create one for yourself.

| language | org | repo |
| ---- | ---- |---- |
| Golang | devstream-io | dtm-scaffolding-golang |
| Java Spring | spring-guides | gs-spring-boot |
| language | org | repo |
|-------------|---------------|----------------------------|
| Golang | devstream-io | dtm-scaffolding-golang |
| Golang | devstream-io | dtm-scaffolding-golang-cli |
| Java Spring | spring-guides | gs-spring-boot |


### Golang
Expand All @@ -111,6 +121,27 @@ tools:

This config will create `dtm-test-golang` repo for user test_owner in GitHub, and the variable ImageRepo will be used for template render.

### Golang CLI

```yaml
tools:
- name: repo-scaffolding
instanceID: golang-cli-scaffolding
options:
destinationRepo:
owner: test_owner
org: ""
repo: dtm-test-golang-cli
branch: main
repoType: github
sourceRepo:
org: devstream-io
repo: dtm-scaffolding-golang-cli
repoType: github
```

This config will create `dtm-test-golang-cli` repo for user test_owner in GitHub.

### Java Spring

```yaml
Expand All @@ -133,11 +164,3 @@ tools:
```

this config will create `dtm-test-java` repo for user test_owner in GitHub.

## Outputs

This plugin has three outputs:

- `owner`
- `repo`
- `repoURL`
46 changes: 34 additions & 12 deletions docs/plugins/repo-scaffolding.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,25 @@
}
```

## Outputs

这个插件有以下三个输出:

- `owner`
- `repo`
- `repoURL`

## 示例

### 官方支持脚手架项目

以下仓库是用于在 `sourceRepo` 设置的官方脚手架仓库,你可以使用这些仓库或者创建自己的脚手架仓库。

| language | org | repo |
| ---- | ---- |---- |
| Golang | devstream-io | dtm-scaffolding-golang |
| Java Spring | spring-guides | gs-spring-boot |
| language | org | repo |
|-------------|---------------|----------------------------|
| Golang | devstream-io | dtm-scaffolding-golang |
| Golang | devstream-io | dtm-scaffolding-golang-cli |
| Java Spring | spring-guides | gs-spring-boot |


### Golang
Expand All @@ -109,6 +118,27 @@ tools:

这个配置在 GitHub 为用于 test_owner 创建 `dtm-test-golang` 仓库,它的生成是基于 `devstream-io/dtm-scaffolding-golang` 官方 Golang 脚手架仓库和传入的变量 `ImageRepo`。

### Golang CLI

```yaml
tools:
- name: repo-scaffolding
instanceID: golang-cli-scaffolding
options:
destinationRepo:
owner: test_owner
org: ""
repo: dtm-test-golang-cli
branch: main
repoType: github
sourceRepo:
org: devstream-io
repo: dtm-scaffolding-golang-cli
repoType: github
```

这个配置在 GitHub 为用于 test_owner 创建 `dtm-test-golang-cli` 仓库,它的生成是基于 `devstream-io/dtm-scaffolding-golang-cli` 官方 Golang CLI 脚手架仓库。

### Java Spring

```yaml
Expand All @@ -131,11 +161,3 @@ tools:
```

这个配置会在 GitLab 为用户 test_owner 创建 `dtm-test-java` 仓库,使用的是 Spring 官方的 `spring-guides/gs-spring-boot` 仓库。

## Outputs

这个插件有以下三个输出:

- `owner`
- `repo`
- `repoURL`
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: goreleaser

on:
push:
tags:
- '*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v3
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
27 changes: 27 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

### VisualStudioCode template
.vscode/*

### JetBrains template
.idea/

### macOS template
.DS_Store

dist/
Empty file.
22 changes: 22 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/Makefile.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SHELL := /bin/bash
BASEDIR = $(shell pwd)

.PHONY: help
help:
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: install
install: ## Install dependencies
@go mod download
@go mod vendor

.PHONY: dev
dev: ## Run with Dev
@go run cmd/root.go

.PHONY: build
build: ## Build todomvc
@go build -o build/[[.AppName]] cmd/main.go

clean: ### Remove build dir
@rm -rf build
28 changes: 28 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# dtm-scaffolding-golang

This repo contains templates used by DevStream plugin "repo-scaffolding" (thereafter: the plugin).

This repo isn't intended to be used directly without DevStream. It should only be consumed by the plugin automatically.

The plugin (together with this repo of templates) can create a repo in GitHub and set up the project layout and initialize the reop with necessary files that are typical for a Go cli app. The followings can be created automatically:

- a Go cli app example (generated by [`cobra-cli init`](https://github.com/spf13/cobra-cli/blob/main/README.md))
- [GoReleaser-Actions workflow](https://github.com/goreleaser/goreleaser-action)(Once you push a tag, it will build and release your app automatically)
- `.gitignore`, generated by [Toptal | gitignore.io](https://www.toptal.com/developers/gitignore/api/go,vim,macos,visualstudiocode) with minimum changes
- Makefile, with install/dev/build/clean

## Usage

- Render all files using go template whose name end with `.tpl` suffix.
- Files whose name don't end with `.tpl` extension don't need to be rendered.
- subdirectory "helm/**_app_name_**" (the **_app_name_** part) should be rendered with `AppName`
- subdicrectory "cmd/**_app_name_**" (the **_app_name_** part) should be rendered with `AppName`

Example of required parameters to render these templates:

```yaml
AppName: my-hello-world
Repo:
Owner: ironcore864
Name: my-hello-world
```
21 changes: 21 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/README.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# [[.AppName]]

This is a repo for app [[.AppName]]; bootstrapped by DevStream.

By default, the automatically generated scaffolding contains:

- a piece of sample go cli app code using the [Cobra Commander Framework](https://github.com/spf13/cobra)
- [GoReleaser](https://goreleaser.com/) for building and releasing the app
- .gitignore
- Makefile

## Automatic Releases

Just push a tag to the repo and [GoReleaser](https://goreleaser.com/) will build and release the app.

For example, to release version 0.1.0, run:

```git
git tag -a v0.1.0 -m "First release"
git push origin v0.1.0
```
51 changes: 51 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/cmd/root.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "[[.AppName]]",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.myapp.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


9 changes: 9 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/go.mod.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/[[.Repo.Owner]]/[[.Repo.Name]]

go 1.19

require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/go.sum.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 11 additions & 0 deletions staging/dtm-repo-scaffolding-golang-cli/main.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package main

import "github.com/[[.Repo.Owner]]/[[.Repo.Name]]/cmd"

func main() {
cmd.Execute()
}