-
Notifications
You must be signed in to change notification settings - Fork 8
feat: add adder to generate cmd stubs from markdown #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
789d638
bcd0a57
e99dfb7
f7c7d4c
703f542
408fe8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Adder configuration file | ||
| # See https://github.com/jrschumacher/adder for documentation | ||
|
|
||
| binary_name: otdfctl | ||
| input: docs/man | ||
| output: cmd/generated | ||
| package: generated | ||
| generated_file_suffix: _generated.go | ||
| index_format: _index | ||
| package_strategy: directory |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,39 +3,42 @@ package cmd | |
| import ( | ||
| "fmt" | ||
|
|
||
| configgenerated "github.com/opentdf/otdfctl/cmd/generated/config" | ||
| "github.com/opentdf/otdfctl/pkg/cli" | ||
| "github.com/opentdf/otdfctl/pkg/config" | ||
| "github.com/opentdf/otdfctl/pkg/man" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func config_updateOutput(cmd *cobra.Command, args []string) { | ||
| c := cli.New(cmd, args) | ||
| // handleConfigOutput implements the business logic for the config output command | ||
| func handleConfigOutput(cmd *cobra.Command, req *configgenerated.OutputRequest) error { | ||
| c := cli.New(cmd, []string{}) | ||
| h := NewHandler(c) | ||
| defer h.Close() | ||
|
|
||
| format := c.Flags.GetRequiredString("format") | ||
| format := req.Flags.Format | ||
|
|
||
| err := config.UpdateOutputFormat(cfgKey, format) | ||
| if err != nil { | ||
| c.ExitWithError("Failed to update output format", err) | ||
| } | ||
|
Comment on lines
21
to
23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The handler function Instead of calling return fmt.Errorf("Failed to update output format: %w", err) |
||
|
|
||
| c.Println(cli.SuccessMessage(fmt.Sprintf("Output format updated to %s", format))) | ||
| return nil | ||
| } | ||
|
|
||
| // handleConfig implements the parent config command (shows help if called without subcommands) | ||
| func handleConfig(cmd *cobra.Command, req *configgenerated.ConfigRequest) error { | ||
| return cmd.Help() | ||
| } | ||
|
|
||
| func init() { | ||
| outputCmd := man.Docs.GetCommand("config/output", | ||
| man.WithRun(config_updateOutput), | ||
| ) | ||
| outputCmd.Flags().String( | ||
| outputCmd.GetDocFlag("format").Name, | ||
| outputCmd.GetDocFlag("format").Default, | ||
| outputCmd.GetDocFlag("format").Description, | ||
| ) | ||
|
|
||
| cmd := man.Docs.GetCommand("config", | ||
| man.WithSubcommands(outputCmd), | ||
| ) | ||
| RootCmd.AddCommand(&cmd.Command) | ||
| // Create commands using generated constructors with handler functions | ||
| configCmd := configgenerated.NewConfigCommand(handleConfig) | ||
| outputCmd := configgenerated.NewOutputCommand(handleConfigOutput) | ||
|
|
||
| // Add subcommand to parent | ||
| configCmd.AddCommand(outputCmd) | ||
|
|
||
| // Add to root command | ||
| RootCmd.AddCommand(configCmd) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,21 @@ package cmd | |
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| selectorsgenerated "github.com/opentdf/otdfctl/cmd/generated/dev/selectors" | ||
| "github.com/opentdf/otdfctl/pkg/cli" | ||
| "github.com/opentdf/otdfctl/pkg/handlers" | ||
| "github.com/opentdf/otdfctl/pkg/man" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var selectors []string | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| func dev_selectorsGen(cmd *cobra.Command, args []string) { | ||
| c := cli.New(cmd, args) | ||
| h := NewHandler(c) | ||
| defer h.Close() | ||
| // handleDevSelectorsGenerate implements the business logic for the generate command | ||
| func handleDevSelectorsGenerate(cmd *cobra.Command, req *selectorsgenerated.GenerateRequest) error { | ||
| c := cli.New(cmd, []string{}) | ||
| handler := NewHandler(c) | ||
| defer handler.Close() | ||
|
|
||
| subject := c.Flags.GetRequiredString("subject") | ||
|
|
||
|
|
@@ -30,15 +32,26 @@ func dev_selectorsGen(cmd *cobra.Command, args []string) { | |
|
|
||
| t := cli.NewTabular(rows...) | ||
| cli.PrintSuccessTable(cmd, "", t) | ||
| return nil | ||
| } | ||
|
|
||
| func dev_selectorsTest(cmd *cobra.Command, args []string) { | ||
| c := cli.New(cmd, args) | ||
| h := NewHandler(c) | ||
| defer h.Close() | ||
| // handleDevSelectorsTest implements the business logic for the test command | ||
| func handleDevSelectorsTest(cmd *cobra.Command, req *selectorsgenerated.TestRequest) error { | ||
| c := cli.New(cmd, []string{}) | ||
| handler := NewHandler(c) | ||
| defer handler.Close() | ||
|
|
||
| subject := c.Flags.GetRequiredString("subject") | ||
| selectors = c.Flags.GetStringSlice("selector", selectors, cli.FlagsStringSliceOptions{Min: 1}) | ||
|
|
||
| // Convert single selector string to slice for compatibility with existing logic | ||
| var selectorsList []string | ||
| if req.Flags.Selector != "" { | ||
| selectorsList = strings.Split(req.Flags.Selector, ",") | ||
| } | ||
|
|
||
| if len(selectorsList) == 0 { | ||
| cli.ExitWithError("Must provide at least one selector", nil) | ||
| } | ||
|
Comment on lines
+52
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| flattened, err := handlers.FlattenSubjectContext(subject) | ||
| if err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -47,7 +60,8 @@ func dev_selectorsTest(cmd *cobra.Command, args []string) { | |
|
|
||
| rows := [][]string{} | ||
| for _, item := range flattened { | ||
| for _, selector := range selectors { | ||
| for _, selector := range selectorsList { | ||
| selector = strings.TrimSpace(selector) | ||
| if selector == item.Key { | ||
| rows = append(rows, []string{item.Key, fmt.Sprintf("%v", item.Value)}) | ||
| } | ||
|
|
@@ -56,40 +70,31 @@ func dev_selectorsTest(cmd *cobra.Command, args []string) { | |
|
|
||
| t := cli.NewTabular(rows...) | ||
| cli.PrintSuccessTable(cmd, "", t) | ||
| return nil | ||
| } | ||
|
|
||
| func init() { | ||
| genCmd := man.Docs.GetCommand("dev/selectors/generate", | ||
| man.WithRun(dev_selectorsGen), | ||
| ) | ||
| genCmd.Flags().StringP( | ||
| genCmd.GetDocFlag("subject").Name, | ||
| genCmd.GetDocFlag("subject").Shorthand, | ||
| genCmd.GetDocFlag("subject").Default, | ||
| genCmd.GetDocFlag("subject").Description, | ||
| ) | ||
|
|
||
| testCmd := man.Docs.GetCommand("dev/selectors/test", | ||
| man.WithRun(dev_selectorsTest), | ||
| ) | ||
| testCmd.Flags().StringP( | ||
| testCmd.GetDocFlag("subject").Name, | ||
| testCmd.GetDocFlag("subject").Shorthand, | ||
| testCmd.GetDocFlag("subject").Default, | ||
| testCmd.GetDocFlag("subject").Description, | ||
| ) | ||
| testCmd.Flags().StringSliceVarP( | ||
| &selectors, | ||
| testCmd.GetDocFlag("selector").Name, | ||
| testCmd.GetDocFlag("selector").Shorthand, | ||
| []string{}, | ||
| testCmd.GetDocFlag("selector").Description, | ||
| ) | ||
|
|
||
| doc := man.Docs.GetCommand("dev/selectors", | ||
| man.WithSubcommands(genCmd, testCmd), | ||
| ) | ||
|
|
||
| dev_selectorsCmd := &doc.Command | ||
| devCmd.AddCommand(dev_selectorsCmd) | ||
| // Create commands using generated code with handler functions | ||
| genCmd := selectorsgenerated.NewGenerateCommand(handleDevSelectorsGenerate) | ||
| testCmd := selectorsgenerated.NewTestCommand(handleDevSelectorsTest) | ||
|
|
||
| // Create selectors parent command | ||
| selectorsCmd := &cobra.Command{ | ||
| Use: "selectors", | ||
| Aliases: []string{"sel"}, | ||
| Short: "Selectors", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Help() | ||
| }, | ||
| } | ||
|
|
||
| // Add subcommands | ||
| selectorsCmd.AddCommand(genCmd) | ||
| selectorsCmd.AddCommand(testCmd) | ||
|
|
||
| // Export for use by dev.go | ||
| DevSelectorsCmd = selectorsCmd | ||
| } | ||
|
|
||
| // DevSelectorsCmd exports the selectors command for use by dev.go | ||
| var DevSelectorsCmd *cobra.Command | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The suggestion to install
batsusingbrewis specific to macOS. To make this Makefile more portable for developers on other operating systems (like Linux), it would be better to provide a more generic installation instruction or link to the officialbats-coreinstallation guide.For example, you could suggest installing via
npmor from source, and link tohttps://github.com/bats-core/bats-core#installation.