Skip to content

Commit

Permalink
feat: add base for the make entity command
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinvautier committed Apr 6, 2021
1 parent c743051 commit 79949d6
Show file tree
Hide file tree
Showing 31 changed files with 290 additions and 977 deletions.
44 changes: 44 additions & 0 deletions cmd/make.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import (
"github.com/edwinvautier/go-cli/services/makeCommand"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// makeCmd represents the install command
var makeCmd = &cobra.Command{
Use: "make",
Short: "make is used to create new files, for example for models",
Long: `make is used to create new files, for example for models, it creates your model file after prompting you for fields`,
Run: func(cmd *cobra.Command, args []string) {
if !isAMakeCommand(args[0]) {
log.Fatal(args[0], " is not a make command!")
}
makeCommand.MakeEntity(args[1])
},
}

func init() {
rootCmd.AddCommand(makeCmd)
}

func isAMakeCommand(commandName string) bool {
return commandName == "entity"
}
68 changes: 68 additions & 0 deletions config/make_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config

import (
"os"

"github.com/gobuffalo/packr/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

// InitMakeCmdConfig creates the needed config for the create command by prompting user and doing other actions
func InitMakeCmdConfig(config *MakeCmdConfig) error {
workdir, err := os.Getwd()
if err != nil {
return err
}

viper.AddConfigPath(workdir)
viper.SetConfigName(".go-cli-config")
if err := viper.ReadInConfig(); err != nil {
return err
}

config.GoPackageFullPath = viper.GetString("package")
config.Box = packr.New("makeEntityBox", "../templates/makeEntity")

return nil
}

// AddModelToConfig set the new bundle to true in config after install
func AddModelToConfig(name string) error {
workdir, err := os.Getwd()
if err != nil {
return err
}

viper.AddConfigPath(workdir)
viper.SetConfigName(".go-cli-config")

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
return err
}

models := viper.GetStringSlice("models")
models = append(models, name)
log.Info("Using config file : ", viper.ConfigFileUsed())
viper.WriteConfig()

return nil
}

// InstallCmdConfig is the struct used to configure make command
type MakeCmdConfig struct {
GoPackageFullPath string
Box *packr.Box

EntityNamePascalCase string
EntityNameLowerCase string
HasCustomTypes bool
HasDate bool
Fields EntityField
}

type EntityField struct {
Type string
Name string
}
16 changes: 16 additions & 0 deletions services/makeCommand/make.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package makeCommand

import "github.com/edwinvautier/go-cli/config"

func MakeEntity(entityName string) error {
var makeCmdConfig config.MakeCmdConfig
if err := config.InitMakeCmdConfig(&makeCmdConfig); err != nil {
return err
}

if err := executeTemplates(makeCmdConfig); err != nil {
return err
}

return nil
}
61 changes: 61 additions & 0 deletions services/makeCommand/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package makeCommand

import (
"os"

"text/template"

"github.com/edwinvautier/go-cli/config"
"github.com/edwinvautier/go-cli/helpers"
"github.com/gobuffalo/packd"
log "github.com/sirupsen/logrus"
)

func executeTemplates(makeCmdConfig config.MakeCmdConfig) error {

makeCmdConfig.Box.Walk(func(path string, f packd.File) error {
fInfo, _ := f.FileInfo()
fileParts := helpers.GetFilePartsFromName(fInfo.Name())
generateFile(fileParts.Path, fileParts.Name, fileParts.OutputName, makeCmdConfig)
return nil
})

return nil
}

func generateFile(path string, name string, outputName string, makeCmdConfig config.MakeCmdConfig) {
// Get template content as string
templateString, err := makeCmdConfig.Box.FindString(path + name)
if err != nil {
log.Error(err)
return
}
workdir, err := os.Getwd()
if err != nil {
log.Error(err)
}
// Create the directory if not exist
if _, err := os.Stat(workdir + "/" + path); os.IsNotExist(err) {
os.MkdirAll(workdir+"/"+path, os.ModePerm)
}

err = executeTemplate(makeCmdConfig, outputName, workdir+"/"+path, templateString)
if err != nil {
log.Error(err)
return
}
}

func executeTemplate(makeCmdConfig config.MakeCmdConfig, outputName string, path string, templateString string) error {
// Create the file
file, err := os.Create(path + outputName)
if err != nil {
log.Error(err)
return err
}
// Execute template and write file
parsedTemplate := template.Must(template.New("template").Parse(templateString))
err = parsedTemplate.Execute(file, makeCmdConfig)

return nil
}
12 changes: 0 additions & 12 deletions templates/.env.dist.template

This file was deleted.

42 changes: 0 additions & 42 deletions templates/.github/ISSUE_TEMPLATE/bug_report.md.template

This file was deleted.

12 changes: 0 additions & 12 deletions templates/.github/ISSUE_TEMPLATE/feature_request.md.template

This file was deleted.

39 changes: 0 additions & 39 deletions templates/.github/workflows/ci.yml.template

This file was deleted.

67 changes: 0 additions & 67 deletions templates/.github/workflows/codeql-analysis.yml.template

This file was deleted.

5 changes: 0 additions & 5 deletions templates/.gitignore.template

This file was deleted.

Loading

0 comments on commit 79949d6

Please sign in to comment.