github.com/harryzcy/snuuze@v0.3.3-0.20240314015559-83a8fc5627a8/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/viper"
     7  
     8  	"github.com/harryzcy/snuuze/types"
     9  )
    10  
    11  var (
    12  	config types.Config
    13  )
    14  
    15  // LoadWorkflows loads the workflows configurations
    16  func LoadConfig() error {
    17  	c := viper.New()
    18  	c.SetConfigName("snuuze")
    19  	c.SetConfigType("yaml")
    20  	c.AddConfigPath(".")
    21  	c.AddConfigPath(".github")
    22  	err := c.ReadInConfig()
    23  	if err != nil {
    24  		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
    25  			// use default config
    26  			setDefaultConfig()
    27  			return nil
    28  		}
    29  		return fmt.Errorf("fatal error config file: %w", err)
    30  	}
    31  	err = c.Unmarshal(&config)
    32  	if err != nil {
    33  		return fmt.Errorf("unable to decode into struct, %w", err)
    34  	}
    35  	return nil
    36  }
    37  
    38  func setDefaultConfig() {
    39  	config.Version = "1"
    40  	config.Presets = []string{"base"}
    41  	config.Rules = []types.Rule{
    42  		{
    43  			Name:            "all dependencies",
    44  			PackageManagers: types.AllPackageManagers,
    45  		},
    46  	}
    47  }
    48  
    49  func GetConfig() types.Config {
    50  	return config
    51  }
    52  
    53  func GetRules() []types.Rule {
    54  	return config.Rules
    55  }