|
1 | 1 | package bootstrap |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
4 | 6 | "os" |
5 | 7 |
|
6 | | - "github.com/pubgo/dix/dixinternal" |
7 | | - "github.com/pubgo/funk/assert" |
8 | | - "github.com/pubgo/funk/config" |
9 | | - "github.com/pubgo/funk/log" |
10 | | - "github.com/pubgo/funk/pathutil" |
11 | | - "github.com/rs/zerolog" |
| 8 | + "github.com/pubgo/funk/v2/assert" |
| 9 | + "github.com/pubgo/funk/v2/config" |
| 10 | + "github.com/pubgo/funk/v2/env" |
| 11 | + "github.com/pubgo/funk/v2/log" |
| 12 | + "github.com/pubgo/funk/v2/pathutil" |
| 13 | + "github.com/pubgo/funk/v2/running" |
12 | 14 | "gopkg.in/yaml.v3" |
13 | 15 |
|
| 16 | + "github.com/pubgo/fastcommit/cmds/fastcommitcmd" |
14 | 17 | "github.com/pubgo/fastcommit/configs" |
15 | 18 | "github.com/pubgo/fastcommit/utils" |
16 | 19 | ) |
17 | 20 |
|
18 | | -type ConfigProvider struct { |
19 | | - Version *configs.Version `yaml:"version"` |
20 | | - OpenaiConfig *utils.OpenaiConfig `yaml:"openai"` |
| 21 | +type configProvider struct { |
| 22 | + Version *configs.Version `yaml:"version"` |
| 23 | + OpenaiConfig *utils.OpenaiConfig `yaml:"openai"` |
| 24 | + CommitConfig *fastcommitcmd.Config `yaml:"commit"` |
21 | 25 | } |
22 | 26 |
|
23 | 27 | func initConfig() { |
24 | | - configs.InitEnv() |
| 28 | + slog.SetDefault(slog.New(log.NewSlog(log.GetLogger("")))) |
| 29 | + log.SetEnableChecker(func(ctx context.Context, lvl log.Level, name, message string, fields log.Fields) bool { |
| 30 | + if running.Debug() { |
| 31 | + return true |
| 32 | + } |
25 | 33 |
|
26 | | - dixinternal.SetLog(func(logger log.Logger) log.Logger { |
27 | | - if configs.IsDebug() { |
28 | | - return logger.WithLevel(zerolog.DebugLevel) |
| 34 | + if name == "dix" || name == "env" || fields["module"] == "env" { |
| 35 | + return false |
29 | 36 | } |
30 | | - return logger.WithLevel(zerolog.InfoLevel) |
| 37 | + return true |
31 | 38 | }) |
32 | 39 |
|
| 40 | + env.MustSet("LC_ALL", "C") |
| 41 | + env.LoadFiles(configs.GetLocalEnvPath()).Must() |
| 42 | + |
33 | 43 | configPath := configs.GetConfigPath() |
34 | | - defaultConfigData := configs.GetDefaultConfig() |
| 44 | + envPath := configs.GetEnvPath() |
35 | 45 | if pathutil.IsNotExist(configPath) { |
36 | | - assert.Must(os.WriteFile(configPath, defaultConfigData, 0644)) |
| 46 | + assert.Must(os.WriteFile(configPath, configs.GetDefaultConfig(), 0644)) |
| 47 | + assert.Must(os.WriteFile(envPath, configs.GetEnvConfig(), 0644)) |
37 | 48 | return |
38 | 49 | } |
39 | 50 |
|
40 | | - var cfg ConfigProvider |
| 51 | + type versionConfigProvider struct { |
| 52 | + Version *configs.Version `yaml:"version"` |
| 53 | + } |
| 54 | + var cfg versionConfigProvider |
41 | 55 | config.LoadFromPath(&cfg, configPath) |
42 | 56 |
|
43 | | - var defaultCfg ConfigProvider |
| 57 | + var defaultCfg versionConfigProvider |
| 58 | + defaultConfigData := configs.GetDefaultConfig() |
44 | 59 | assert.Must(yaml.Unmarshal(defaultConfigData, &defaultCfg)) |
45 | 60 | if cfg.Version == nil || cfg.Version.Name == "" || defaultCfg.Version.Name != cfg.Version.Name { |
46 | 61 | assert.Must(os.WriteFile(configPath, defaultConfigData, 0644)) |
| 62 | + assert.Must(os.WriteFile(envPath, configs.GetEnvConfig(), 0644)) |
47 | 63 | } |
48 | 64 |
|
49 | 65 | config.SetConfigPath(configPath) |
|
0 commit comments