From cc54004f3de071d15c0c76bb1454eb69c8c38aa0 Mon Sep 17 00:00:00 2001 From: edwinvautier Date: Wed, 14 Apr 2021 16:36:42 +0200 Subject: [PATCH 1/5] feat(install): Stop install if bundle in config --- cmd/install.go | 4 ++++ config/bundles/base.go | 24 ++++++++++++++++++++++++ config/install_command.go | 4 +++- helpers/strings.go | 11 +++++++++++ services/createCommand/init_project.go | 2 +- 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 config/bundles/base.go diff --git a/cmd/install.go b/cmd/install.go index ebdb13a..94e0a27 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -17,6 +17,7 @@ limitations under the License. */ import ( + "github.com/edwinvautier/go-cli/config/bundles" "github.com/edwinvautier/go-cli/services/installCommand" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -29,6 +30,9 @@ var installCmd = &cobra.Command{ Long: `A command that install bundles from edwinvautier/go-cli/bundles`, Run: func(cmd *cobra.Command, args []string) { for _, bundleName := range args { + if bundles.IsInstalled(bundleName) { + break + } if err := installCommand.InstallBundle(bundleName); err != nil { log.Error(err) } diff --git a/config/bundles/base.go b/config/bundles/base.go new file mode 100644 index 0000000..00c7d7d --- /dev/null +++ b/config/bundles/base.go @@ -0,0 +1,24 @@ +package bundles + +import ( + "github.com/edwinvautier/go-cli/helpers" + "github.com/edwinvautier/go-cli/services/filesystem" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" +) + +func FindBundlesInConfig() []string { + workdir := filesystem.GetWorkdirOrDie() + viper.AddConfigPath(workdir) + viper.SetConfigName(".go-cli-config") + viper.ReadInConfig() + log.Info(viper.GetStringSlice("bundles")) + + return viper.GetStringSlice("bundles") +} + +func IsInstalled(name string) bool { + bundles := FindBundlesInConfig() + + return helpers.ContainsString(bundles, name) +} \ No newline at end of file diff --git a/config/install_command.go b/config/install_command.go index ca43859..64c8ddc 100644 --- a/config/install_command.go +++ b/config/install_command.go @@ -22,7 +22,9 @@ func UpdateConfigAfterInstalling(name string) { viper.AddConfigPath(workdir) viper.SetConfigName(".go-cli-config") - viper.Set("bundles."+name, true) + bundles := viper.GetStringSlice("bundles") + bundles = append(bundles, name) + viper.Set("bundles", bundles) viper.ReadInConfig() viper.WriteConfig() } diff --git a/helpers/strings.go b/helpers/strings.go index b97c995..f6398fe 100644 --- a/helpers/strings.go +++ b/helpers/strings.go @@ -59,3 +59,14 @@ func UpperCaseFirstChar(word string) string { func LowerCase(name string) string { return strings.ToLower(name) } + +// Check if a substring exists in strings slice +func ContainsString(slice []string, substr string) bool { + for _, element := range slice { + if substr == element { + return true + } + } + + return false +} diff --git a/services/createCommand/init_project.go b/services/createCommand/init_project.go index f93de77..1463e18 100644 --- a/services/createCommand/init_project.go +++ b/services/createCommand/init_project.go @@ -66,7 +66,7 @@ func createProjectConfig(workdir string, config *config.CreateCmdConfig) { viper.Set("package", config.GoPackageFullPath) viper.Set("database", config.DBMS) viper.Set("use_docker", config.UseDocker) - viper.SetDefault("bundles.authenticator", false) + viper.SetDefault("bundles", []string{}) viper.AutomaticEnv() // read in environment variables that match From 448b5ce79e9172bbc009835c6c8b0c067afecc57 Mon Sep 17 00:00:00 2001 From: edwinvautier Date: Thu, 15 Apr 2021 09:12:40 +0200 Subject: [PATCH 2/5] feat: Add update command that reads in models to update project config. --- bundles/authenticator/env_test.go | 2 +- cmd/install.go | 2 +- cmd/update.go | 50 ++++++++++++++++++ config/base.go | 4 +- config/bundles/base.go | 13 ++--- config/create_command_test.go | 8 +-- config/install_command.go | 2 +- config/install_command_test.go | 2 +- config/make_command.go | 2 +- config/make_command_test.go | 34 ++++++------ config/update.go | 73 ++++++++++++++++++++++++++ config/viper.go | 5 +- config/viper_test.go | 10 ++-- helpers/strings.go | 6 +-- helpers/strings_test.go | 24 ++++----- services/filesystem/filesystem.go | 2 +- services/filesystem/filesystem_test.go | 16 +++--- services/updateCommand/parser.go | 71 +++++++++++++++++++++++++ 18 files changed, 259 insertions(+), 67 deletions(-) create mode 100644 cmd/update.go create mode 100644 config/update.go create mode 100644 services/updateCommand/parser.go diff --git a/bundles/authenticator/env_test.go b/bundles/authenticator/env_test.go index 8f13b38..400ac84 100644 --- a/bundles/authenticator/env_test.go +++ b/bundles/authenticator/env_test.go @@ -31,7 +31,7 @@ func Test_goDotEnvVariable(t *testing.T) { } }) } - workdir:= filesystem.GetWorkdirOrDie() + workdir := filesystem.GetWorkdirOrDie() createEnv := exec.Command("touch", workdir+"/.env") createEnv.Run() diff --git a/cmd/install.go b/cmd/install.go index 94e0a27..16de8b8 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -31,7 +31,7 @@ var installCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { for _, bundleName := range args { if bundles.IsInstalled(bundleName) { - break + continue } if err := installCommand.InstallBundle(bundleName); err != nil { log.Error(err) diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 0000000..70e508c --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,50 @@ +/* +Copyright © 2021 NAME HERE + +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. +*/ +package cmd + +import ( + "github.com/edwinvautier/go-cli/config" + "github.com/spf13/cobra" +) + +// updateCmd represents the update command +var updateCmd = &cobra.Command{ + Use: "update", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + config.UpdateConfig() + }, +} + +func init() { + rootCmd.AddCommand(updateCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // updateCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // updateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/config/base.go b/config/base.go index d2639fd..96a1e4f 100644 --- a/config/base.go +++ b/config/base.go @@ -14,11 +14,11 @@ type CommandConfigInterface interface { } func initBasicConfig() baseConfig { - if err := initViper(); err != nil { + if err := InitViper(); err != nil { log.Error(err) return baseConfig{} } - workdir:= filesystem.GetWorkdirOrDie() + workdir := filesystem.GetWorkdirOrDie() return baseConfig{ PackagePath: viper.GetString("package"), diff --git a/config/bundles/base.go b/config/bundles/base.go index 00c7d7d..e6ba2e8 100644 --- a/config/bundles/base.go +++ b/config/bundles/base.go @@ -1,18 +1,15 @@ package bundles import ( + "github.com/edwinvautier/go-cli/config" "github.com/edwinvautier/go-cli/helpers" - "github.com/edwinvautier/go-cli/services/filesystem" - log "github.com/sirupsen/logrus" "github.com/spf13/viper" ) func FindBundlesInConfig() []string { - workdir := filesystem.GetWorkdirOrDie() - viper.AddConfigPath(workdir) - viper.SetConfigName(".go-cli-config") - viper.ReadInConfig() - log.Info(viper.GetStringSlice("bundles")) + if err := config.InitViper(); err != nil { + return []string{} + } return viper.GetStringSlice("bundles") } @@ -21,4 +18,4 @@ func IsInstalled(name string) bool { bundles := FindBundlesInConfig() return helpers.ContainsString(bundles, name) -} \ No newline at end of file +} diff --git a/config/create_command_test.go b/config/create_command_test.go index 75903f4..7a477ee 100644 --- a/config/create_command_test.go +++ b/config/create_command_test.go @@ -9,7 +9,7 @@ import ( func TestCreateCmdConfig_GetBox(t *testing.T) { type fields struct { - Box *packr.Box + Box *packr.Box } tests := []struct { name string @@ -27,7 +27,7 @@ func TestCreateCmdConfig_GetBox(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cmd := CreateCmdConfig{ - Box: tt.fields.Box, + Box: tt.fields.Box, } name := cmd.Box.Name if got := cmd.GetBox().Name; !reflect.DeepEqual(got, name) { @@ -39,7 +39,7 @@ func TestCreateCmdConfig_GetBox(t *testing.T) { func TestCreateCmdConfig_GetProjectPath(t *testing.T) { type fields struct { - ProjectPath string + ProjectPath string } tests := []struct { name string @@ -64,7 +64,7 @@ func TestCreateCmdConfig_GetProjectPath(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cmd := CreateCmdConfig{ - ProjectPath: tt.fields.ProjectPath, + ProjectPath: tt.fields.ProjectPath, } if got := cmd.GetProjectPath(); got != tt.want { t.Errorf("CreateCmdConfig.GetProjectPath() = %v, want %v", got, tt.want) diff --git a/config/install_command.go b/config/install_command.go index 64c8ddc..c6f91ff 100644 --- a/config/install_command.go +++ b/config/install_command.go @@ -18,7 +18,7 @@ func InitInstallCmdConfig(config *InstallCmdConfig) error { // UpdateConfigAfterInstalling set the new bundle to true in config after install func UpdateConfigAfterInstalling(name string) { - workdir:= filesystem.GetWorkdirOrDie() + workdir := filesystem.GetWorkdirOrDie() viper.AddConfigPath(workdir) viper.SetConfigName(".go-cli-config") diff --git a/config/install_command_test.go b/config/install_command_test.go index a602831..4ebea06 100644 --- a/config/install_command_test.go +++ b/config/install_command_test.go @@ -71,4 +71,4 @@ func TestInstallCmdConfig_GetProjectPath(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/config/make_command.go b/config/make_command.go index 715e085..ed5c5e3 100644 --- a/config/make_command.go +++ b/config/make_command.go @@ -23,7 +23,7 @@ func InitMakeCmdConfig(config *MakeCmdConfig) error { // AddModelToConfig set the new bundle to true in config after install func AddModelToConfig(newEntity entity.NewEntity) error { - workdir:= filesystem.GetWorkdirOrDie() + workdir := filesystem.GetWorkdirOrDie() viper.AddConfigPath(workdir) viper.SetConfigName(".go-cli-config") diff --git a/config/make_command_test.go b/config/make_command_test.go index 00c6bf0..c5ad900 100644 --- a/config/make_command_test.go +++ b/config/make_command_test.go @@ -24,16 +24,16 @@ func TestAddModelToConfig(t *testing.T) { name: "", args: args{ newEntity: entity.NewEntity{ - Name: "", + Name: "", NamePascalCase: "", - NameLowerCase: "", - HasDate: false, + NameLowerCase: "", + HasDate: false, HasCustomTypes: false, Fields: []entity.EntityField{ { - Type: "string", - Name: "Name", - IsSlice: false, + Type: "string", + Name: "Name", + IsSlice: false, SliceType: "", }, }, @@ -51,7 +51,7 @@ func TestAddModelToConfig(t *testing.T) { } // Create config file - workdir:= filesystem.GetWorkdirOrDie() + workdir := filesystem.GetWorkdirOrDie() if _, err := os.Create(workdir + "/.go-cli-config.yml"); err != nil { log.Error(err) return @@ -66,16 +66,16 @@ func TestAddModelToConfig(t *testing.T) { name: "", args: args{ newEntity: entity.NewEntity{ - Name: "", + Name: "", NamePascalCase: "", - NameLowerCase: "", - HasDate: false, + NameLowerCase: "", + HasDate: false, HasCustomTypes: false, Fields: []entity.EntityField{ { - Type: "string", - Name: "Name", - IsSlice: false, + Type: "string", + Name: "Name", + IsSlice: false, SliceType: "", }, }, @@ -100,7 +100,7 @@ func TestAddModelToConfig(t *testing.T) { func TestMakeCmdConfig_GetBox(t *testing.T) { type fields struct { - Box *packr.Box + Box *packr.Box } tests := []struct { name string @@ -118,7 +118,7 @@ func TestMakeCmdConfig_GetBox(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cmd := MakeCmdConfig{ - Box: tt.fields.Box, + Box: tt.fields.Box, } name := cmd.Box.Name if got := cmd.GetBox().Name; !reflect.DeepEqual(got, name) { @@ -130,7 +130,7 @@ func TestMakeCmdConfig_GetBox(t *testing.T) { func TestMakeCmdConfig_GetProjectPath(t *testing.T) { type fields struct { - ProjectPath string + ProjectPath string } tests := []struct { name string @@ -155,7 +155,7 @@ func TestMakeCmdConfig_GetProjectPath(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cmd := MakeCmdConfig{ - ProjectPath: tt.fields.ProjectPath, + ProjectPath: tt.fields.ProjectPath, } if got := cmd.GetProjectPath(); got != tt.want { t.Errorf("MakeCmdConfig.GetProjectPath() = %v, want %v", got, tt.want) diff --git a/config/update.go b/config/update.go new file mode 100644 index 0000000..da5fccc --- /dev/null +++ b/config/update.go @@ -0,0 +1,73 @@ +package config + +import ( + "io/ioutil" + + "github.com/edwinvautier/go-cli/helpers" + "github.com/edwinvautier/go-cli/prompt/entity" + "github.com/edwinvautier/go-cli/services/filesystem" + "github.com/edwinvautier/go-cli/services/updateCommand" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" +) + +// Read the project main parts in order to refresh the config store in the .go-cli-config.yml +func UpdateConfig() error { + if err := InitViper(); err != nil { + return err + } + + // Update models + if err := updateModels(); err != nil { + return err + } + return nil +} + +func updateModels() error { + // Get config models list + configModels := viper.GetStringMap("models") + + // Get project models list + projectModels := entity.GetEntitiesList() + + // Trigger config generation for each new entity + for _, modelName := range projectModels { + if configModels[helpers.LowerCase(modelName)] != nil { + continue + } + + var entity entity.NewEntity + entity.Name = modelName + if err := generateModel(&entity); err != nil { + log.Error("couldn't add " + modelName + "to config") + return err + } + + if err := AddModelToConfig(entity); err != nil { + log.Error("couldn't add entity to config : ", err) + return err + } + } + + return nil +} + +func generateModel(model *entity.NewEntity) error { + model.NamePascalCase = model.Name + model.NameLowerCase = helpers.LowerCase(model.Name) + + // Get file content + workdir := filesystem.GetWorkdirOrDie() + filePath := workdir + "/api/models/" + model.NameLowerCase + ".go" + content, err := ioutil.ReadFile(filePath) + + if err != nil { + log.Error("couldn't read : ", filePath) + return err + } + contentString := string(content) + updateCommand.ParseEntity(model, contentString) + + return nil +} diff --git a/config/viper.go b/config/viper.go index db030ca..f46088e 100644 --- a/config/viper.go +++ b/config/viper.go @@ -5,8 +5,9 @@ import ( "github.com/spf13/viper" ) -func initViper() error { - workdir:= filesystem.GetWorkdirOrDie() +// init acces to viper config in workdir +func InitViper() error { + workdir := filesystem.GetWorkdirOrDie() viper.AddConfigPath(workdir) viper.SetConfigName(".go-cli-config") diff --git a/config/viper_test.go b/config/viper_test.go index 180da24..5b4454a 100644 --- a/config/viper_test.go +++ b/config/viper_test.go @@ -14,13 +14,13 @@ func Test_initViper(t *testing.T) { wantErr bool }{ { - name: "Test without config file", + name: "Test without config file", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := initViper(); (err != nil) != tt.wantErr { + if err := InitViper(); (err != nil) != tt.wantErr { t.Errorf("initViper() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -32,19 +32,19 @@ func Test_initViper(t *testing.T) { log.Error(err) return } - + tests = []struct { name string wantErr bool }{ { - name: "Test with config file", + name: "Test with config file", wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := initViper(); (err != nil) != tt.wantErr { + if err := InitViper(); (err != nil) != tt.wantErr { t.Errorf("initViper() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/helpers/strings.go b/helpers/strings.go index f6398fe..88facbc 100644 --- a/helpers/strings.go +++ b/helpers/strings.go @@ -8,8 +8,8 @@ import ( // JoinString takes a pointer to a string and modify this string in order to remove spaces and replace them by dashes func JoinString(str string) string { slices := strings.Split(str, " ") - if slices[len(slices) - 1] == "" { - slices = slices[:len(slices) - 1] + if slices[len(slices)-1] == "" { + slices = slices[:len(slices)-1] } return strings.Join(slices, "-") @@ -29,7 +29,7 @@ func GetFilePartsFromName(name string, outputName string) FileParts { fileParts.OutputName = strings.Join(slices[:len(slices)-1], ".") } else { fileParts.OutputName = fileParts.Name - } + } } else { fileParts.OutputName = outputName } diff --git a/helpers/strings_test.go b/helpers/strings_test.go index 0f5fa46..6db8236 100644 --- a/helpers/strings_test.go +++ b/helpers/strings_test.go @@ -52,37 +52,37 @@ func TestGetFilePartsFromName(t *testing.T) { { name: "test without outputName", args: args{ - name: "/files/helloworld.go.test", + name: "/files/helloworld.go.test", outputName: "", }, want: FileParts{ - Name: "helloworld.go.test", + Name: "helloworld.go.test", OutputName: "helloworld.go", - Path: "/files/", + Path: "/files/", }, }, { name: "test withOut extension", args: args{ - name: "/files/helloworldgotest", + name: "/files/helloworldgotest", outputName: "", }, want: FileParts{ - Name: "helloworldgotest", + Name: "helloworldgotest", OutputName: "helloworldgotest", - Path: "/files/", + Path: "/files/", }, }, { name: "test simple path", args: args{ - name: "/files/helloworld.go", + name: "/files/helloworld.go", outputName: "hello.txt", }, want: FileParts{ - Name: "helloworld.go", + Name: "helloworld.go", OutputName: "hello.txt", - Path: "/files/", + Path: "/files/", }, }, } @@ -104,17 +104,17 @@ func TestUpperCaseFirstChar(t *testing.T) { args args want string }{ - { + { name: "test with helloworld", args: args{word: "helloworld"}, want: "Helloworld", }, - { + { name: "test empty", args: args{word: ""}, want: "", }, - { + { name: "test without letter", args: args{word: "1helloworld"}, want: "1helloworld", diff --git a/services/filesystem/filesystem.go b/services/filesystem/filesystem.go index 79dd73f..e5f4716 100644 --- a/services/filesystem/filesystem.go +++ b/services/filesystem/filesystem.go @@ -33,4 +33,4 @@ func GetWorkdirOrDie() string { } return workdir -} \ No newline at end of file +} diff --git a/services/filesystem/filesystem_test.go b/services/filesystem/filesystem_test.go index e6b559c..c99305a 100644 --- a/services/filesystem/filesystem_test.go +++ b/services/filesystem/filesystem_test.go @@ -34,7 +34,7 @@ func TestDirectoryExists(t *testing.T) { } if err := os.Mkdir(dirPath, os.ModePerm); err != nil { - log.Error("mkdir : ",err) + log.Error("mkdir : ", err) } tests = []struct { name string @@ -69,8 +69,8 @@ func TestRemoveDirAndFiles(t *testing.T) { wantErr bool }{ { - name: "Test without directory", - args: args{path: dirPath}, + name: "Test without directory", + args: args{path: dirPath}, wantErr: false, }, } @@ -82,10 +82,10 @@ func TestRemoveDirAndFiles(t *testing.T) { }) } if err := os.Mkdir(dirPath, os.ModePerm); err != nil { - log.Error("mkdir : ",err) + log.Error("mkdir : ", err) } if _, err := os.Create(dirPath + "/test.txt"); err != nil { - log.Error("create file : ",err) + log.Error("create file : ", err) } tests = []struct { @@ -94,8 +94,8 @@ func TestRemoveDirAndFiles(t *testing.T) { wantErr bool }{ { - name: "Test with directory", - args: args{path: dirPath}, + name: "Test with directory", + args: args{path: dirPath}, wantErr: false, }, } @@ -107,4 +107,4 @@ func TestRemoveDirAndFiles(t *testing.T) { }) } os.Remove(dirPath) -} \ No newline at end of file +} diff --git a/services/updateCommand/parser.go b/services/updateCommand/parser.go new file mode 100644 index 0000000..36d3dfc --- /dev/null +++ b/services/updateCommand/parser.go @@ -0,0 +1,71 @@ +package updateCommand + +import ( + "strings" + + "github.com/edwinvautier/go-cli/prompt/entity" +) + +// ParseEntity takes a file string and try to parse entity from it +func ParseEntity(model *entity.NewEntity, fileContent string) { + lines := strings.Split(fileContent, "\n") + // look inside it for infos + lineIsStruct := false + for _, line := range lines { + if hasClosingBracket(line) { + lineIsStruct = false + if haveFoundFields(model) { + break + } + } + + if lineIsStruct { + var field entity.EntityField + parseField(model, &field, line) + + if len(field.Name) > 2 || len(field.Type) > 2 { + model.Fields = append(model.Fields, field) + } + } + + if strings.Contains(line, "type") { + lineIsStruct = true + } + } +} + +func assignType(model *entity.NewEntity, field *entity.EntityField, element string) { + if strings.Contains(element, "[]") { + field.IsSlice = true + field.SliceType = strings.Trim(element, "[]") + element = "slice" + } else if strings.Contains(element, "Time") { + element = "date" + model.HasDate = true + } + field.Type = element +} + +func hasClosingBracket(line string) bool { + return strings.Contains(line, "}") +} + +func haveFoundFields(model *entity.NewEntity) bool { + return len(model.Fields) > 0 +} + +func parseField(model *entity.NewEntity, field *entity.EntityField, line string) { + elements := strings.Split(line, " ") + + for _, element := range elements { + element := strings.Trim(element, "\t") + if len(element) < 2 { + continue + } + if field.Name == "" { + field.Name = element + } else if field.Type == "" { + assignType(model, field, element) + } + } +} From 82132215204d76519f12bc85fbd310238ad0c17a Mon Sep 17 00:00:00 2001 From: edwinvautier Date: Thu, 15 Apr 2021 09:38:03 +0200 Subject: [PATCH 3/5] test(update): Test the entity parser --- services/updateCommand/parser_test.go | 130 ++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 services/updateCommand/parser_test.go diff --git a/services/updateCommand/parser_test.go b/services/updateCommand/parser_test.go new file mode 100644 index 0000000..c979640 --- /dev/null +++ b/services/updateCommand/parser_test.go @@ -0,0 +1,130 @@ +package updateCommand + +import ( + "testing" + + "github.com/edwinvautier/go-cli/prompt/entity" +) + +func TestParseEntity(t *testing.T) { + type args struct { + model *entity.NewEntity + fileContent string + } + tests := []struct { + name string + args args + wantFields bool + }{ + { + name: "Test with bad file", + args: args{ + model: &entity.NewEntity{Name: "customer"}, + fileContent: "", + }, + wantFields: false, + }, + { + name: "Test with goo file file", + args: args{ + model: &entity.NewEntity{Name: "customer"}, + fileContent: "type Customer struct {\nID int\n Name string\n}", + }, + wantFields: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ParseEntity(tt.args.model, tt.args.fileContent) + if tt.wantFields && len(tt.args.model.Fields) < 1 { + t.Error("No fields : ", tt.args.model) + } + }) + } +} + +func Test_parseField(t *testing.T) { + type args struct { + model *entity.NewEntity + field *entity.EntityField + line string + } + tests := []struct { + name string + args args + wantName bool + wantType bool + wantHasDate bool + wantIsSlice bool + }{ + { + name: "test with bad line", + args: args{ + model: &entity.NewEntity{Name: "Customer"}, + field: &entity.EntityField{}, + line: "blop", + }, + wantName: false, + wantType: false, + wantHasDate: false, + wantIsSlice: false, + }, + { + name: "test with standard type", + args: args{ + model: &entity.NewEntity{Name: "Customer"}, + field: &entity.EntityField{}, + line: "ID int", + }, + wantName: true, + wantType: true, + wantHasDate: false, + wantIsSlice: false, + }, + { + name: "test with date", + args: args{ + model: &entity.NewEntity{Name: "Customer"}, + field: &entity.EntityField{}, + line: "\tCreatedAt time.Time", + }, + wantName: true, + wantType: true, + wantHasDate: true, + wantIsSlice: false, + }, + { + name: "test with slice", + args: args{ + model: &entity.NewEntity{Name: "Customer"}, + field: &entity.EntityField{}, + line: "TodoList []string", + }, + wantName: false, + wantType: false, + wantHasDate: false, + wantIsSlice: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + parseField(tt.args.model, tt.args.field, tt.args.line) + + if tt.wantName && len(tt.args.field.Name) < 2 { + t.Error("No field name : ", tt.args.field) + } + + if tt.wantType && len(tt.args.field.Type) < 2 { + t.Error("No field type : ", tt.args.field) + } + + if tt.wantIsSlice && tt.args.field.IsSlice == false { + t.Error("expected field is slice : ", tt.args.field) + } + + if tt.wantHasDate && tt.args.model.HasDate == false { + t.Error("expected model to have date : ", tt.args.field) + } + }) + } +} From e772b0d07391a5a14b43835742d4b7147d6962cf Mon Sep 17 00:00:00 2001 From: edwinvautier Date: Thu, 15 Apr 2021 09:45:44 +0200 Subject: [PATCH 4/5] fix: fix codacy errors. --- cmd/update.go | 4 ++-- config/bundles/base.go | 2 ++ config/update.go | 7 ++----- config/viper.go | 2 +- helpers/strings.go | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cmd/update.go b/cmd/update.go index 70e508c..99c1ba2 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -1,5 +1,6 @@ +package cmd /* -Copyright © 2021 NAME HERE +Copyright © 2021 Edwin Vautier edwin.vautier@gmail.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,7 +14,6 @@ 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. */ -package cmd import ( "github.com/edwinvautier/go-cli/config" diff --git a/config/bundles/base.go b/config/bundles/base.go index e6ba2e8..5031731 100644 --- a/config/bundles/base.go +++ b/config/bundles/base.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/viper" ) +// FindBundlesInConfig returns the list of bundles from the config file func FindBundlesInConfig() []string { if err := config.InitViper(); err != nil { return []string{} @@ -14,6 +15,7 @@ func FindBundlesInConfig() []string { return viper.GetStringSlice("bundles") } +// IsInstalled takes a bundle name and check if this name is in the config installed bundles func IsInstalled(name string) bool { bundles := FindBundlesInConfig() diff --git a/config/update.go b/config/update.go index da5fccc..d291228 100644 --- a/config/update.go +++ b/config/update.go @@ -11,17 +11,14 @@ import ( "github.com/spf13/viper" ) -// Read the project main parts in order to refresh the config store in the .go-cli-config.yml +// UpdateConfig reads the project main parts in order to refresh the config store in the .go-cli-config.yml func UpdateConfig() error { if err := InitViper(); err != nil { return err } // Update models - if err := updateModels(); err != nil { - return err - } - return nil + return updateModels() } func updateModels() error { diff --git a/config/viper.go b/config/viper.go index f46088e..23feeba 100644 --- a/config/viper.go +++ b/config/viper.go @@ -5,7 +5,7 @@ import ( "github.com/spf13/viper" ) -// init acces to viper config in workdir +// InitViper init acces to viper config in workdir func InitViper() error { workdir := filesystem.GetWorkdirOrDie() viper.AddConfigPath(workdir) diff --git a/helpers/strings.go b/helpers/strings.go index 88facbc..f98bb64 100644 --- a/helpers/strings.go +++ b/helpers/strings.go @@ -60,7 +60,7 @@ func LowerCase(name string) string { return strings.ToLower(name) } -// Check if a substring exists in strings slice +// ContainsString check if a substring exists in strings slice func ContainsString(slice []string, substr string) bool { for _, element := range slice { if substr == element { From 29ce8d09a04e847da7d403a3cb3706a45aa13218 Mon Sep 17 00:00:00 2001 From: edwinvautier Date: Thu, 15 Apr 2021 10:20:11 +0200 Subject: [PATCH 5/5] refacto: rename udpate.go to update_command.go --- config/{update.go => update_command.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename config/{update.go => update_command.go} (100%) diff --git a/config/update.go b/config/update_command.go similarity index 100% rename from config/update.go rename to config/update_command.go