github.com/topi314/pogo-icons@v0.0.0-20250403082451-64aa0bf0322a/pogoicons/config.go (about)

     1  package pogoicons
     2  
     3  import (
     4  	"fmt"
     5  	"log/slog"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/BurntSushi/toml"
    10  	"github.com/disgoorg/snowflake/v2"
    11  )
    12  
    13  func LoadConfig(cfgPath string) (Config, error) {
    14  	file, err := os.Open(cfgPath)
    15  	if err != nil {
    16  		return Config{}, fmt.Errorf("failed to open config file: %w", err)
    17  	}
    18  	defer func() {
    19  		_ = file.Close()
    20  	}()
    21  
    22  	cfg := defaultConfig()
    23  	if _, err = toml.NewDecoder(file).Decode(&cfg); err != nil {
    24  		return Config{}, fmt.Errorf("failed to decode config file: %w", err)
    25  	}
    26  
    27  	return cfg, nil
    28  }
    29  
    30  func defaultConfig() Config {
    31  	return Config{
    32  		Repository: "https://github.com/PokeAPI/api-data",
    33  		Bot: BotConfig{
    34  			Token:        "",
    35  			GuildIDs:     nil,
    36  			SyncCommands: true,
    37  		},
    38  		Log: LogConfig{
    39  			Level:     slog.LevelInfo,
    40  			Format:    LogFormatText,
    41  			AddSource: false,
    42  			NoColor:   false,
    43  		},
    44  	}
    45  }
    46  
    47  type Config struct {
    48  	Repository string    `toml:"repository"`
    49  	Bot        BotConfig `toml:"bot"`
    50  	Log        LogConfig `toml:"log"`
    51  }
    52  
    53  func (c Config) String() string {
    54  	return fmt.Sprintf("Repository: %s\nBot: %s\nLog: %s",
    55  		c.Repository,
    56  		c.Bot,
    57  		c.Log,
    58  	)
    59  }
    60  
    61  type LogFormat string
    62  
    63  const (
    64  	LogFormatJSON   LogFormat = "json"
    65  	LogFormatText   LogFormat = "text"
    66  	LogFormatLogFMT LogFormat = "log-fmt"
    67  )
    68  
    69  type LogConfig struct {
    70  	Level     slog.Level `toml:"level"`
    71  	Format    LogFormat  `toml:"format"`
    72  	AddSource bool       `toml:"add_source"`
    73  	NoColor   bool       `toml:"no_color"`
    74  }
    75  
    76  func (c LogConfig) String() string {
    77  	return fmt.Sprintf("\n Level: %s\n Format: %s\n AddSource: %t\n NoColor: %t",
    78  		c.Level,
    79  		c.Format,
    80  		c.AddSource,
    81  		c.NoColor,
    82  	)
    83  }
    84  
    85  type BotConfig struct {
    86  	Token        string         `toml:"token"`
    87  	GuildIDs     []snowflake.ID `toml:"guild_ids"`
    88  	SyncCommands bool           `toml:"sync_commands"`
    89  }
    90  
    91  func (c BotConfig) String() string {
    92  	return fmt.Sprintf("\n Token: %s\n GuildIDs: %s\n SyncCommands: %t",
    93  		strings.Repeat("*", len(c.Token)),
    94  		c.GuildIDs,
    95  		c.SyncCommands,
    96  	)
    97  }
    98  
    99  type AssetConfig struct {
   100  	Events    []EventConfig    `toml:"events"`
   101  	Cosmetics []CosmeticConfig `toml:"cosmetics"`
   102  }
   103  
   104  type EventConfig struct {
   105  	Name                string  `toml:"name"`
   106  	Background          string  `toml:"background"`
   107  	BackgroundIcon      string  `toml:"background_icon"`
   108  	BackgroundIconScale float64 `toml:"background_icon_scale"`
   109  	PokemonScale        float64 `toml:"pokemon_scale"`
   110  }
   111  
   112  type CosmeticConfig struct {
   113  	Name  string  `toml:"name"`
   114  	Image string  `toml:"image"`
   115  	Scale float64 `toml:"scale"`
   116  }