Rishabh Raghwendra
Rishabh Raghwendra

Reputation: 320

How to run another command from Cobra

I am building a CLI app in GoLang. I am using Cobra for doing that, and I have the following code for that:

/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
    "fmt"

    "github.com/spf13/cobra"
)

// pullCmd represents the pull command
var pullCmd = &cobra.Command{
    Use:   "pull",
    Short: "Take pull from repo",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("pull called")
    },
}

func init() {
    rootCmd.AddCommand(pullCmd)

    // Here you will define your flags and configuration settings.

    // Cobra supports Persistent Flags which will work for this command
    // and all subcommands, e.g.:
    // pullCmd.PersistentFlags().String("foo", "", "A help for foo")

    // Cobra supports local flags which will only run when this command
    // is called directly, e.g.:
    // pullCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

I want the pull command to run git pull command internally whenever I run my pull command. How can I do that?

Upvotes: 1

Views: 1653

Answers (1)

Roozbeh Sayadi
Roozbeh Sayadi

Reputation: 346

The os/exec package can help you out here. Create a command for git pull, and then Run it.

Upvotes: 0

Related Questions