示例#1
0
文件: storage.go 项目: ryo33/zenv
func ReadStorage(name, key, subkey string) []string {
	if len(name) == 0 {
		name = STORAGE
	}
	fi := util.ReadFile(getStorageFilePath(name, key, subkey))
	return util.Remove(fi, "")
}
示例#2
0
文件: settings.go 项目: ryo33/zenv
func Read(dir string) Items {
	items := make(map[string][][]string)
	// each settings
	for key, se := range settings {
		if pa := path.Join(dir, key); util.Exists(pa) {
			its := [][]string{}
			// each lines of file
			for line, str := range util.ReadFile(pa) {
				// if succeeded reading
				if it := se.read(str); len(it) != 0 {
					its = append(its, it)
				} else {
					util.PrintErrorMessage(util.FormatErrorAtLine(pa, line, "Can't parse"))
				}
			}
			if len(its) > 0 {
				items[key] = its
			}
		}
	}
	pa := path.Join(dir, RC)
	// each lines of file
	for line, str := range util.ReadFile(pa) {
		read := false
		if fir := strings.Index(str, " "); fir != -1 {
			key := str[:fir]
			str := str[fir+1:]
			// if setting exists
			if se, ok := settings[key]; ok {
				// if succeeded reading
				if it := se.read(str); len(it) != 0 {
					is, ok := items[key]
					if !ok {
						is = [][]string{}
					}
					items[key] = append(is, it)
					read = true
				}
			}
		}
		if !read {
			util.PrintErrorMessage(util.FormatErrorAtLine(pa, line, "Can't parse"))
		}
	}
	return Items(items)
}
示例#3
0
文件: global.go 项目: ryo33/zenv
func RemoveGlobalEnv(name string) {
	envFile := GetEnvsPath()
	tmp := util.ReadFile(envFile)
	result := []string{}
	for _, en := range tmp {
		if en != name {
			result = append(result, en)
		}
	}
	util.WriteFile(envFile, result)
}
示例#4
0
文件: global.go 项目: ryo33/zenv
func (env *Env) AddGlobalEnv(force bool) {
	envFile := GetEnvsPath()
	tmp := util.ReadFile(envFile)
	for _, en := range tmp {
		if en == env.name {
			if !force {
				util.PrintErrorMessage("already exists\n--force to overwrite") //TODO already exists  --force to overwrite
			}
			break
		}
	}
	util.WriteFile(envFile, append(tmp, env.name))
}
示例#5
0
文件: environment.go 项目: ryo33/zenv
func Clean(current string) {
	settings.Initialize(getZenvPath())
	tmpPath := storage.GetStoragePath(storage.TMP)
	for _, dir := range util.GetAllDir(tmpPath) {
		if current != dir {
			activated := util.ReadFile(path.Join(tmpPath, dir, ACTIVATED))
			for _, act := range activated {
				env := getLocalEnv(act)
				if env != nil {
					env.ReadSettings()
					env.items.Clean(settings.NewInfo(getZenvPath(), env.dir))
				}
			}
			util.RemoveDir(path.Join(tmpPath, dir))
		}
	}
}
示例#6
0
文件: temporal.go 项目: ryo33/zenv
func ClearTemporal() {
	now := time.Now()
	for _, dir := range util.GetAllDir(GetStoragePath(TMP)) {
		last := util.ReadFile(getStorageFilePath(TMP, dir, LAST))
		if len(last) != 1 {
			updateLast(dir)
			continue
		}
		ti, err := time.Parse(time.ANSIC, last[0])
		if err == nil {
			if int(now.Sub(ti).Hours())/24 > remove {
				util.RemoveDir(GetStorageDir(TMP, dir))
			}
		} else {
			updateLast(dir)
		}
	}
}
示例#7
0
文件: io.go 项目: ryo33/zenv
//Read info
func readInfo(pa string) *Env {
	data := util.ReadFile(path.Join(pa, INFO))
	var name, dir string
	var global, recursive, exclusive bool
	for _, d := range data {
		re := strings.Split(d, SEPARATOR)
		if len(re) == 2 {
			switch re[0] {
			case "name":
				name = re[1]
			case "dir":
				dir = re[1]
			case "global":
				global = false
				if re[1] == "true" {
					global = true
				}
			case "recursive":
				recursive = false
				if re[1] == "true" {
					recursive = true
				}
			case "exclusive":
				exclusive = false
				if re[1] == "true" {
					exclusive = true
				}
			}
		}
	}
	if len(name) == 0 || len(dir) == 0 {
		return nil
	}
	env := NewEnv(global, name, recursive, exclusive)
	//TODO read others
	return env
}
示例#8
0
文件: io.go 项目: ryo33/zenv
//Read all global envs
func readEnvs() []string {
	return util.ReadFile(path.Join(util.GetHomeDir(), zenv.ZENV, ENVS, ENVS))
}
示例#9
0
文件: global.go 项目: ryo33/zenv
func GetGlovalEnvs() []string {
	envFile := GetEnvsPath()
	return util.ReadFile(envFile)
}
示例#10
0
文件: global.go 项目: ryo33/zenv
func getEnvDirs() []string {
	return util.ReadFile(path.Join(util.GetHomeDir(), zenv.ZENV, DIRS))
}