1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

[BRANDING] alias {FORGEJO,GITEA}_{CUSTOM,WORK_DIR}

FORGEJO_* environment variables are set to the corresponding GITEA_*
variable when the cli starts. This approach is intended to minimize
the conflicts on rebase. All occurences of GITEA_* are left untouched
in the codebase and they are only changed to FORGEJO_* if exposed to
the user.

(cherry picked from commit e466f9d10e)
(cherry picked from commit e33e95931b)
(cherry picked from commit 2cfc6519b7)
(cherry picked from commit af8864373a)
(cherry picked from commit a0550ff339)
(cherry picked from commit 24dc0a5191)
(cherry picked from commit e255eea2b4)
(cherry picked from commit 0c4f5afa7a)
(cherry picked from commit 42fce708d0)
(cherry picked from commit e7278c3c22)
(cherry picked from commit 0fb9ed7e0e)
(cherry picked from commit a98308aa4d)
(cherry picked from commit b8695fcbe0)
(cherry picked from commit 4aee8719f5)
(cherry picked from commit 1c503c1ba7)
(cherry picked from commit cf1ed8551e)
(cherry picked from commit c52459b088)
(cherry picked from commit 92cac277b2)
(cherry picked from commit 2c744f1118)
This commit is contained in:
Loïc Dachary 2023-02-24 13:38:29 +01:00 committed by Earl Warren
parent 8b59e016c7
commit 989a98a8c5
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 122 additions and 4 deletions

16
main.go
View file

