From 5586f808a5db8b6a6476fd03ee8ed95862bca824 Mon Sep 17 00:00:00 2001 From: Vicente Zepeda Mas Date: Fri, 20 Oct 2017 11:02:59 -0600 Subject: [PATCH] Prepares args to use in command --- cmd/insert.go | 14 ++------------ cmd/root.go | 8 ++++---- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/cmd/insert.go b/cmd/insert.go index 371369d..d7784d0 100644 --- a/cmd/insert.go +++ b/cmd/insert.go @@ -35,23 +35,13 @@ this command will, when executed correctly, insert the string in the file 'testf $ go-sif insert -f testfile string to insert in the file this command will, when executed correctly, insert the string at the end of file 'testfile' in line a new line`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { // TODO: Work your own magic here fmt.Println("insert called") + return nil }, } func init() { RootCmd.AddCommand(insertCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // insertCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // insertCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") - } diff --git a/cmd/root.go b/cmd/root.go index 598aace..b2e8e82 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,9 +23,8 @@ import ( ) var ( - cfgFile string - file string - line int + cfgFile, file, text string + line int ) // RootCmd represents the base command when called without any subcommands @@ -51,7 +50,8 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) RootCmd.PersistentFlags().StringVarP(&file, "file", "f", "", "The file where you want to insert the string") - RootCmd.PersistentFlags().IntVarP(&line, "line", "l", 0, "Line where the string is going to be inserted, if omited the string will be inserted in a new line at the end of the file") + RootCmd.PersistentFlags().IntVarP(&line, "line", "l", -1, "Line where the string is going to be inserted, if omited the string will be inserted \n in a new line at the end of the file. Count starts at 1.") + RootCmd.PersistentFlags().StringVarP(&text, "text", "t", "", "The to insert into file") } // initConfig reads in config file and ENV variables if set.