Skip to content

Commit

Permalink
relayout mir's help toolkit code
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 2, 2020
1 parent cf536ff commit ddf5554
Show file tree
Hide file tree
Showing 26 changed files with 334 additions and 188 deletions.
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ ci: misspell vet test

.PHONY: build
build: fmt
go build -o mir cmd/mir.go
go build -o mir mirc/main.go

.PHONY: generate
generate:
-rm -f generator/templates_gen.go
-rm -f cmd/create/templates_gen.go
go generate generator/templates.go
go generate cmd/create/templates.go
$(GOFMT) -w generator/templates.go
$(GOFMT) -w cmd/create/templates.go
$(GOFMT) -w generator/templates_gen.go

.PHONY: test
test: fmt
Expand Down
44 changes: 0 additions & 44 deletions cmd/create/create.go

This file was deleted.

74 changes: 0 additions & 74 deletions cmd/mir.go

This file was deleted.

6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
module github.com/alimy/mir/v2

go 1.12

require (
github.com/gin-gonic/gin v1.4.0
github.com/onsi/ginkgo v1.10.2
github.com/onsi/gomega v1.7.0
)
54 changes: 0 additions & 54 deletions go.sum

This file was deleted.

1 change: 1 addition & 0 deletions mirc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/mir
18 changes: 18 additions & 0 deletions mirc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GOFILES := $(shell find . -name "*.go" -type f)

LDFLAGS += -X "gitbus.com/exlab/zim-ss/version.BuildTime=$(shell date -u '+%Y-%m-%d %I:%M:%S %Z')"
LDFLAGS += -X "gitbus.com/exlab/zim-ss/version.GitHash=$(shell git rev-parse HEAD)"

.PHONY: build
build: fmt
go build -ldflags '$(LDFLAGS)' -o mir main.go

.PHONY: generate
generate:
-rm -f cmd/templates_gen.go
go generate cmd/templates.go
$(GOFMT) -w cmd/templates_gen.go

.PHONY: fmt
fmt:
$(GOFMT) -w $(GOFILES)
7 changes: 7 additions & 0 deletions mirc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Mirc
Mir's help toolkit

### Release
```bash
% hub release create -m "mirc/{tag eg:v2.0.1} release" mirc/{tag eg:v2.0.1}
```
83 changes: 83 additions & 0 deletions mirc/cmd/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"log"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

var (
dstPath string
style string
)

func init() {
newCmd := &cobra.Command{
Use: "new",
Short: "create template project",
Long: "create template project",
Run: newRun,
}

// parse flags for agentCmd
newCmd.Flags().StringVarP(&dstPath, "dst", "d", ".", "genereted destination target directory")
newCmd.Flags().StringVarP(&style, "type", "t", "gin", "generated engine type style(eg: gin,chi,mux,httprout)")

// register agentCmd as sub-command
register(newCmd)
}

// newRun run new command
func newRun(_cmd *cobra.Command, _args []string) {
path, err := filepath.EvalSymlinks(dstPath)
if err != nil {
if os.IsNotExist(err) {
if !filepath.IsAbs(dstPath) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
path = filepath.Join(cwd, dstPath)
} else {
path = dstPath
}
} else {
log.Fatal(err)
}
}

tmpls, exist := tmplFiles[style]
if !exist {
log.Fatal("not exist style engine")
}

if err = genProject(path, tmpls); err != nil {
log.Fatal(err)
}
}

func genProject(dstPath string, tmpls map[string]string) error {
var (
err error
filePath, dirPath string
file *os.File
)

for fileName, assetName := range tmpls {
filePath = filepath.Join(dstPath, fileName)
dirPath = filepath.Dir(filePath)
if err = os.MkdirAll(dirPath, 0755); err != nil {
break
}
file, err = os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
break
}
if _, err = file.Write(MustAsset(assetName)); err != nil {
break
}
}
return err
}
30 changes: 30 additions & 0 deletions mirc/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import "github.com/spf13/cobra"

var (
rootCmd = &cobra.Command{
Use: "mir",
Short: "mir help toolkit",
Long: `mir help toolkit`,
}
)

// Setup set root command name,short-describe, long-describe
// return &cobra.Command to custom other options
func Setup(use, short, long string) *cobra.Command {
rootCmd.Use = use
rootCmd.Short = short
rootCmd.Long = long
return rootCmd
}

// register add sub-command
func register(cmd *cobra.Command) {
rootCmd.AddCommand(cmd)
}

// Execute start application
func Execute() {
rootCmd.Execute()
}
6 changes: 2 additions & 4 deletions cmd/create/templates.go → mirc/cmd/templates.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package create

import "github.com/alimy/mir/v2/core"
package cmd

//go:generate go-bindata -nomemcopy -pkg=${GOPACKAGE} -ignore=README.md -prefix=templates -debug=false -o=templates_gen.go templates/...

var tmplFiles = map[string]map[string]string{
core.EngineGin: {
"gin": {
"Makefile": "makefile.tmpl",
"README.md": "readme.tmpl",
"go.mod": "gin_go_mod.tmpl",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/create/templates_gen.go → mirc/cmd/templates_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions mirc/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/alimy/mir/mirc/v2/version"
"github.com/spf13/cobra"
)

func init() {
versionCmd := &cobra.Command{
Use: "version",
Short: "show version information",
Long: "show version information",
Run: versionRun,
}
register(versionCmd)
}

func versionRun(_cmd *cobra.Command, _args []string) {
fmt.Printf("v%s\nBuildTime:%s\nBuildGitSHA:%s\n",
version.MircVer, version.BuildTime, version.GitHash)
}
Loading

0 comments on commit ddf5554

Please sign in to comment.