当前位置: 首页>>代码示例>>Golang>>正文


Golang variables.Scope类代码示例

本文整理汇总了Golang中github.com/danwakefield/gosh/variables.Scope的典型用法代码示例。如果您正苦于以下问题:Golang Scope类的具体用法?Golang Scope怎么用?Golang Scope使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Scope类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: CdCmd

func CdCmd(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
	// This does not conform to the posix spec.
	// Very simplified. Cd to first arg or attempt to cd to home dir
	cdTarget := "."

	if len(args) == 0 {
		homeDir := scp.Get("HOME")
		if homeDir.Val == "" {
			// Implementation defined behaviour. We try to grab
			// the homedir of the current user
			u, err := user.Current()
			if err == nil {
				cdTarget = u.HomeDir
			}
		} else {
			cdTarget = homeDir.Val
		}
	} else {
		cdTarget = args[0]
	}

	if err := scp.SetPwd(cdTarget); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		return T.ExitFailure
	}
	return T.ExitSuccess
}
开发者ID:danwakefield,项目名称:gosh,代码行数:27,代码来源:cd.go

示例2: Sub

func (s SubVariable) Sub(scp *variables.Scope) (returnString string) {
	logex.Debug("Substituting variable")
	defer func() {
		logex.Debugf("Returned '%s'", returnString)
	}()
	v := scp.Get(s.VarName)

	switch s.SubType {
	case VarSubNormal:
		return v.Val
	case VarSubLength:
		// For the values ${#*} and ${#@}
		// the number of positional parameters is returned
		// We need to perform IFS splitting to figure this out
		return strconv.Itoa(len(v.Val))
	}

	varExists := v.Set == true
	// CheckNull means that an empty string is treated as unset
	if s.CheckNull {
		varExists = varExists && v.Val != ""
	}

	switch s.SubType {
	case VarSubAssign:
		if varExists {
			return v.Val
		}
		scp.Set(s.VarName, s.SubVal)
		return s.SubVal
	case VarSubMinus:
		if varExists {
			return v.Val
		}
		return s.SubVal
	case VarSubPlus:
		if varExists {
			return ""
		}
		return s.SubVal
	case VarSubQuestion:
		if varExists {
			return v.Val
		}
		if s.SubVal != "" {
			ExitShellWithMessage(T.ExitFailure, s.SubVal)
		}
		ExitShellWithMessage(T.ExitFailure, s.VarName+": Parameter not set")
	case VarSubTrimRight, VarSubTrimRightMax, VarSubTrimLeft, VarSubTrimLeftMax:
		ExitShellWithMessage(T.ExitFailure, "Trim operations not implemented")
	}

	logex.Fatal("SubVariable.Sub unreached")
	return ""
}
开发者ID:danwakefield,项目名称:gosh,代码行数:55,代码来源:substitutions.go

示例3: Eval

func (n NodeFor) Eval(scp *variables.Scope, ioc *T.IOContainer) T.ExitStatus {
	returnExit := T.ExitSuccess

	expandedArgs := make([]string, len(n.Args))
	for i, arg := range n.Args {
		// This will need to be changed when IFS splitting is coded.
		// Append each split as a seperate item
		expandedArgs[i] = arg.Expand(scp)
	}

	for _, arg := range expandedArgs {
		scp.Set(n.LoopVar, arg)
		returnExit = n.Body.Eval(scp, ioc)
	}

	return returnExit
}
开发者ID:danwakefield,项目名称:gosh,代码行数:17,代码来源:nodes.go

示例4: execExternal

func (n NodeCommand) execExternal(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
	// This is needed so that pipes will terminate
	if pw, isPipeWriter := ioc.Out.(*io.PipeWriter); isPipeWriter {
		defer func() {
			if err := pw.Close(); err != nil {
				panic(err) // XXX: Print error to stdout and continue?
			}
		}()
	}

	cmd := exec.Command(args[0], args[1:]...)
	cmd.Env = scp.Environ()
	cmd.Stdin = ioc.In
	cmd.Stderr = ioc.Err
	cmd.Stdout = ioc.Out

	err := cmd.Run()
	if err == nil {
		return T.ExitSuccess
	}
	return T.ExitFailure
}
开发者ID:danwakefield,项目名称:gosh,代码行数:22,代码来源:nodes.go

示例5: LocalCmd

func LocalCmd(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
	// Local should also do assignments, split args on equal sign? already
	// expanded
	for _, a := range args {
		tmp := scp.Get(a)
		if tmp.Set {
			scp.Set(a, tmp.Val, variables.LocalScope)
		} else {
			scp.Set(a, "", variables.LocalScope)
		}
	}
	return T.ExitSuccess
}
开发者ID:danwakefield,项目名称:gosh,代码行数:13,代码来源:local.go

示例6: EvalFunc

func (n NodeFunction) EvalFunc(scp *variables.Scope, ioc *T.IOContainer, args []string) T.ExitStatus {
	scp.PushFunction(args)
	defer scp.Pop()
	return n.Body.Eval(scp, ioc)
}
开发者ID:danwakefield,项目名称:gosh,代码行数:5,代码来源:nodes.go


注:本文中的github.com/danwakefield/gosh/variables.Scope类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。