Skip to content

Commit

Permalink
refactor: improve function and simplify comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
beliven-fabrizio-gortani committed Jun 22, 2023
1 parent 5cbfdf5 commit f2cffdf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
14 changes: 7 additions & 7 deletions controllers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func upsertConfigSSH() error {

defer file.Close()

oldContent, err := os.ReadFile(config.SSHConfigFilePath)
oldContent, _ := os.ReadFile(config.SSHConfigFilePath)
oldContentToString := string(oldContent)

delimiterStart := "# HSSH start managed"
delimiterEnd := "# HSSH end managed"
includeString := "Include " + config.HSSHHostFolderName + "/*"

var row = delimiterStart + "\n" + includeString + "\n" + delimiterEnd + "\n\n"
if isFilePathInConfigSSH(oldContentToString, row) == true {
if isFilePathInConfigSSH(oldContentToString, row) {
deleteRegex := regexp.MustCompile("(?ms)" + delimiterStart + ".*" + delimiterEnd + "\n\n")
oldContentToString = deleteRegex.ReplaceAllString(oldContentToString, "")
}
Expand Down Expand Up @@ -124,12 +124,12 @@ func isHSSHInitialized() bool {
func Init(force bool) {

isInit := isHSSHInitialized()
if force == false && isInit == false {
if !force && !isInit {
messages.ConfigNotEditedYet()
os.Exit(0)
}

if force == false && isInit == true {
if !force && isInit {
viper.SetConfigFile(config.HSSHConfigFilePath)
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
Expand All @@ -142,7 +142,7 @@ func Init(force bool) {
func() {
CreateHSSHConfig(func(err error, isNotConfigured bool) {
messages.PrintStep(fmt.Sprintf("File %s", config.HSSHConfigFilePath), err)
if isNotConfigured == true {
if isNotConfigured {
messages.MustBeConfigured()
}
})
Expand All @@ -156,9 +156,9 @@ func Init(force bool) {
CreateHSSHHostFolder(func(err error) {
messages.PrintStep(fmt.Sprintf("Folder %s", config.HSSHHostFolderPath), err)
isEmpty, err := isFolderEmpty(config.HSSHHostFolderPath)
if err != nil || isEmpty == true {
if err != nil || isEmpty {
Sync()
messages.PrintStep(fmt.Sprintf("Automatic Sync"), err)
messages.PrintStep("Automatic Sync", err)
} else {
messages.Print("Folder is not empty. The automatic sync will be ignored\n")
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func unique(arr []string) []string {
result := []string{}

for e := range arr {
if occured[arr[e]] != true {
if !occured[arr[e]] {
occured[arr[e]] = true
result = append(result, arr[e])
}
Expand Down
22 changes: 9 additions & 13 deletions controllers/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func craftPath(filePath string) string {
func getObsoleteFiles(whitelist []string) []string {
files := []string{}
filepath.Walk(config.HSSHHostFolderPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() == true {
if info.IsDir() {
return nil
}

Expand All @@ -49,7 +49,7 @@ func getObsoleteFiles(whitelist []string) []string {
}
}

if match == true {
if match {
return nil
}

Expand Down Expand Up @@ -101,7 +101,6 @@ func syncWithProvider(providerConnection providers.ProviderConnection) []string
}

hostPath = host.GetPath()
return
}(file.Path, file.ID)
}

Expand All @@ -121,33 +120,31 @@ func syncWithProvider(providerConnection providers.ProviderConnection) []string
}

func readProviderConnections() []providers.ProviderConnection {
var providersConfig Config
list := []providers.ProviderConnection{}

// Single provider of the first version
singleProvider := viper.GetString("provider")

// Multiple provider of the early version
// if the user select the new version of multi structured providers
// the result is empty slice
multiProvider := viper.GetStringSlice("providers")

// Structured multi provider for the new version
var providersConfig Config
viper.Unmarshal(&providersConfig)

multiProvider = append(multiProvider, singleProvider)
multiProvider = unique(multiProvider)

list := []providers.ProviderConnection{}
// Structured multi provider for the new version
viper.Unmarshal(&providersConfig)

// Convert string connections into structured version
for _, p := range multiProvider {
pconn := providers.ProviderConnection{}
pconn.FromString(p)

list = append(list, pconn)
}

list = append(list, providersConfig.Providers...)

return list
return append(list, providersConfig.Providers...)
}

// Sync ...
Expand All @@ -168,7 +165,6 @@ func Sync() {
} else {
channel <- syncWithProvider(p)
}
return
}(provider)
}

Expand Down

0 comments on commit f2cffdf

Please sign in to comment.