Skip to content

Commit

Permalink
Fix lint and add license
Browse files Browse the repository at this point in the history
  • Loading branch information
Khang Ha committed Sep 5, 2020
1 parent 5c42767 commit 4fc01f6
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 19 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Ha Le Duy Khang (a.k.a Kelvin)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ var initCmd = &cobra.Command{
}

err = utils.CreateFile(fmt.Sprintf("./%v", ".env"), []byte(templates.EnvTemplate))
if err != nil {
logrus.Fatal(err)
}

fmt.Println("Go mod init...")
time.Sleep(time.Second)
Expand Down
3 changes: 3 additions & 0 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var newCmd = &cobra.Command{
},
}

// GenerateController ...
func GenerateController(params utils.StandardizeParams) error {
path := fmt.Sprintf("./%v", "controllers")
fileName := utils.Snake(params.ControllerName)
Expand All @@ -97,6 +98,7 @@ func GenerateController(params utils.StandardizeParams) error {
return err
}

// GenerateService ...
func GenerateService(params utils.StandardizeParams) error {
path := fmt.Sprintf("./%v", "services")
fileName := utils.Snake(params.ServiceName)
Expand All @@ -109,6 +111,7 @@ func GenerateService(params utils.StandardizeParams) error {
return err
}

// GenerateRepository ...
func GenerateRepository(params utils.StandardizeParams) error {
path := fmt.Sprintf("./%v", "repositories")
fileName := utils.Snake(params.RepositoryName)
Expand Down
1 change: 1 addition & 0 deletions constants/characters.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package constants

// Character constants
const (
Backtick = "`"
)
1 change: 1 addition & 0 deletions constants/colors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package constants

// Color Constants
const (
ColorReset = "\033[0m"
ColorRed = "\033[31m"
Expand Down
19 changes: 10 additions & 9 deletions constants/def.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package constants

// Definition constants
const (
MODULE_NAME = "MODULE_NAME"
BACK_STICK = "BACK_STICK"
BASE_PATH = "BASE_PATH"
CONTROLLER_NAME = "CONTROLLER_NAME"
CONTROLLER_NAME_LOWER = "controller_name"
SERVICE_NAME = "SERVICE_NAME"
SERVICE_NAME_LOWER = "service_name"
REPOSITORY_NAME = "REPOSITORY_NAME"
REPOSITORY_NAME_LOWER = "repository_name"
ModuleName = "MODULE_NAME"
BackStick = "BACK_STICK"
BasePath = "BASE_PATH"
ControllerName = "CONTROLLER_NAME"
ControllerNameLower = "controller_name"
ServiceName = "SERVICE_NAME"
ServiceNameLower = "service_name"
RepositoryName = "REPOSITORY_NAME"
RepositoryNameLower = "repository_name"

Space = " "
Underscore = "_"
Expand Down
1 change: 1 addition & 0 deletions constants/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package constants

// Version ...
const Version = "v0.0.6"
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
return
}

pwd, err = filepath.EvalSymlinks(pwd)
_, err = filepath.EvalSymlinks(pwd)
if err != nil {
logrus.Error(err)
return
Expand Down
1 change: 1 addition & 0 deletions templates/adapter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// DBAdapterTemplate ...
const DBAdapterTemplate = `package database
import (
Expand Down
1 change: 1 addition & 0 deletions templates/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// ConfigTemplate ...
const ConfigTemplate = `package configs
import (
Expand Down
3 changes: 3 additions & 0 deletions templates/controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// BaseController ...
const BaseController = `package controllers
import (
Expand All @@ -19,6 +20,7 @@ func (h *Base) HandleError(c *gin.Context, err error) {
utils.HandleError(c, err)
}`

// ControllerTemplate ...
const ControllerTemplate = `package controllers
import (
Expand All @@ -45,6 +47,7 @@ func (c *CONTROLLER_NAMEController) DoSomething(ctx *gin.Context) {
}
`

// AppController ...
const AppController = `package controllers
import (
Expand Down
2 changes: 2 additions & 0 deletions templates/dtos.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// BaseDtos ...
const BaseDtos = `package dtos
// Meta is common meta.
Expand All @@ -9,6 +10,7 @@ type Meta struct {
}
`

// HealthDtos ...
const HealthDtos = `package dtos
// HealthResponse represent body response of health check API.
Expand Down
1 change: 1 addition & 0 deletions templates/env.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// EnvTemplate ...
const EnvTemplate = `export MYSQL_USER=root
export MYSQL_PASS=root
export MYSQL_HOST=localhost
Expand Down
2 changes: 2 additions & 0 deletions templates/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// ErrorsTemplate ...
const ErrorsTemplate = `package errors
import (
Expand Down Expand Up @@ -113,6 +114,7 @@ func Wrapf(errCode ErrorCode, err error, msg string, args ...interface{}) error
}
`

// ErrorCodesTemplate ...
const ErrorCodesTemplate = `package errors
import (
Expand Down
1 change: 1 addition & 0 deletions templates/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// MainContent ...
const MainContent = `package main
import (
Expand Down
1 change: 1 addition & 0 deletions templates/makefile.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// MakefileTemplate ...
const MakefileTemplate = `prepare:
@go get -u github.com/beego/bee
@go get -u github.com/swaggo/swag
Expand Down
1 change: 1 addition & 0 deletions templates/repository.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// RepositoryTemplate ...
const RepositoryTemplate = `package repositories
import (
Expand Down
1 change: 1 addition & 0 deletions templates/service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// ServiceTemplate ...
const ServiceTemplate = `package services
import (
Expand Down
1 change: 1 addition & 0 deletions templates/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package templates

// ResponseUtil ...
const ResponseUtil = `package utils
import (
Expand Down
20 changes: 11 additions & 9 deletions utils/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/khanghldk/gokit/constants"
)

// StandardizeParams ...
type StandardizeParams struct {
ModuleName string
BasePath string
Expand All @@ -14,18 +15,19 @@ type StandardizeParams struct {
RepositoryName string
}

// StandardizedTemplate ...
func StandardizedTemplate(content string, params StandardizeParams) string {
content = strings.ReplaceAll(content, constants.BACK_STICK, constants.Backtick)
content = strings.ReplaceAll(content, constants.MODULE_NAME, params.ModuleName)
content = strings.ReplaceAll(content, constants.BASE_PATH, params.BasePath)
content = strings.ReplaceAll(content, constants.BackStick, constants.Backtick)
content = strings.ReplaceAll(content, constants.ModuleName, params.ModuleName)
content = strings.ReplaceAll(content, constants.BasePath, params.BasePath)

content = strings.ReplaceAll(content, constants.CONTROLLER_NAME, Camel(params.ControllerName, false))
content = strings.ReplaceAll(content, constants.SERVICE_NAME, Camel(params.ServiceName, false))
content = strings.ReplaceAll(content, constants.REPOSITORY_NAME, Camel(params.RepositoryName, false))
content = strings.ReplaceAll(content, constants.ControllerName, Camel(params.ControllerName, false))
content = strings.ReplaceAll(content, constants.ServiceName, Camel(params.ServiceName, false))
content = strings.ReplaceAll(content, constants.RepositoryName, Camel(params.RepositoryName, false))

content = strings.ReplaceAll(content, constants.CONTROLLER_NAME_LOWER, Camel(params.ControllerName, true))
content = strings.ReplaceAll(content, constants.SERVICE_NAME_LOWER, Camel(params.ServiceName, true))
content = strings.ReplaceAll(content, constants.REPOSITORY_NAME_LOWER, Camel(params.RepositoryName, true))
content = strings.ReplaceAll(content, constants.ControllerNameLower, Camel(params.ControllerName, true))
content = strings.ReplaceAll(content, constants.ServiceNameLower, Camel(params.ServiceName, true))
content = strings.ReplaceAll(content, constants.RepositoryNameLower, Camel(params.RepositoryName, true))

return content
}
1 change: 1 addition & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
)

// CreateFile ...
func CreateFile(fileName string, content []byte) error {
_, err := os.Create(fileName)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"os/exec"
)

// GitInit ...
func GitInit(dir string) ([]byte, error) {
cmd := exec.Command("bash", "-c", "git init")
cmd.Dir = dir
return cmd.CombinedOutput()
}

// GetVersion ...
func GetVersion() (string, error) {
cmd := exec.Command("bash", "-c", "git tag --sort=-version:refname | head -n 1")
output, err := cmd.Output()
Expand Down
3 changes: 3 additions & 0 deletions utils/go_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import (
"github.com/khanghldk/gokit/constants"
)

// GoModInit ...
func GoModInit(moduleName, dir string) ([]byte, error) {
cmd := exec.Command("bash", "-c", fmt.Sprintf("go mod init %v", moduleName))
cmd.Dir = dir
return cmd.CombinedOutput()
}

// GoModTidy ...
func GoModTidy(dir string) ([]byte, error) {
cmd := exec.Command("bash", "-c", "go mod tidy")
cmd.Dir = dir
return cmd.CombinedOutput()
}

// GetModuleName ...
func GetModuleName() string {
var result string
var count = 0
Expand Down
4 changes: 4 additions & 0 deletions utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// snake: hello_world
// camel words: HelloWorld

// Snake ...
func Snake(s string) string {
var result string
if strings.Contains(s, constants.Space) {
Expand Down Expand Up @@ -45,6 +46,7 @@ func Snake(s string) string {
return result
}

// Camel ...
func Camel(s string, lowerFirst bool) string {
var result string
if strings.Contains(s, constants.Space) {
Expand Down Expand Up @@ -86,13 +88,15 @@ func Camel(s string, lowerFirst bool) string {
return result
}

// UcFirst ...
func UcFirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}

// LcFirst ...
func LcFirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
Expand Down

0 comments on commit 4fc01f6

Please sign in to comment.