-
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.
Merge pull request #44 from edwinvautier/feat-add-config-read
Feat add config read
- Loading branch information
Showing
20 changed files
with
422 additions
and
60 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
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
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,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") | ||
} |
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
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,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) | ||
} |
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
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
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 |
---|---|---|
|
@@ -71,4 +71,4 @@ func TestInstallCmdConfig_GetProjectPath(t *testing.T) { | |
} | ||
}) | ||
} | ||
} | ||
} |
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
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
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,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 | ||
} |
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
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
Oops, something went wrong.