@ -83,6 +83,14 @@ DEFAULT CONFIGURATION:
}, },
} }
func forgejoEnv() {
for _, k := range []string{"CUSTOM", "WORK_DIR"} {
if v, ok := os.LookupEnv("FORGEJO_" + k); ok {
os.Setenv("GITEA_"+k, v)
}
}
}
func main() { func main() {
path, err := os.Executable() path, err := os.Executable()
if err != nil { if err != nil {
@ -117,10 +125,12 @@ func main() {
} }
func mainApp(subCmds ...cli.Command) { func mainApp(subCmds ...cli.Command) {
forgejoEnv()
app := cli.NewApp() app := cli.NewApp()
app.Name = "Gitea" app.Name = "Forgejo"
app.Usage = "A painless self-hosted Git service" app.Usage = "Beyond coding. We forge."
app.Description = `By default, Gitea will start serving using the web-server with no argument, which can alternatively be run by running the subcommand "web".` app.Description = `By default, forgejo will start serving using the web-server with no
argument - which can alternatively be run by running the subcommand web.`
app.Version = Version + formatBuiltWith() app.Version = Version + formatBuiltWith()
app.EnableBashCompletion = true app.EnableBashCompletion = true

View file

@ -129,6 +129,14 @@ func InitWorkPathAndCfgProvider(getEnvFn func(name string) string, args ArgWorkP
} }
} }
envWorkPath = getEnvFn("FORGEJO_WORK_DIR")
if envWorkPath != "" {
tmpWorkPath.Set(envWorkPath)
if !filepath.IsAbs(tmpWorkPath.Value) {
log.Fatal("FORGEJO_WORK_DIR (work path) must be absolute path")
}
}
envCustomPath := getEnvFn("GITEA_CUSTOM") envCustomPath := getEnvFn("GITEA_CUSTOM")
if envCustomPath != "" { if envCustomPath != "" {
tmpCustomPath.Set(envCustomPath) tmpCustomPath.Set(envCustomPath)
@ -136,6 +144,14 @@ func InitWorkPathAndCfgProvider(getEnvFn func(name string) string, args ArgWorkP
log.Fatal("GITEA_CUSTOM (custom path) must be absolute path") log.Fatal("GITEA_CUSTOM (custom path) must be absolute path")
} }
} }
envCustomPath = getEnvFn("FORGEJO_CUSTOM")
if envCustomPath != "" {
tmpCustomPath.Set(envCustomPath)
if !filepath.IsAbs(tmpCustomPath.Value) {
log.Fatal("FORGEJO_CUSTOM (custom path) must be absolute path")
}
}
} }
readFromArgs := func() { readFromArgs := func() {
@ -180,7 +196,7 @@ func InitWorkPathAndCfgProvider(getEnvFn func(name string) string, args ArgWorkP
log.Fatal("WORK_PATH in %q must be absolute path", configWorkPath) log.Fatal("WORK_PATH in %q must be absolute path", configWorkPath)
} }
configWorkPath = filepath.Clean(configWorkPath) configWorkPath = filepath.Clean(configWorkPath)
if tmpWorkPath.Value != "" && (getEnvFn("GITEA_WORK_DIR") != "" || args.WorkPath != "") { if tmpWorkPath.Value != "" && (getEnvFn("GITEA_WORK_DIR") != "" || getEnvFn("FORGEJO_WORK_DIR") != "" || args.WorkPath != "") {
fi1, err1 := os.Stat(tmpWorkPath.Value) fi1, err1 := os.Stat(tmpWorkPath.Value)
fi2, err2 := os.Stat(configWorkPath) fi2, err2 := os.Stat(configWorkPath)
if err1 != nil || err2 != nil || !os.SameFile(fi1, fi2) { if err1 != nil || err2 != nil || !os.SameFile(fi1, fi2) {

View file

@ -60,6 +60,22 @@ func TestInitWorkPathAndCommonConfig(t *testing.T) {
assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf) assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf)
}) })
t.Run("WorkDir(env)", func(t *testing.T) {
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{})
assert.Equal(t, dirBar, AppWorkPath)
assert.Equal(t, fp(dirBar, "custom"), CustomPath)
assert.Equal(t, fp(dirBar, "custom/conf/app.ini"), CustomConf)
})
t.Run("WorkDir(env,arg)", func(t *testing.T) {
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirXxx})
assert.Equal(t, dirXxx, AppWorkPath)
assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
assert.Equal(t, fp(dirXxx, "custom/conf/app.ini"), CustomConf)
})
t.Run("CustomPath(env)", func(t *testing.T) { t.Run("CustomPath(env)", func(t *testing.T) {
testInit(dirFoo, "", "") testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{}) InitWorkPathAndCommonConfig(envVars{"GITEA_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{})
@ -76,6 +92,22 @@ func TestInitWorkPathAndCommonConfig(t *testing.T) {
assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf) assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf)
}) })
t.Run("CustomPath(env)", func(t *testing.T) {
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{})
assert.Equal(t, dirFoo, AppWorkPath)
assert.Equal(t, fp(dirBar, "custom1"), CustomPath)
assert.Equal(t, fp(dirBar, "custom1/conf/app.ini"), CustomConf)
})
t.Run("CustomPath(env,arg)", func(t *testing.T) {
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": fp(dirBar, "custom1")}.Getenv, ArgWorkPathAndCustomConf{CustomPath: "custom2"})
assert.Equal(t, dirFoo, AppWorkPath)
assert.Equal(t, fp(dirFoo, "custom2"), CustomPath)
assert.Equal(t, fp(dirFoo, "custom2/conf/app.ini"), CustomConf)
})
t.Run("CustomConf", func(t *testing.T) { t.Run("CustomConf", func(t *testing.T) {
testInit(dirFoo, "", "") testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: "app1.ini"}) InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: "app1.ini"})
@ -115,6 +147,32 @@ func TestInitWorkPathAndCommonConfig(t *testing.T) {
assert.True(t, AppWorkPathMismatch) assert.True(t, AppWorkPathMismatch)
}) })
t.Run("CustomConfOverrideWorkPath", func(t *testing.T) {
iniWorkPath := fp(tmpDir, "app-workpath.ini")
_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
assert.Equal(t, dirXxx, AppWorkPath)
assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
assert.Equal(t, iniWorkPath, CustomConf)
assert.False(t, AppWorkPathMismatch)
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirBar}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
assert.Equal(t, dirXxx, AppWorkPath)
assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
assert.Equal(t, iniWorkPath, CustomConf)
assert.True(t, AppWorkPathMismatch)
testInit(dirFoo, "", "")
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{WorkPath: dirBar, CustomConf: iniWorkPath})
assert.Equal(t, dirXxx, AppWorkPath)
assert.Equal(t, fp(dirXxx, "custom"), CustomPath)
assert.Equal(t, iniWorkPath, CustomConf)
assert.True(t, AppWorkPathMismatch)
})
t.Run("Builtin", func(t *testing.T) { t.Run("Builtin", func(t *testing.T) {
testInit(dirFoo, dirBar, dirXxx) testInit(dirFoo, dirBar, dirXxx)
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{}) InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
@ -148,4 +206,38 @@ func TestInitWorkPathAndCommonConfig(t *testing.T) {
assert.Equal(t, fp(dirXxx, "custom1"), CustomPath) assert.Equal(t, fp(dirXxx, "custom1"), CustomPath)
assert.Equal(t, iniWorkPath, CustomConf) assert.Equal(t, iniWorkPath, CustomConf)
}) })
t.Run("Builtin", func(t *testing.T) {
testInit(dirFoo, dirBar, dirXxx)
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
assert.Equal(t, dirFoo, AppWorkPath)
assert.Equal(t, dirBar, CustomPath)
assert.Equal(t, dirXxx, CustomConf)
testInit(dirFoo, "custom1", "cfg.ini")
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{})
assert.Equal(t, dirFoo, AppWorkPath)
assert.Equal(t, fp(dirFoo, "custom1"), CustomPath)
assert.Equal(t, fp(dirFoo, "custom1/cfg.ini"), CustomConf)
testInit(dirFoo, "custom1", "cfg.ini")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_WORK_DIR": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
assert.Equal(t, dirYyy, AppWorkPath)
assert.Equal(t, fp(dirYyy, "custom1"), CustomPath)
assert.Equal(t, fp(dirYyy, "custom1/cfg.ini"), CustomConf)
testInit(dirFoo, "custom1", "cfg.ini")
InitWorkPathAndCommonConfig(envVars{"FORGEJO_CUSTOM": dirYyy}.Getenv, ArgWorkPathAndCustomConf{})
assert.Equal(t, dirFoo, AppWorkPath)
assert.Equal(t, dirYyy, CustomPath)
assert.Equal(t, fp(dirYyy, "cfg.ini"), CustomConf)
iniWorkPath := fp(tmpDir, "app-workpath.ini")
_ = os.WriteFile(iniWorkPath, []byte("WORK_PATH="+dirXxx), 0o644)
testInit(dirFoo, "custom1", "cfg.ini")
InitWorkPathAndCommonConfig(envVars{}.Getenv, ArgWorkPathAndCustomConf{CustomConf: iniWorkPath})
assert.Equal(t, dirXxx, AppWorkPath)
assert.Equal(t, fp(dirXxx, "custom1"), CustomPath)
assert.Equal(t, iniWorkPath, CustomConf)
})
} }