Golang Command.Run Examples

Golang Command.Run - 5 examples found. These are the top rated real world Golang examples of github.com/spf13/cobra.Command.Run extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
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
	}
}
Example #2
0
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()
}
Example #3
0
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)
			}
		}
	}
}
Example #4
0
// 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
}
Example #5
0
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
}