本文整理汇总了Golang中github.com/spf13/cobra.Command.Run方法的典型用法代码示例。如果您正苦于以下问题:Golang Command.Run方法的具体用法?Golang Command.Run怎么用?Golang Command.Run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/spf13/cobra.Command
的用法示例。
在下文中一共展示了Command.Run方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: run
func run(cmd *cobra.Command, args []string) {
p, err := pkg.ReadCaddyJSON()
if err != nil {
log.Fatal(err)
return
}
if script, ok := p.Scripts[args[0]]; ok {
fmt.Printf("Running script \"%s\"\n%s\n", args[0], script)
fields := strings.Fields(script)
cmd := exec.Command(fields[0], fields[1:]...)
cmd.Env = append(os.Environ(), "GO15VENDOREXPERIMENT=1")
var buf bytes.Buffer
cmd.Stderr = &buf
cmd.Stdout = &buf
err := cmd.Run()
out := buf.String()
fmt.Println(out)
if err != nil {
fmt.Println(err)
}
return
}
}
示例2: HandleNodeCommand
func HandleNodeCommand(cmd *cobra.Command, args []string) {
var (
name string
err tree_lib.TreeError
)
err.From = tree_lib.FROM_HANDLE_NODE_COMMAND
name, err.Err = cmd.Flags().GetString("set-name")
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
}
// If we have set-name flag then we just setting current_node in database and exiting
if len(name) > 0 {
tree_db.Set(tree_db.DB_RANDOM, []byte("current_node"), []byte(name))
return
}
daemon := false
daemon, err.Err = cmd.Flags().GetBool("daemon")
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
if daemon {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("%s node > %s 2>&1 &", os.Args[0], tree_log.LogFile))
err.Err = cmd.Run()
if !err.IsNull() {
log.Fatal(err.Err)
}
return
}
name, err.Err = cmd.Flags().GetString("name")
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
if len(name) == 0 {
current_node_byte, err := tree_db.Get(tree_db.DB_RANDOM, []byte("current_node"))
if !err.IsNull() {
tree_log.Error(err.From, "Getting current node name from Random database, ", err.Error())
return
}
if len(current_node_byte) == 0 {
fmt.Println("Name is important for the first time run")
return
}
} else {
err = tree_node.SetCurrentNode(name)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
}
tree_node.Start()
}
示例3: build
func build(cmd *cobra.Command, args []string) {
p, err := pkg.ReadCaddyJSON()
if err != nil {
log.Fatal(err)
return
}
if build, ok := p.Scripts["build"]; ok {
fmt.Printf("Running script \"build\"\n%s", build)
fields := strings.Fields(build)
cmd := exec.Command(fields[0], fields[1:]...)
cmd.Env = append(os.Environ(), "GO15VENDOREXPERIMENT=1")
var buf bytes.Buffer
cmd.Stderr = &buf
cmd.Stdout = &buf
err := cmd.Run()
out := buf.String()
fmt.Println(out)
if err != nil {
fmt.Println(err)
}
} else {
if strings.Contains(runtime.Version(), "go1.5") {
// add GO15VENDOREXPERIMENT=1 env var so it uses local "vendor""
var goArgs []string
if output != "" {
goArgs = []string{"-o", output}
}
goArgs = append(goArgs, args...)
goArgs = append([]string{"build"}, goArgs...)
cmd := exec.Command("go", goArgs...)
cmd.Env = append(os.Environ(), "GO15VENDOREXPERIMENT=1")
var buf bytes.Buffer
cmd.Stderr = &buf
cmd.Stdout = &buf
err := cmd.Run()
out := buf.String()
fmt.Println(out)
if err != nil {
fmt.Println(err)
}
}
}
}
示例4: applyToCreate
// applyToCreate injects the deprecation notice about for 'apply' command into
// 'create' command.
// TODO: Remove this once we get rid of 'apply' in all documentation/etc.
func applyToCreate(dst *cobra.Command) *cobra.Command {
dst.Aliases = append(dst.Aliases, "apply")
oldRun := dst.Run
dst.Run = func(c *cobra.Command, args []string) {
calledApply := false
calledApply = calledApply || len(os.Args) >= 2 && os.Args[1] == "apply" // `oc apply`
calledApply = calledApply || len(os.Args) >= 3 && os.Args[2] == "apply" // `openshift cli apply`
if calledApply {
glog.Errorf("DEPRECATED: The 'apply' command is now deprecated, use 'create' instead.")
}
oldRun(c, args)
}
return dst
}
示例5: ToCommand
func (u usageTemplate) ToCommand(run cobraRunE) *cobra.Command {
c := cobra.Command{
Use: u.Use,
Short: u.Short,
Long: u.Long,
}
if run != nil {
// newer versions of cobra support a run function that returns an error,
// but in the meantime, this should help ease the transition
c.Run = func(cmd *cobra.Command, args []string) {
err := run(cmd, args)
if err != nil {
cmd.Usage()
fatalf(err.Error())
}
}
}
return &c
}