cfg.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "fmt"
  4. se "git.swzry.com/ProjectNagae/saki-v0/engine"
  5. "github.com/BurntSushi/toml"
  6. "github.com/urfave/cli/v2"
  7. "os"
  8. )
  9. var _ se.Config = (*Config)(nil)
  10. type TomlConfigDef struct {
  11. VmConfig *VmConfigDef `toml:"vm"`
  12. }
  13. type Config struct {
  14. *TomlConfigDef
  15. willPrintResults bool
  16. isStrictMode bool
  17. suppressWarning bool
  18. }
  19. func (c *Config) RuntimeWarning(err error) {
  20. if !c.suppressWarning {
  21. RuntimeWarning(err)
  22. }
  23. }
  24. func (c *Config) IsStrictMode() bool {
  25. return c.isStrictMode
  26. }
  27. func (c *Config) WillPrintResults() bool {
  28. return c.willPrintResults
  29. }
  30. func (c *Config) SetArgsSpecifiedItemsForExec(cx *cli.Context) {
  31. strictMode := cx.Bool("strict-mode")
  32. noResultPrint := cx.Bool("no-js-result-print")
  33. suppressWarning := cx.Bool("suppress-warn")
  34. c.isStrictMode = strictMode
  35. c.willPrintResults = !noResultPrint
  36. c.suppressWarning = suppressWarning
  37. }
  38. func (c *TomlConfigDef) GlobalModulePath() []string {
  39. return c.VmConfig.GlobalModuleFolders
  40. }
  41. type VmConfigDef struct {
  42. GlobalModuleFolders []string `toml:"global_module_folders"`
  43. }
  44. func LoadConfig(c *cli.Context) (*Config, error) {
  45. cfgFile := c.String("config")
  46. if cfgFile == "" {
  47. cfgFile = "/nagae/elip4ng/config/sakish.toml"
  48. }
  49. cfgBin, err := os.ReadFile(cfgFile)
  50. if err != nil {
  51. return nil, fmt.Errorf("failed to read config file at %s:\n%w", cfgFile, err)
  52. }
  53. var tcfg *TomlConfigDef
  54. err = toml.Unmarshal(cfgBin, &tcfg)
  55. if err != nil {
  56. return nil, fmt.Errorf("failed to parse config file at %s:\n%w", cfgFile, err)
  57. }
  58. cfg := Config{
  59. TomlConfigDef: tcfg,
  60. }
  61. return &cfg, nil
  62. }