feat: customize local environment name #313

This commit is contained in:
Jacky 2024-05-07 11:20:35 +08:00
parent 4c74bc8619
commit 1e9de6f21b
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
13 changed files with 78 additions and 36 deletions

View file

@ -20,7 +20,8 @@ type Server struct {
GithubProxy string `json:"github_proxy" binding:"omitempty,url"`
CertRenewalInterval int `json:"cert_renewal_interval" binding:"min=7,max=21"`
RecursiveNameservers []string `json:"recursive_nameservers" binding:"omitempty,dive,hostname_port"`
SkipInstallation bool `json:"skip_installation"`
SkipInstallation bool `json:"skip_installation" protected:"true"`
Name string `json:"name" binding:"omitempty,alpha_num_dash_dot"`
}
func (s *Server) GetCADir() string {

View file

@ -6,7 +6,8 @@ import (
"gopkg.in/ini.v1"
"log"
"os"
"strings"
"reflect"
"strings"
"time"
)
@ -69,19 +70,7 @@ func MapTo() {
}
}
func mapTo(section string, v interface{}) {
err := Conf.Section(section).MapTo(v)
if err != nil {
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
}
}
func reflectFrom(section string, v interface{}) {
err := Conf.Section(section).ReflectFrom(v)
if err != nil {
log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err)
}
}
func Save() (err error) {
for k, v := range sections {
@ -95,6 +84,33 @@ func Save() (err error) {
return
}
func ProtectedFill(targetSettings interface{}, newSettings interface{}) {
s := reflect.TypeOf(targetSettings).Elem()
vt := reflect.ValueOf(targetSettings).Elem()
vn := reflect.ValueOf(newSettings).Elem()
// copy the values from new to target settings if it is not protected
for i := 0; i < s.NumField(); i++ {
if s.Field(i).Tag.Get("protected") != "true" {
vt.Field(i).Set(vn.Field(i))
}
}
}
func mapTo(section string, v interface{}) {
err := Conf.Section(section).MapTo(v)
if err != nil {
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
}
}
func reflectFrom(section string, v interface{}) {
err := Conf.Section(section).ReflectFrom(v)
if err != nil {
log.Fatalf("Cfg.ReflectFrom %s err: %v", section, err)
}
}
func parseEnv(ptr interface{}, prefix string) {
err := env.ParseWithOptions(ptr, env.Options{
Prefix: EnvPrefix + prefix,
@ -105,3 +121,5 @@ func parseEnv(ptr interface{}, prefix string) {
log.Fatalf("settings.parseEnv: %v\n", err)
}
}

View file

@ -26,6 +26,7 @@ func TestSetup(t *testing.T) {
_ = os.Setenv("NGINX_UI_SERVER_CERT_RENEWAL_INTERVAL", "14")
_ = os.Setenv("NGINX_UI_SERVER_RECURSIVE_NAMESERVERS", "8.8.8.8")
_ = os.Setenv("NGINX_UI_SERVER_SKIP_INSTALLATION", "true")
_ = os.Setenv("NGINX_UI_SERVER_NAME", "test")
_ = os.Setenv("NGINX_UI_NGINX_ACCESS_LOG_PATH", "/tmp/nginx/access.log")
_ = os.Setenv("NGINX_UI_NGINX_ERROR_LOG_PATH", "/tmp/nginx/error.log")
@ -70,6 +71,7 @@ func TestSetup(t *testing.T) {
assert.Equal(t, 14, ServerSettings.CertRenewalInterval)
assert.Equal(t, []string{"8.8.8.8"}, ServerSettings.RecursiveNameservers)
assert.Equal(t, true, ServerSettings.SkipInstallation)
assert.Equal(t, "test", ServerSettings.Name)
assert.Equal(t, "/tmp/nginx/access.log", NginxSettings.AccessLogPath)
assert.Equal(t, "/tmp/nginx/error.log", NginxSettings.ErrorLogPath)