Skip to content

Commit

Permalink
chore: 🚚 move validating config into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashRajpurohit committed Jul 31, 2024
1 parent 78b6dfb commit 24549f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
17 changes: 3 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,9 @@ var rootCmd = &cobra.Command{
logger.Info("Config loaded from: ", config.GetConfigFile(cfgFile))
logger.Info("Validating config ⏳")

if cfg.Username == "" {
logger.Fatal("No username found in config file, please add one.")
}

if cfg.Token == "" {
logger.Fatal("No token found in config file, please add one. See here: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#about-personal-access-tokens")
}

if cfg.BackupDir == "" {
logger.Fatal("No backup directory found in config file, please add one.")
}

if len(cfg.IncludeRepos) > 0 && len(cfg.ExcludeRepos) > 0 {
logger.Warn("Both include and exclude repos are set, ignoring exclude repos")
err = config.ValidateConfig(cfg)
if err != nil {
logger.Fatalf("Error validating config: %s", err)
}

var client client.Client
Expand Down
27 changes: 27 additions & 0 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import "fmt"

func ValidateConfig(cfg Config) error {
if cfg.Username == "" {
return fmt.Errorf("username cannot be empty")
}

if cfg.Token == "" {
return fmt.Errorf("token cannot be empty. See here: https://github.com/AkashRajpurohit/git-sync#how-do-i-create-a-github-personal-access-token")
}

if cfg.BackupDir == "" {
return fmt.Errorf("backup directory cannot be empty")
}

if cfg.Server.Domain == "" {
return fmt.Errorf("server domain cannot be empty")
}

if cfg.Server.Protocol != "https" && cfg.Server.Protocol != "http" {
return fmt.Errorf("server protocol can only be http or https")
}

return nil
}

0 comments on commit 24549f1

Please sign in to comment.