diff --git a/packages/koi/config/use_config_env.go b/packages/koi/config/use_config_env.go new file mode 100644 index 00000000..2c69b00e --- /dev/null +++ b/packages/koi/config/use_config_env.go @@ -0,0 +1,30 @@ +package config + +import ( + envUtil "koi/util/env" + "strings" +) + +func UseConfigEnv(env *[]string) { + if Config.Env == nil { + return + } + + for _, e := range Config.Env { + if len(e) == 0 { + continue + } + + i := strings.Index(e, "=") + var k, v string + if i >= 0 { + k = e[:i] + v = e[i+1:] + } else { + k = e + v = "" + } + + envUtil.UseEnv(env, k, v) + } +} diff --git a/packages/koi/config/useenv.go b/packages/koi/config/useenv.go deleted file mode 100644 index 6b08ba98..00000000 --- a/packages/koi/config/useenv.go +++ /dev/null @@ -1,42 +0,0 @@ -package config - -import "strings" - -func UseConfigEnv(env []string) []string { - if Config.Env == nil { - return env - } - - for _, eAddSrc := range Config.Env { - spl := strings.Split(eAddSrc, "=") - if len(spl) == 0 { - continue - } - var eAdd string - eKey := spl[0] + "=" - if len(spl) == 1 { - eAdd = eKey - } else { - eAdd = eAddSrc - } - - for { - notFound := true - for i, e := range env { - if strings.HasPrefix(e, eKey) { - env = append(env[:i], env[i+1:]...) - notFound = false - break - } - } - - if notFound { - break - } - } - - env = append(env, eAdd) - } - - return env -} diff --git a/packages/koi/daemon/node_cmd.go b/packages/koi/daemon/node_cmd.go index fed8b76b..457e6cb2 100644 --- a/packages/koi/daemon/node_cmd.go +++ b/packages/koi/daemon/node_cmd.go @@ -4,6 +4,7 @@ import ( "bufio" "koi/config" "koi/util" + envUtil "koi/util/env" l "koi/util/logger" "koi/util/strutil" "koi/util/supcolor" @@ -81,88 +82,23 @@ func CreateNodeCmd( if config.Config.UseDataHome { l.Debug("Now replace HOME/USERPROFILE.") - for { - notFound := true - for i, e := range env { - if strings.HasPrefix(e, "HOME=") || strings.HasPrefix(e, "USERPROFILE=") { - env = append(env[:i], env[i+1:]...) - notFound = false - break - } - } - - if notFound { - break - } - } - - env = append(env, "HOME="+config.Config.InternalHomeDir) - env = append(env, "USERPROFILE="+config.Config.InternalHomeDir) + envUtil.UseEnv(&env, "HOME", config.Config.InternalHomeDir) + envUtil.UseEnv(&env, "USERPROFILE", config.Config.InternalHomeDir) l.Debugf("HOME=%s", config.Config.InternalHomeDir) if runtime.GOOS == "windows" { l.Debug("Now replace APPDATA.") - for { - notFound := true - for i, e := range env { - if strings.HasPrefix(e, "APPDATA=") { - env = append(env[:i], env[i+1:]...) - notFound = false - break - } - } - - if notFound { - break - } - } - - roamingPath := filepath.Join(config.Config.InternalHomeDir, "AppData", "Roaming") - env = append(env, "APPDATA="+roamingPath) - l.Debugf("APPDATA=%s", roamingPath) - - l.Debug("Now replace LOCALAPPDATA.") - for { - notFound := true - for i, e := range env { - if strings.HasPrefix(e, "LOCALAPPDATA=") { - env = append(env[:i], env[i+1:]...) - notFound = false - break - } - } - - if notFound { - break - } - } - localPath := filepath.Join(config.Config.InternalHomeDir, "AppData", "Local") - env = append(env, "LOCALAPPDATA="+localPath) + envUtil.UseEnv(&env, "LOCALAPPDATA", localPath) l.Debugf("LOCALAPPDATA=%s", localPath) } } if config.Config.UseDataTemp { l.Debug("Now replace TMPDIR/TEMP/TMP.") - for { - notFound := true - for i, e := range env { - if strings.HasPrefix(e, "TMPDIR=") || strings.HasPrefix(e, "TEMP=") || strings.HasPrefix(e, "TMP=") { - env = append(env[:i], env[i+1:]...) - notFound = false - break - } - } - - if notFound { - break - } - } - - env = append(env, "TMPDIR="+config.Config.InternalTempDir) - env = append(env, "TEMP="+config.Config.InternalTempDir) - env = append(env, "TMP="+config.Config.InternalTempDir) + envUtil.UseEnv(&env, "TMPDIR", config.Config.InternalTempDir) + envUtil.UseEnv(&env, "TEMP", config.Config.InternalTempDir) + envUtil.UseEnv(&env, "TMP", config.Config.InternalTempDir) l.Debugf("TEMP=%s", config.Config.InternalTempDir) } @@ -201,8 +137,8 @@ func CreateNodeCmd( env = append(env, koiEnv) l.Debug(koiEnv) - env = supcolor.UseEnvironColor(env, supcolor.Stderr) - env = config.UseConfigEnv(env) + supcolor.UseColorEnv(&env, supcolor.Stderr) + config.UseConfigEnv(&env) l.Debugf("PWD=%s", dir) diff --git a/packages/koi/util/env/useenv.go b/packages/koi/util/env/useenv.go new file mode 100644 index 00000000..debcf3a4 --- /dev/null +++ b/packages/koi/util/env/useenv.go @@ -0,0 +1,34 @@ +package env + +import ( + "fmt" + "strings" +) + +func UseEnv(env *[]string, key string, value string) { + RemoveEnv(env, key) + *env = append(*env, fmt.Sprintf("%s=%s", key, value)) +} + +func RemoveEnv(env *[]string, key string) { + removeEnvIntl(env, key) + removeEnvIntl(env, strings.ToUpper(key)) + removeEnvIntl(env, strings.ToLower(key)) +} + +func removeEnvIntl(env *[]string, key string) { + for { + notFound := true + for i, e := range *env { + if strings.HasPrefix(e, key+"=") { + *env = append((*env)[:i], (*env)[i+1:]...) + notFound = false + break + } + } + + if notFound { + break + } + } +} diff --git a/packages/koi/util/supcolor/use_color_env.go b/packages/koi/util/supcolor/use_color_env.go new file mode 100644 index 00000000..9831a344 --- /dev/null +++ b/packages/koi/util/supcolor/use_color_env.go @@ -0,0 +1,36 @@ +// Package supcolor determine whether the io supports color. +package supcolor + +import ( + envUtil "koi/util/env" + "strconv" +) + +func UseColorEnv(env *[]string, mode int8) { + // FORCE_COLOR + envUtil.UseEnv(env, "FORCE_COLOR", strconv.Itoa(int(mode))) + + // COLORTERM + if mode >= 3 { + envUtil.UseEnv(env, "COLORTERM", "truecolor") + } + + // TERM + if mode >= 3 { + envUtil.UseEnv(env, "TERM", "xterm-truecolor") + } else if mode == 2 { + envUtil.UseEnv(env, "TERM", "xterm-256color") + } else if mode == 1 { + envUtil.UseEnv(env, "TERM", "xterm-color") + } else { + envUtil.UseEnv(env, "TERM", "dumb") + } + + // CLICOLOR + if mode >= 1 { + envUtil.UseEnv(env, "CLICOLOR", "1") + } + + // TERM_PROGRAM + envUtil.UseEnv(env, "TERM_PROGRAM", "Koi") +} diff --git a/packages/koi/util/supcolor/usecolor.go b/packages/koi/util/supcolor/usecolor.go deleted file mode 100644 index 7294b280..00000000 --- a/packages/koi/util/supcolor/usecolor.go +++ /dev/null @@ -1,57 +0,0 @@ -// Package supcolor determine whether the io supports color. -package supcolor - -import ( - "fmt" - "strings" -) - -func UseEnvironColor(env []string, mode int8) []string { - for { - notFound := true - for i, e := range env { - if strings.HasPrefix(e, "FORCE_COLOR=") || - strings.HasPrefix(e, "COLORTERM=") || - strings.HasPrefix(e, "TERM=") || - strings.HasPrefix(e, "CLICOLOR=") || - strings.HasPrefix(e, "TERM_PROGRAM=") { - env = append(env[:i], env[i+1:]...) - notFound = false - break - } - } - - if notFound { - break - } - } - - // FORCE_COLOR - env = append(env, fmt.Sprintf("FORCE_COLOR=%d", mode)) - - // COLORTERM - if mode >= 3 { - env = append(env, "COLORTERM=truecolor") - } - - // TERM - if mode >= 3 { - env = append(env, "TERM=xterm-truecolor") - } else if mode == 2 { - env = append(env, "TERM=xterm-256color") - } else if mode == 1 { - env = append(env, "TERM=xterm-color") - } else { - env = append(env, "TERM=dumb") - } - - // CLICOLOR - if mode >= 1 { - env = append(env, "CLICOLOR=1") - } - - // TERM_PROGRAM - env = append(env, "TERM_PROGRAM=Koi") - - return env -}