mirror of https://github.com/go-gitea/gitea.git
Merge 9acb9377af into 01351cc6c7
commit
1fbc9ecfa2
@ -0,0 +1,31 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package env
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Filter(include, exclude []string) {
|
||||||
|
env := os.Environ()
|
||||||
|
for _, v := range env {
|
||||||
|
included := false
|
||||||
|
for _, i := range include {
|
||||||
|
if strings.HasPrefix(v, i) {
|
||||||
|
included = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !included {
|
||||||
|
for _, e := range exclude {
|
||||||
|
if strings.HasPrefix(v, e) {
|
||||||
|
parts := strings.SplitN(v, "=", 2)
|
||||||
|
os.Unsetenv(parts[0])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package env
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFilter(t *testing.T) {
|
||||||
|
t.Setenv("GITEA_FOO", "bar")
|
||||||
|
t.Setenv("FOO", "bar")
|
||||||
|
Filter([]string{}, []string{"GITEA_"})
|
||||||
|
if os.Getenv("GITEA_FOO") != "" {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
if os.Getenv("FOO") != "bar" {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Setenv("GITEA_TEST_FOO", "bar")
|
||||||
|
t.Setenv("GITEA_BAR", "foo")
|
||||||
|
t.Setenv("GITEA_BAR_BAZ", "foo")
|
||||||
|
t.Setenv("GITEA_BAZ", "huz")
|
||||||
|
Filter([]string{"GITEA_TEST_", "GITEA_BAR="}, []string{"GITEA_"})
|
||||||
|
if os.Getenv("GITEA_BAR") != "foo" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if os.Getenv("GITEA_TEST_FOO") != "bar" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if os.Getenv("GITEA_BAZ") != "" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if os.Getenv("GITEA_BAR_BAZ") != "" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue