Skip to content

Commit

Permalink
🎨 code format
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing committed May 24, 2022
1 parent a77c943 commit 9678fa7
Show file tree
Hide file tree
Showing 18 changed files with 266 additions and 82 deletions.
31 changes: 16 additions & 15 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ on:
- 'v*'

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup
uses: actions/setup-go@v2
with:
go-version: '1.17'
- name: Tidy
run: go mod tidy
- name: Test
run: go test -v ./...
# test:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Setup
# uses: actions/setup-go@v2
# with:
# go-version: '1.17'
# - name: Tidy
# run: go mod tidy
# - name: Test
# run: go test -v ./...
tag:
runs-on: ubuntu-latest
needs: test
# needs: test
if: startsWith(github.ref, 'refs/tags/')
outputs:
tag: ${{ steps.tag.outputs.tag }}
Expand Down Expand Up @@ -53,10 +53,11 @@ jobs:
GOOS: ${{ matrix.os }}
GOARCH: amd64
run: |
echo ${{ needs.tag.outputs.tag }} > version/version
if [ ${GOOS} == windows ];then
export app=generate-tool.exe
export app=mss-boot-generator.exe
else
export app=generate-tool
export app=mss-boot-generator
fi
go build -o ${app} .
chmod +x ${app}
Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ hs_err_pid*
*White*
workspace
src
generate-tool
mss-boot-generator

### config local ###
*-dev.yaml
Expand All @@ -63,4 +63,7 @@ generate-tool

### demo template ###
*demo*
default
default

### version ###
version/version
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ PROJECT:=micro-service-gen-tool
.PHONY: build

build-linux, build:
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o generate-tool main.go
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o mss-boot-generator main.go
build-windows:
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -o generate-tool.exe main.go
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -o mss-boot-generator.exe main.go
build-darwin:
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -o generate-tool main.go
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -o mss-boot-generator main.go
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# micro-service-gen-tool
White Matrix Micro Service Generate CLI Tool
# mss-boot-generator
MSS-Boot Micro Service Generate CLI Tool

## usage
### template demo
https://github.com/lwnmengjing/template-demo
### template set
https://github.com/mss-boot-io/mss-boot-template

### ignore file
- .templateignore: will not scan
- .templateparseignore: will scan, but not parse
### linux
```bazaar
generate-tool
mss-boot-generator
Usage:
generate-tool [flags]
generate-tool [command]
mss-boot-generator [flags]
mss-boot-generator [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
install Install generate-tool
install Install mss-boot-generator
run Start generate project
update Install generate-tool
update Install mss-boot-generator
version Get version info
Flags:
-h, --help help for generate-tool
-h, --help help for mss-boot-generator
Use "generate-tool [command] --help" for more information about a command.
Use "mss-boot-generator [command] --help" for more information about a command.
```
20 changes: 10 additions & 10 deletions cmd/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (

"github.com/spf13/cobra"

"github.com/mss-boot-io/micro-service-gen-tool/cmd/install"
"github.com/mss-boot-io/micro-service-gen-tool/cmd/run"
"github.com/mss-boot-io/micro-service-gen-tool/cmd/update"
"github.com/mss-boot-io/micro-service-gen-tool/cmd/version"
"github.com/mss-boot-io/micro-service-gen-tool/pkg"
v "github.com/mss-boot-io/micro-service-gen-tool/version"
"github.com/mss-boot-io/mss-boot-generator/cmd/install"
"github.com/mss-boot-io/mss-boot-generator/cmd/run"
"github.com/mss-boot-io/mss-boot-generator/cmd/update"
"github.com/mss-boot-io/mss-boot-generator/cmd/version"
"github.com/mss-boot-io/mss-boot-generator/pkg"
v "github.com/mss-boot-io/mss-boot-generator/version"
)

