-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add base for the make entity command
- Loading branch information
edwinvautier
committed
Apr 6, 2021
1 parent
c743051
commit 79949d6
Showing
31 changed files
with
290 additions
and
977 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
templates/.github/ISSUE_TEMPLATE/feature_request.md.template
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.