Skip to content

Commit

Permalink
update command
Browse files Browse the repository at this point in the history
  • Loading branch information
Shipu committed Mar 1, 2022
1 parent 877c554 commit 17bb292
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 46 deletions.
20 changes: 13 additions & 7 deletions cmd/generate/crud.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package main
package generate

import (
"fmt"
pluralize "github.com/gertd/go-pluralize"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"log"
"path"
"runtime"
"strings"
)

var PackageName string

var cliCmd = &cobra.Command{
var PackageRoot string = "src"

var CrudCmd = &cobra.Command{
Use: "crud",
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(2),
RunE: crud,
}

func crud(cmd *cobra.Command, args []string) error {
PackageName = args[0]
name := args[1]

hasDir, _ := afero.DirExists(afero.NewOsFs(), "pkg")
hasDir, _ := afero.DirExists(afero.NewOsFs(), PackageRoot)
if !hasDir {
afero.NewOsFs().Mkdir("pkg", 0755)
afero.NewOsFs().Mkdir(PackageRoot, 0755)
}

fs := afero.NewBasePathFs(afero.NewOsFs(), "pkg/")
fs := afero.NewBasePathFs(afero.NewOsFs(), PackageRoot+"/")

createFolders(fs, name)
createFiles(fs, name)

log.Println("Successfully generated CRUD for " + Title(name))

return nil
}

Expand Down Expand Up @@ -59,7 +64,7 @@ func createFile(fs afero.Fs, name string, stubPath, filePath string) {
contents, _ := fileContents(stubPath)
contents = replaceStub(contents, name)

err := overwrite("pkg/"+filePath, contents)
err := overwrite(PackageRoot+"/"+filePath, contents)
if err != nil {
fmt.Println(err)
}
Expand All @@ -84,6 +89,7 @@ func replaceStub(content string, name string) string {
content = strings.Replace(content, "{{PluralLowerName}}", Lower(Plural(name)), -1)
content = strings.Replace(content, "{{SingularLowerName}}", Lower(Singular(name)), -1)
content = strings.Replace(content, "{{PackageName}}", PackageName, -1)
content = strings.Replace(content, "{{PackageRoot}}", PackageRoot, -1)
return content
}

Expand Down
27 changes: 0 additions & 27 deletions cmd/generate/main.go

This file was deleted.

6 changes: 3 additions & 3 deletions cmd/generate/stubs/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "C"
import (
"github.com/gin-gonic/gin"
"github.com/shipu/artifact"
"{{PackageName}}/pkg/{{SingularLowerName}}/models"
"{{PackageName}}/pkg/{{SingularLowerName}}/services"
"{{PackageName}}/{{PackageRoot}}/{{SingularLowerName}}/models"
"{{PackageName}}/{{PackageRoot}}/{{SingularLowerName}}/services"
"net/http"
)

Expand Down Expand Up @@ -100,4 +100,4 @@ func {{TitleName}}Delete() gin.HandlerFunc {

artifact.Res.Status(http.StatusOK).Message("Successfully Delete !!!").Data({{SingularLowerName}}).Json(c)
}
}
}
8 changes: 6 additions & 2 deletions cmd/generate/stubs/model.stub
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package models

import (
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/shipu/artifact"
"go.mongodb.org/mongo-driver/bson/primitive"
)

var {{TitleName}}Collection artifact.MongoCollection = artifact.Mongo.Collection("{{PluralLowerName}}")
var {{TitleName}}Collection artifact.MongoCollection

type {{TitleName}} struct {
Id primitive.ObjectID `json:"id,omitempty" bson:"_id"`
Task string `json:"task" bson:"task"`
Status string `json:"status" bson:"status"`
}

func Setup() {
{{TitleName}}Collection = artifact.Mongo.Collection("{{PluralLowerName}}")
}
13 changes: 7 additions & 6 deletions cmd/generate/stubs/route.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package routes

import (
. "github.com/shipu/artifact"
c "{{PackageName}}/pkg/{{SingularLowerName}}/controllers"
c "{{PackageName}}/{{PackageRoot}}/{{SingularLowerName}}/controllers"
)

func Setup() {
Router.GET("{{PluralLowerName}}", c.{{TitleName}}Index())
Router.POST("{{PluralLowerName}}", c.{{TitleName}}Create())
Router.GET("{{PluralLowerName}}/:{{SingularLowerName}}Id", c.{{TitleName}}Show())
Router.PUT("{{PluralLowerName}}/:{{SingularLowerName}}Id", c.{{TitleName}}Update())
Router.DELETE("{{PluralLowerName}}/:{{SingularLowerName}}Id", c.{{TitleName}}Delete())
v1 := Router.Group("api/v1")
v1.GET("{{PluralLowerName}}", c.{{TitleName}}Index())
v1.POST("{{PluralLowerName}}", c.{{TitleName}}Create())
v1.GET("{{PluralLowerName}}/:{{SingularLowerName}}Id", c.{{TitleName}}Show())
v1.PUT("{{PluralLowerName}}/:{{SingularLowerName}}Id", c.{{TitleName}}Update())
v1.DELETE("{{PluralLowerName}}/:{{SingularLowerName}}Id", c.{{TitleName}}Delete())
}
2 changes: 1 addition & 1 deletion cmd/generate/stubs/service.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"{{PackageName}}/pkg/{{SingularLowerName}}/models"
"{{PackageName}}/{{PackageRoot}}/{{SingularLowerName}}/models"
"log"
)

Expand Down
28 changes: 28 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"github.com/shipu/golang-gin-boilerplate/art/cmd/generate"
"github.com/spf13/cobra"
"log"
)

var rootCmd = &cobra.Command{
Use: "art",
Short: "A simple artifact command",
Long: `A simple artifact command`,
Run: hello,
}

func hello(cmd *cobra.Command, args []string) {
log.Println("art command")
}

func init() {
rootCmd.AddCommand(generate.CrudCmd)
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 17bb292

Please sign in to comment.