var rootCmd = &cobra.Command{
Use: "generate-tool",
Short: "generate-tool",
Use: "mss-boot-generator",
Short: "mss-boot-generator",
SilenceUsage: true,
Long: `generate-tool`,
Long: `mss-boot-generator`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
tip()
Expand All @@ -34,7 +34,7 @@ var rootCmd = &cobra.Command{
}

func tip() {
usageStr := `欢迎使用 ` + pkg.Green(`generate-tool `+v.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
usageStr := `欢迎使用 ` + pkg.Green(`mss-boot-generator `+v.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
fmt.Printf("%s\n", usageStr)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package install
import (
"github.com/spf13/cobra"

"github.com/mss-boot-io/micro-service-gen-tool/pkg"
"github.com/mss-boot-io/mss-boot-generator/pkg"
)

var (
StartCmd = &cobra.Command{
Use: "install",
Short: "Install generate-tool",
Example: "generate-tool install",
Short: "Install mss-boot-generator",
Example: "mss-boot-generator install",
PreRun: func(cmd *cobra.Command, args []string) {

},
Expand Down
58 changes: 39 additions & 19 deletions cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package run
import (
"fmt"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -12,14 +14,15 @@ import (
"github.com/google/uuid"
"github.com/spf13/cobra"

"github.com/mss-boot-io/micro-service-gen-tool/pkg"
"github.com/mss-boot-io/mss-boot-generator/pkg"
)

var (
StartCmd = &cobra.Command{
configFile string
StartCmd = &cobra.Command{
Use: "run",
Short: "Start generate project",
Example: "generate-tool run",
Example: "mss-boot-generator run",
PreRun: func(cmd *cobra.Command, args []string) {
pkg.Upgrade(true)
pre()
Expand All @@ -36,6 +39,10 @@ var (
defaultTemplate = "https://github.com/WhiteMatrixTech/matrix-microservice-template.git"
)

func init() {
StartCmd.Flags().StringVarP(&configFile, "config", "c", "", "config file")
}

func pre() {
if os.Getenv("MICRO_DEFAULT_TEMPLATE") != "" {
defaultTemplate = os.Getenv("MICRO_DEFAULT_TEMPLATE")
Expand Down Expand Up @@ -139,33 +146,46 @@ SUBPATH:
goto SUBPATH
}
projectName := "default"
fmt.Printf("project name(default:%s)", pkg.Yellow(projectName))
fmt.Printf("project name(default:%s): ", pkg.Yellow(projectName))
_, _ = fmt.Scanf("%s", &projectName)
keys, err := pkg.GetParseFromTemplate(templateWorkspace, subPath)
if err != nil {
log.Fatalln(err)
params := make(map[string]interface{})
if configFile != "" {
rb, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalln(err)
}
err = yaml.Unmarshal(rb, &params)
if err != nil {
log.Fatalln(err)
}
params["service"] = projectName
} else {
keys, err := pkg.GetParseFromTemplate(templateWorkspace, subPath)
if err != nil {
log.Fatalln(err)
}
for key := range keys {
var value string
BACK:
fmt.Printf("%s: ", pkg.Yellow(key))
_, _ = fmt.Scanf("%s", &value)
if value == "" {
goto BACK
}
keys[key] = value
params[key] = value
}
}

fmt.Println(pkg.Magenta("please config your param's value"))

for key := range keys {
var value string
BACK:
fmt.Printf("%s: ", pkg.Yellow(key))
_, _ = fmt.Scanf("%s", &value)
if value == "" {
goto BACK
}
keys[key] = value
}

err = pkg.Generate(&pkg.TemplateConfig{
Service: subPath,
TemplateLocal: templateWorkspace,
CreateRepo: false,
Destination: filepath.Join(".", projectName),
Github: nil,
Params: keys,
Params: params,
Ignore: nil,
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package update
import (
"github.com/spf13/cobra"

"github.com/mss-boot-io/micro-service-gen-tool/pkg"
"github.com/mss-boot-io/mss-boot-generator/pkg"
)

var (
StartCmd = &cobra.Command{
Use: "update",
Short: "Install generate-tool",
Example: "generate-tool update",
Short: "Install mss-boot-generator",
Example: "mss-boot-generator update",
PreRun: func(cmd *cobra.Command, args []string) {

},
Expand Down
6 changes: 3 additions & 3 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package version
import (
"github.com/spf13/cobra"

"github.com/mss-boot-io/micro-service-gen-tool/pkg"
"github.com/mss-boot-io/micro-service-gen-tool/version"
"github.com/mss-boot-io/mss-boot-generator/pkg"
"github.com/mss-boot-io/mss-boot-generator/version"
)

var (
StartCmd = &cobra.Command{
Use: "version",
Short: "Get version info",
Example: "generate-tool version",
Example: "mss-boot-generator version",
PreRun: func(cmd *cobra.Command, args []string) {
pkg.Upgrade(true)
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mss-boot-io/micro-service-gen-tool
module github.com/mss-boot-io/mss-boot-generator

go 1.17

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "github.com/mss-boot-io/micro-service-gen-tool/cmd"
import "github.com/mss-boot-io/mss-boot-generator/cmd"

func main() {
cmd.Execute()
Expand Down
6 changes: 3 additions & 3 deletions pkg/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ type release struct {
TagName string `json:"tag_name"`
}

// GetLatestVersion get generate-tool version
// GetLatestVersion get mss-boot-generator version
func GetLatestVersion() string {
// get version
data, err := http.Get("https://api.github.com/repos/WhiteMatrixTech/micro-service-gen-tool/releases/latest")
data, err := http.Get("https://api.github.com/repos/mss-boot-io/mss-boot-generator/releases/latest")
if err != nil {
log.Fatal(err.Error())
}
Expand All @@ -79,7 +79,7 @@ func downloadLatest() string {
fmt.Println("the latest version is", version)
filename := runtime.GOOS + "_" + runtime.GOARCH + ".tar.gz"
// download latest package
downloadUrl := fmt.Sprintf("https://github.chainide.com/WhiteMatrixTech/micro-service-gen-tool/releases/download/%s/%s", version, filename)
downloadUrl := fmt.Sprintf("https://github.chainide.com/mss-boot-io/mss-boot-generator/releases/download/%s/%s", version, filename)
fmt.Println("download package from ", downloadUrl)
resp, err := http.Get(downloadUrl)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package pkg

import (
"bytes"
"fmt"
"github.com/mss-boot-io/mss-boot-generator/temp_func"
"io/ioutil"
"log"
"mime"
Expand Down Expand Up @@ -61,7 +63,7 @@ func Generate(c *TemplateConfig) (err error) {
c.Github = nil
}
//delete destinationPath
_ = os.RemoveAll(c.Destination)
//_ = os.RemoveAll(c.Destination)

t := &Generator{
SubPath: c.Service,
Expand Down Expand Up @@ -187,7 +189,7 @@ func (e *Generator) TraverseFunc(path string, f os.DirEntry, err error) error {
}
}
templatePath := path
t := template.New(path)
t := template.New(path).Funcs(temp_func.DefaultFuncMap)
t = template.Must(t.Parse(path))
var buffer bytes.Buffer
if err = t.Execute(&buffer, e.Cfg); err != nil {
Expand Down Expand Up @@ -236,7 +238,8 @@ func (e *Generator) TraverseFunc(path string, f os.DirEntry, err error) error {
return err
}
buffer = bytes.Buffer{}
t = template.New(path + "[file]")
fmt.Println(templatePath)
t = template.New(path + "[file]").Funcs(temp_func.DefaultFuncMap)
t = template.Must(t.Parse(string(rb)))
if err = t.Execute(&buffer, e.Cfg); err != nil {
log.Printf("path %s parse error\n", templatePath)
Expand Down
Loading

0 comments on commit 9678fa7

Please sign in to comment.