Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cobra Commander: How to call a command from another command?

Tags:

go

In cobra I've create a command of commands:

myapp zip -directory "xzy" -output="zipname"
myapp upload -filename="abc"

I want to make a zipAndUpload command and reuse the existing commands, i.e.,

myapp zipup -directory "xzy" -outout="zipname"

Here the zipup would first call the "zip" command and then use the output name from the "zip" command as the "filename" flag for the "upload" command.

How can I execute this without a lot of code duplication?

like image 402
user2644113 Avatar asked Sep 19 '25 19:09

user2644113


2 Answers

The "shared" switches make as global.

The "run" sections of the sub-commands, convert to functions.

To do this you must define the commands manually:

var (
    cfgDirectory  string
    cfgFilename  string
    cfgOutput string
)

var rootCmd = &cobra.Command{
    Use:   "root",
    Short: "root",
    Long:  "root",
    Run: func(cmd *cobra.Command, args []string) {
        // something
    },
}

var uploadCmd = &cobra.Command{
    Use:   'upload',
    Short: 'upload',
    Long:  `upload`,
    Run: func(cmd *cobra.Command, args []string) {
        Upload()
    },
}

var zipCmd = &cobra.Command{
    Use:   "zip",
    Short: "zip",
    Long:  "zip",
    Run: func(cmd *cobra.Command, args []string) {
        Zip()
    },
}

var zipupCmd = &cobra.Command{
    Use:   "zipup",
    Short: "zipup",
    Long:  "zipup",
    Run: func(cmd *cobra.Command, args []string) {
        Zip()
        Upload()
    },
}

func setFlags() {
    rootCmd.PersistentFlags().StringVar(&cfgDirectory, "directory", "", "explanation")
    rootCmd.PersistentFlags().StringVar(&cfgFilename, "filename", "", "explanation")
    rootCmd.PersistentFlags().StringVar(&cfgOutput, "output", "", "explanation")
}

func Upload() {
    // you know what to do
}

func Zip() {
    // you know what to do
}
...

// Add subcommands
rootCmd.AddCommand(zipCmd)
rootCmd.AddCommand(uploadCmd)
rootCmd.AddCommand(zipupCmd)

Hope this helps, this is the best I could do without any example code.

When you create new commands from the cobra CLI, it usually puts them into separate files. If you want to run a command from a different command file, it would look something like this:

package cmd

import "github.com/spf13/cobra"

// DirectoryFlag specifies the directory
var DirectoryFlag string

// OutputFlag specifies the output
var OutputFlag string

// zipupCmd represents the "zipup" command
var zipupCmd = &cobra.Command{
    Use:   "zipup",
    Short: "Zip & Upload",
    Long: `Zip & Upload`,
    Run: func(cmd *cobra.Command, args []string) {
        anyArgs := "whatever"
        zipCmd.Run(cmd, []string{anyArgs})
        uploadCmd.Run(cmd, []string{anyArgs})
    },
}

func init() {
    rootCmd.AddCommand(zipupCmd)
    zipupCmd.Flags().StringVarP(&DirectoryFlag, "directory", "d", "", "set the directory")
    zipupCmd.Flags().StringVarP(&OutputFlag, "output", "o", "", "set the output")
}
like image 32
Jim Fisk Avatar answered Sep 21 '25 09:09

Jim Fisk