1 Star 0 Fork 0

GiteeStudio / gitenv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
envctx.go 1.07 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
江二十三 提交于 2018-06-29 16:50 . support expandenv
package gitenv
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// Envcontext is
type Envcontext struct {
Env map[string]string
}
// NewEnvcontext todo
func NewEnvcontext() (*Envcontext, error) {
exe, err := os.Executable()
if err != nil {
return nil, err
}
ec := &Envcontext{Env: make(map[string]string)}
exedir := filepath.Dir(exe)
if filepath.Base(exedir) == "bin" {
ec.Env["APPDIR"] = filepath.Dir(exedir)
} else {
ec.Env["APPDIR"] = exedir
}
return ec, nil
}
// Append env
func (ec *Envcontext) Append(key, value string) error {
if len(key) == 0 || len(value) == 0 {
return errors.New("Empty key value")
}
ec.Env[key] = value
return nil
}
// Delete Env
func (ec *Envcontext) Delete(key string) error {
if _, ok := ec.Env[key]; ok {
delete(ec.Env, key)
return nil
}
return fmt.Errorf("'%s' not exists", key)
}
// Expand callback
func (ec *Envcontext) Expand(s string) string {
if _, ok := ec.Env[s]; ok {
return ec.Env[s]
}
return os.Getenv(s)
}
// Expandenv is
func (ec *Envcontext) Expandenv(s string) string {
return os.Expand(s, ec.Expand)
}
Go
1
https://gitee.com/oscstudio/gitenv.git
git@gitee.com:oscstudio/gitenv.git
oscstudio
gitenv
gitenv
master

搜索帮助

14c37bed 8189591 565d56ea 8189591