From 3fc31722dd8c885c90652140d86c6c5ecbb57b7d Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 17 Dec 2023 13:17:18 +0100 Subject: [PATCH] tmp --- cmd/carapace/cmd/root.go | 76 ++++++++++++-------------------------- cmd/carapace/cmd/run.go | 32 ++++++++++++++++ cmd/carapace/cmd/schema.go | 26 +++++++++++++ cmd/carapace/cmd/scrape.go | 20 ++++++++++ cmd/carapace/cmd/style.go | 24 ++++++++++++ 5 files changed, 126 insertions(+), 52 deletions(-) create mode 100644 cmd/carapace/cmd/run.go create mode 100644 cmd/carapace/cmd/schema.go create mode 100644 cmd/carapace/cmd/scrape.go create mode 100644 cmd/carapace/cmd/style.go diff --git a/cmd/carapace/cmd/root.go b/cmd/carapace/cmd/root.go index 2b43d79b5c..4fd5c908a9 100644 --- a/cmd/carapace/cmd/root.go +++ b/cmd/carapace/cmd/root.go @@ -61,62 +61,33 @@ var rootCmd = &cobra.Command{ Config is written to [%v/carapace]. Specs are loaded from [%v/carapace/specs]. `, suppressErr(xdg.UserCacheDir), suppressErr(xdg.UserConfigDir), suppressErr(xdg.UserConfigDir)), - Args: cobra.ArbitraryArgs, // TODO + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - switch { - case cmd.Flag("list").Changed: - listCmd.SetArgs(args) - listCmd.Execute() - return - case cmd.Flag("macros").Changed: - macrosCmd.SetArgs(args) - macrosCmd.Execute() - return - } - // since flag parsing is disabled do this manually // TODO switch to cobra flag parsing with interspersed=false (redirect completion from completers/carapace_completer to here) - switch os.Args[0] { // TODO use flags + switch args[0] { // TODO use flags case "--macros": - if len(args) > 1 { - printMacro(args[1]) - } else { - printMacros() - } + macrosCmd.SetArgs(args[1:]) + macrosCmd.Execute() case "-h", "--help": cmd.Help() case "-v", "--version": println(cmd.Version) - // case "--list": - // printCompleters() - // case "--list=json": - // printCompletersJson() + case "--list": + listCmd.SetArgs(args[1:]) + listCmd.Execute() case "--run": - _, spec, err := loadSpec(args[1]) - if err != nil { - fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) - os.Exit(1) - } - - cmd := spec.ToCobra() - cmd.SetArgs(args[2:]) - cmd.Execute() // TODO handle error? + runCmd.SetArgs(args[1:]) + runCmd.Execute() case "--schema": - if schema, err := spec.Schema(); err != nil { - fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) // TODO fail / exit 1 ? - } else { - fmt.Fprintln(cmd.OutOrStdout(), schema) - } + schemaCmd.SetArgs(args[1:]) + schemaCmd.Execute() case "--scrape": - if len(args) > 1 { - scrape(args[1]) - } + scrapeCmd.SetArgs(args[1:]) + scrapeCmd.Execute() case "--style": - if len(args) > 1 { - if err := setStyle(args[1]); err != nil { - fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) - } - } + styleCmd.SetArgs(args[1:]) + styleCmd.Execute() default: if overlayPath, err := overlayPath(args[0]); err == nil && len(args) > 2 { // and arg[1] is a known shell cmd := &cobra.Command{ @@ -177,6 +148,7 @@ var rootCmd = &cobra.Command{ } }, + DisableFlagParsing: true, FParseErrWhitelist: cobra.FParseErrWhitelist{ UnknownFlags: true, }, @@ -444,10 +416,10 @@ func init() { rootCmd.Flags().BoolP("help", "h", false, "help for carapace") rootCmd.Flags().Bool("list", false, "list completers") rootCmd.Flags().Bool("macros", false, "list spec macros") - rootCmd.Flags().String("run", "", "run spec") + rootCmd.Flags().Bool("run", false, "run spec") rootCmd.Flags().Bool("schema", false, "json schema for spec files") - rootCmd.Flags().String("scrape", "", "scrape spec to go code") - rootCmd.Flags().String("style", "", "set style") + rootCmd.Flags().Bool("scrape", false, "scrape spec to go code") + rootCmd.Flags().Bool("style", false, "set style") rootCmd.Flags().BoolP("version", "v", false, "version for carapace") rootCmd.MarkFlagsMutuallyExclusive( @@ -468,11 +440,11 @@ func init() { carapace.Gen(rootCmd).Standalone() spec.Register(rootCmd) - carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ - "run": carapace.ActionFiles(".yaml"), - "scrape": carapace.ActionFiles(".yaml"), - "style": carapace.ActionStyleConfig().NoSpace(), - }) + // carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ + // "run": carapace.ActionFiles(".yaml"), + // "scrape": carapace.ActionFiles(".yaml"), + // "style": carapace.ActionStyleConfig().NoSpace(), + // }) carapace.Gen(rootCmd).PositionalCompletion( action.ActionCompleters(), diff --git a/cmd/carapace/cmd/run.go b/cmd/carapace/cmd/run.go new file mode 100644 index 0000000000..4fb24fe1ae --- /dev/null +++ b/cmd/carapace/cmd/run.go @@ -0,0 +1,32 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var runCmd = &cobra.Command{ + Use: "run", + Short: "", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + _, spec, err := loadSpec(args[0]) + if err != nil { + fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) + os.Exit(1) + } + + specCmd := spec.ToCobra() + + specCmd.SetArgs(args[2:]) + specCmd.Execute() // TODO handle error? + }, +} + +func init() { + carapace.Gen(runCmd).Standalone() + +} diff --git a/cmd/carapace/cmd/schema.go b/cmd/carapace/cmd/schema.go new file mode 100644 index 0000000000..0de4e6607f --- /dev/null +++ b/cmd/carapace/cmd/schema.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "fmt" + + "github.com/rsteube/carapace" + spec "github.com/rsteube/carapace-spec" + "github.com/spf13/cobra" +) + +var schemaCmd = &cobra.Command{ + Use: "schema", + Short: "", + Run: func(cmd *cobra.Command, args []string) { + if schema, err := spec.Schema(); err != nil { + fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) // TODO fail / exit 1 ? + } else { + fmt.Fprintln(cmd.OutOrStdout(), schema) + } + }, +} + +func init() { + carapace.Gen(schemaCmd).Standalone() + +} diff --git a/cmd/carapace/cmd/scrape.go b/cmd/carapace/cmd/scrape.go new file mode 100644 index 0000000000..3e38718c61 --- /dev/null +++ b/cmd/carapace/cmd/scrape.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var scrapeCmd = &cobra.Command{ + Use: "scrape", + Short: "", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + scrape(args[0]) + }, +} + +func init() { + carapace.Gen(scrapeCmd).Standalone() + +} diff --git a/cmd/carapace/cmd/style.go b/cmd/carapace/cmd/style.go new file mode 100644 index 0000000000..db979f91ec --- /dev/null +++ b/cmd/carapace/cmd/style.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "fmt" + + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var styleCmd = &cobra.Command{ + Use: "style", + Short: "", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + if err := setStyle(args[1]); err != nil { + fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) + } + }, +} + +func init() { + carapace.Gen(styleCmd).Standalone() + +}