Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 17, 2023
1 parent 67527b2 commit 3fc3172
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 52 deletions.
76 changes: 24 additions & 52 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -177,6 +148,7 @@ var rootCmd = &cobra.Command{
}

},
DisableFlagParsing: true,
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
Expand Down Expand Up @@ -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(
Expand All @@ -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(),
Expand Down
32 changes: 32 additions & 0 deletions cmd/carapace/cmd/run.go
Original file line number Diff line number Diff line change
@@ -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()

}
26 changes: 26 additions & 0 deletions cmd/carapace/cmd/schema.go
Original file line number Diff line number Diff line change
@@ -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()

}
20 changes: 20 additions & 0 deletions cmd/carapace/cmd/scrape.go
Original file line number Diff line number Diff line change
@@ -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()

}
24 changes: 24 additions & 0 deletions cmd/carapace/cmd/style.go
Original file line number Diff line number Diff line change
@@ -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()

}

0 comments on commit 3fc3172

Please sign in to comment.