iconfig.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/urfave/cli/v2"
  5. "gopkg.in/yaml.v3"
  6. "os"
  7. "reflect"
  8. )
  9. const cliMetaKey = "cli.config"
  10. type IConfig interface {
  11. Invalidate() error
  12. }
  13. func Invalidate(config IConfig) error {
  14. if config == nil {
  15. return fmt.Errorf("config must not be a nil")
  16. }
  17. val := reflect.ValueOf(config)
  18. if val.Kind() != reflect.Struct {
  19. if val.IsZero() {
  20. return fmt.Errorf("config is empty")
  21. }
  22. val = reflect.ValueOf(config).Elem()
  23. }
  24. for i := 0; i < val.NumField(); i++ {
  25. if !val.Field(i).CanInterface() || val.Field(i).IsZero() {
  26. continue
  27. }
  28. if elm, ok := val.Field(i).Interface().(IConfig); ok {
  29. if er := elm.Invalidate(); er != nil {
  30. return er
  31. }
  32. }
  33. }
  34. return nil
  35. }
  36. func ReadConfig[T IConfig](path string) (*T, error) {
  37. var (
  38. cfg = new(T)
  39. fd []byte
  40. err error
  41. )
  42. if fd, err = os.ReadFile(path); err != nil {
  43. return nil, err
  44. }
  45. if err = yaml.Unmarshal(fd, cfg); err != nil {
  46. return nil, err
  47. }
  48. if err = Invalidate(*cfg); err != nil {
  49. return nil, err
  50. }
  51. return cfg, nil
  52. }
  53. func LoadConfig[T IConfig](ctx *cli.Context, path cli.Path) error {
  54. cfg, err := ReadConfig[T](path)
  55. if err != nil {
  56. return fmt.Errorf("could not load config from: %s; %v", path, err)
  57. }
  58. if cfg == nil {
  59. return fmt.Errorf("could not load config from: %s", path)
  60. }
  61. ctx.App.Metadata[cliMetaKey] = cfg
  62. return nil
  63. }
  64. func Config[T IConfig](ctx *cli.Context) *T {
  65. cfg, ok := ctx.App.Metadata["cli.config"].(*T)
  66. if !ok {
  67. return nil
  68. }
  69. return cfg
  70. }