Skip to content

Commit

Permalink
Merge pull request #44 from edwinvautier/feat-add-config-read
Browse files Browse the repository at this point in the history
Feat add config read
  • Loading branch information
Edwin Vautier authored Apr 15, 2021
2 parents 97ba635 + 29ce8d0 commit ddeaa1b
Show file tree
Hide file tree
Showing 20 changed files with 422 additions and 60 deletions.
2 changes: 1 addition & 1 deletion bundles/authenticator/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_goDotEnvVariable(t *testing.T) {
}
})
}
workdir:= filesystem.GetWorkdirOrDie()
workdir := filesystem.GetWorkdirOrDie()
createEnv := exec.Command("touch", workdir+"/.env")
createEnv.Run()

Expand Down
4 changes: 4 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
continue
}
if err := installCommand.InstallBundle(bundleName); err != nil {
log.Error(err)
}
Expand Down
50 changes: 50 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd
/*
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.
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/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")
}
4 changes: 2 additions & 2 deletions config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
23 changes: 23 additions & 0 deletions config/bundles/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bundles

import (
"github.com/edwinvautier/go-cli/config"
"github.com/edwinvautier/go-cli/helpers"
"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{}
}

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()

return helpers.ContainsString(bundles, name)
}
8 changes: 4 additions & 4 deletions config/create_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestCreateCmdConfig_GetBox(t *testing.T) {
type fields struct {
Box *packr.Box
Box *packr.Box
}
tests := []struct {
name string
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions config/install_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ 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")
viper.Set("bundles."+name, true)
bundles := viper.GetStringSlice("bundles")
bundles = append(bundles, name)
viper.Set("bundles", bundles)
viper.ReadInConfig()
viper.WriteConfig()
}
Expand Down
2 changes: 1 addition & 1 deletion config/install_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ func TestInstallCmdConfig_GetProjectPath(t *testing.T) {
}
})
}
}
}
2 changes: 1 addition & 1 deletion config/make_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
34 changes: 17 additions & 17 deletions config/make_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
},
},
Expand All @@ -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
Expand All @@ -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: "",
},
},
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand Down
70 changes: 70 additions & 0 deletions config/update_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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"
)

// 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
return updateModels()
}

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
}
5 changes: 3 additions & 2 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"github.com/spf13/viper"
)

func initViper() error {
workdir:= filesystem.GetWorkdirOrDie()
// InitViper init acces to viper config in workdir
func InitViper() error {
workdir := filesystem.GetWorkdirOrDie()
viper.AddConfigPath(workdir)
viper.SetConfigName(".go-cli-config")

Expand Down
10 changes: 5 additions & 5 deletions config/viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand All @@ -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)
}
})
Expand Down
Loading

0 comments on commit ddeaa1b

Please sign in to comment.