Skip to content

Commit

Permalink
feat(install): Stop install if bundle in config
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinvautier committed Apr 14, 2021
1 parent 97ba635 commit cc54004
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
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) {
break
}
if err := installCommand.InstallBundle(bundleName); err != nil {
log.Error(err)
}
Expand Down
24 changes: 24 additions & 0 deletions config/bundles/base.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 3 additions & 1 deletion config/install_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
11 changes: 11 additions & 0 deletions helpers/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion services/createCommand/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit cc54004

Please sign in to comment.