// CreateRootCmd creates the root command
CreateRootCmd creates the root command
// CreateRootCmd creates the root command
func CreateRootCmd(driver *summon.Driver, args []string, options summon.MainOptions) *cobra.Command {
	cmdName := filepath.Base(args[0])
	var showVersion bool
	main := &mainCmd{
		driver: driver,
	}
	rootCmd := &cobra.Command{
		Use:   cmdName + " [file to summon]",
		Short: cmdName + " main command",
		Args: func(cmd *cobra.Command, args []string) error {
			if main.copyAll || showVersion || main.listOptions.asOption {
				return nil
			}
			if len(args) < 1 {
				return fmt.Errorf("requires one file to summon, received %d", len(args))
			}
			if main.json != "" && main.jsonFile != "" {
				return fmt.Errorf("--json and --json-file are mutually exclusive")
			}
			return nil
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			main.out = cmd.OutOrStdout()
			if showVersion {
				v, ok := makeVersion()
				if !ok {
					return fmt.Errorf("Missing build info")
				}
				enc := json.NewEncoder(main.out)
				enc.SetIndent("", "  ")
				enc.Encode(v)
				return nil
			}
			if !main.copyAll && !main.listOptions.asOption {
				filename := args[0]
				main.filename = filename
			}
			if main.jsonFile != "" {
				var j []byte
				var err error
				if main.jsonFile == "-" {
					j, err = ioutil.ReadAll(cmd.InOrStdin())
				} else {
					j, err = ioutil.ReadFile(main.jsonFile)
				}
				if err != nil {
					return err
				}
				main.json = string(j)
			}
			return main.run()
		},
	}
	rootCmd.PersistentFlags().StringVar(&main.json, "json", "", "json to use to render template")
	rootCmd.PersistentFlags().StringVar(&main.jsonFile, "json-file", "", "json file to use to render template, with '-' for stdin")
	rootCmd.PersistentFlags().BoolVarP(&main.debug, "debug", "d", false, "print debug info on stderr")
	rootCmd.Flags().BoolVarP(&main.copyAll, "all", "a", false, "restitute all data")
	rootCmd.Flags().BoolVar(&main.raw, "raw", false, "output without any template rendering")
	rootCmd.Flags().StringVarP(&main.dest, "out", "o", driver.Config.OutputDir, "destination directory, or '-' for stdout")
	rootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "output data version info and exit")
	main.listOptions = newListCmd(options.WithoutRunSubcmd, rootCmd, driver)
	newRunCmd(options.WithoutRunSubcmd, rootCmd, driver, main)
	rootCmd.AddCommand(newCompletionCmd(driver))
	return rootCmd
}
func CreateRootCmd ( driver * summon . Driver , args [ ] string , options summon . MainOptions ) * cobra . Command { cmdName := filepath . Base ( args [ 0 ] ) var showVersion bool main := & mainCmd { driver : driver , } rootCmd := & cobra . Command { Use : cmdName + " " , Short : cmdName + " " , Args : func ( cmd * cobra . Command , args [ ] string ) error { if main . copyAll || showVersion || main . listOptions . asOption { return nil } if len ( args ) < 1 { return fmt . Errorf ( " " , len ( args ) ) } if main . json != " " && main . jsonFile != " " { return fmt . Errorf ( " " ) } return nil } , RunE : func ( cmd * cobra . Command , args [ ] string ) error { main . out = cmd . OutOrStdout ( ) if showVersion { v , ok := makeVersion ( ) if ! ok { return fmt . Errorf ( " " ) } enc := json . NewEncoder ( main . out ) enc . SetIndent ( " " , " " ) enc . Encode ( v ) return nil } if ! main . copyAll && ! main . listOptions . asOption { filename := args [ 0 ] main . filename = filename } if main . jsonFile != " " { var j [ ] byte var err error if main . jsonFile == " " { j , err = ioutil . ReadAll ( cmd . InOrStdin ( ) ) } else { j , err = ioutil . ReadFile ( main . jsonFile ) } if err != nil { return err } main . json = string ( j ) } return main . run ( ) } , } rootCmd . PersistentFlags ( ) . StringVar ( & main . json , " " , " " , " " ) rootCmd . PersistentFlags ( ) . StringVar ( & main . jsonFile , " " , " " , " " ) rootCmd . PersistentFlags ( ) . BoolVarP ( & main . debug , " " , " " , false , " " ) rootCmd . Flags ( ) . BoolVarP ( & main . copyAll , " " , " " , false , " " ) rootCmd . Flags ( ) . BoolVar ( & main . raw , " " , false , " " ) rootCmd . Flags ( ) . StringVarP ( & main . dest , " " , " " , driver . Config . OutputDir , " " ) rootCmd . Flags ( ) . BoolVarP ( & showVersion , " " , " " , false , " " ) main . listOptions = newListCmd ( options . WithoutRunSubcmd , rootCmd , driver ) newRunCmd ( options . WithoutRunSubcmd , rootCmd , driver , main ) rootCmd . AddCommand ( newCompletionCmd ( driver ) ) return rootCmd }
CreateRootCmd creates the root command
CreateRootCmd creates the root command
// add ls cmd or --ls flag // add run cmd, or root subcommands // add completion