Beispiel #1
0
func (items Items) AddItems(lable string, force bool, its2 [][]string) {
	its, ok := items.ToMap()[lable]
	se, ok2 := settings[lable]
	if !ok {
		its = [][]string{}
	}
	if !ok2 {
		util.PrintErrorMessageContinue(lable + " lable is undefined")
	} else {
		for idx, it := range its {
			tmp := its2
			for idx2, it2 := range tmp {
				if se.equal(it, it2) {
					if !force {
						util.PrintErrorMessage(strings.Join(it2, " ") + " already exists\n--force to overwrite")
					} else {
						items.ToMap()[lable][idx] = it2
					}
					its2 = append(its2[:idx2], its2[idx2+1:]...)
				}
			}
		}
		items.ToMap()[lable] = append(its, its2...)
	}
}
Beispiel #2
0
// it is almost same as getLocalEnv but never return nil
func GetLocalEnv(name string) *Env {
	env := getLocalEnv(name)
	if env == nil {
		util.PrintErrorMessage("not exists")
	}
	return env
}
Beispiel #3
0
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)
}
Beispiel #4
0
func doLocal(c *cli.Context) {
	pwd := util.GetCurrentPath()
	if environment.ExistsLocalEnv(pwd) && !c.Bool("force") {
		util.PrintErrorMessage(fmt.Sprintf("%s already exists\n--force flag to force to initialize", environment.ZENV_LOCAL))
	} else {
		env := environment.NewEnv(false, pwd, !c.Bool("not-recursive"), c.Bool("exclusive"))
		env.Write()
	}
}
Beispiel #5
0
func DoZenv(c *cli.Context) {
	args := c.Args()
	if len(args) == 1 {
		for _, env := range environment.GetActivated(args[0]) {
			util.Print(env)
		}
	} else {
		util.PrintErrorMessage("needs 1 arg")
	}
}
Beispiel #6
0
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))
}
Beispiel #7
0
func getGlobalEnv(name string) *Env {
	if !ExistsGlobalEnv(name) {
		util.PrintErrorMessage("not exists global env")
	}
	return read(getGlobalPath(name))
}