Skip to content

Commit de4a21f

Browse files
authored
Refactor INI package (first step) (#25024)
The INI package has many bugs and quirks, and in fact it is unmaintained. This PR is the first step for the INI package refactoring: * Use Gitea's "config_provider" to provide INI access * Deprecate the INI package by golangci.yml rule
1 parent 7a58733 commit de4a21f

File tree

11 files changed

+219
-99
lines changed

11 files changed

+219
-99
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ linters-settings:
8686
- io/ioutil: "use os or io instead"
8787
- golang.org/x/exp: "it's experimental and unreliable."
8888
- code.gitea.io/gitea/modules/git/internal: "do not use the internal package, use AddXxx function instead"
89+
- gopkg.in/ini.v1: "do not use the ini package, use gitea's config system instead"
8990

9091
issues:
9192
max-issues-per-linter: 0

build/backport-locales.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15-
"gopkg.in/ini.v1"
15+
"code.gitea.io/gitea/modules/setting"
1616
)
1717

1818
func main() {
@@ -22,25 +22,21 @@ func main() {
2222
os.Exit(1)
2323
}
2424

25-
ini.PrettyFormat = false
2625
mustNoErr := func(err error) {
2726
if err != nil {
2827
panic(err)
2928
}
3029
}
31-
collectInis := func(ref string) map[string]*ini.File {
32-
inis := map[string]*ini.File{}
30+
collectInis := func(ref string) map[string]setting.ConfigProvider {
31+
inis := map[string]setting.ConfigProvider{}
3332
err := filepath.WalkDir("options/locale", func(path string, d os.DirEntry, err error) error {
3433
if err != nil {
3534
return err
3635
}
3736
if d.IsDir() || !strings.HasSuffix(d.Name(), ".ini") {
3837
return nil
3938
}
40-
cfg, err := ini.LoadSources(ini.LoadOptions{
41-
IgnoreInlineComment: true,
42-
UnescapeValueCommentSymbols: true,
43-
}, path)
39+
cfg, err := setting.NewConfigProviderForLocale(path)
4440
mustNoErr(err)
4541
inis[path] = cfg
4642
fmt.Printf("collecting: %s @ %s\n", path, ref)

contrib/environment-to-ini/environment-to-ini.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99

1010
"code.gitea.io/gitea/modules/log"
1111
"code.gitea.io/gitea/modules/setting"
12-
"code.gitea.io/gitea/modules/util"
1312

1413
"github.com/urfave/cli"
15-
"gopkg.in/ini.v1"
1614
)
1715

1816
// EnvironmentPrefix environment variables prefixed with this represent ini values to write
@@ -97,19 +95,10 @@ func runEnvironmentToIni(c *cli.Context) error {
9795
providedWorkPath := c.String("work-path")
9896
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
9997

100-
cfg := ini.Empty()
101-
confFileExists, err := util.IsFile(setting.CustomConf)
98+
cfg, err := setting.NewConfigProviderFromFile(&setting.Options{CustomConf: setting.CustomConf, AllowEmpty: true})
10299
if err != nil {
103-
log.Fatal("Unable to check if %s is a file. Error: %v", setting.CustomConf, err)
100+
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
104101
}
105-
if confFileExists {
106-
if err := cfg.Append(setting.CustomConf); err != nil {
107-
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
108-
}
109-
} else {
110-
log.Warn("Custom config '%s' not found, ignore this if you're running first time", setting.CustomConf)
111-
}
112-
cfg.NameMapper = ini.SnackCase
113102

114103
prefixGitea := c.String("prefix") + "__"
115104
suffixFile := "__FILE"

modules/repository/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"code.gitea.io/gitea/modules/timeutil"
2828
"code.gitea.io/gitea/modules/util"
2929

30-
"gopkg.in/ini.v1"
30+
"gopkg.in/ini.v1" //nolint:depguard
3131
)
3232

3333
/*
@@ -241,7 +241,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
241241
// cleanUpMigrateGitConfig removes mirror info which prevents "push --all".
242242
// This also removes possible user credentials.
243243
func cleanUpMigrateGitConfig(configPath string) error {
244-
cfg, err := ini.Load(configPath)
244+
cfg, err := ini.Load(configPath) // FIXME: the ini package doesn't really work with git config files
245245
if err != nil {
246246
return fmt.Errorf("open config file: %w", err)
247247
}

modules/setting/config_env.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"strings"
1111

1212
"code.gitea.io/gitea/modules/log"
13-
14-
"gopkg.in/ini.v1"
1513
)
1614

1715
const escapeRegexpString = "_0[xX](([0-9a-fA-F][0-9a-fA-F])+)_"
@@ -89,7 +87,7 @@ func decodeEnvironmentKey(prefixGitea, suffixFile, envKey string) (ok bool, sect
8987
return ok, section, key, useFileValue
9088
}
9189

92-
func EnvironmentToConfig(cfg *ini.File, prefixGitea, suffixFile string, envs []string) (changed bool) {
90+
func EnvironmentToConfig(cfg ConfigProvider, prefixGitea, suffixFile string, envs []string) (changed bool) {
9391
for _, kv := range envs {
9492
idx := strings.IndexByte(kv, '=')
9593
if idx < 0 {

modules/setting/config_env_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11-
"gopkg.in/ini.v1"
1211
)
1312

1413
func TestDecodeEnvSectionKey(t *testing.T) {
@@ -71,15 +70,15 @@ func TestDecodeEnvironmentKey(t *testing.T) {
7170
}
7271

7372
func TestEnvironmentToConfig(t *testing.T) {
74-
cfg := ini.Empty()
73+
cfg, _ := NewConfigProviderFromData("")
7574

7675
changed := EnvironmentToConfig(cfg, "GITEA__", "__FILE", nil)
7776
assert.False(t, changed)
7877

79-
cfg, err := ini.Load([]byte(`
78+
cfg, err := NewConfigProviderFromData(`
8079
[sec]
8180
key = old
82-
`))
81+
`)
8382
assert.NoError(t, err)
8483

8584
changed = EnvironmentToConfig(cfg, "GITEA__", "__FILE", []string{"GITEA__sec__key=new"})

0 commit comments

Comments
 (0)