Skip to content

Commit

Permalink
refactor: add UseEnv() method, close #38
Browse files Browse the repository at this point in the history
And refactor `UseConfigEnv()` method
  • Loading branch information
ilharp committed Jun 1, 2022
1 parent c75a380 commit a2039d5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 172 deletions.
30 changes: 30 additions & 0 deletions packages/koi/config/use_config_env.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
42 changes: 0 additions & 42 deletions packages/koi/config/useenv.go

This file was deleted.

82 changes: 9 additions & 73 deletions packages/koi/daemon/node_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"koi/config"
"koi/util"
envUtil "koi/util/env"
l "koi/util/logger"
"koi/util/strutil"
"koi/util/supcolor"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)

Expand Down
34 changes: 34 additions & 0 deletions packages/koi/util/env/useenv.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}
36 changes: 36 additions & 0 deletions packages/koi/util/supcolor/use_color_env.go
Original file line number Diff line number Diff line change
@@ -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")
}
57 changes: 0 additions & 57 deletions packages/koi/util/supcolor/usecolor.go

This file was deleted.

0 comments on commit a2039d5

Please sign in to comment.