-
Notifications
You must be signed in to change notification settings - Fork 13
feat(template): add search, get --raw, and help --all #170
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eae9220
feat(template): add --raw flag to output YAML spec
yuaanlin efd2b7a
fix(template): fetch raw YAML from URL instead of GraphQL
yuaanlin 248de2c
feat: add template search, help --all, and CLAUDE.md
yuaanlin e7c6218
fix(template): add timeout and URL escape for raw YAML fetch
yuaanlin 91579e6
fix: address review comments
yuaanlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Zeabur CLI - Development Notes | ||
|
|
||
| ## Build & Test | ||
| - Build: `go build ./...` | ||
| - Run: `go run ./cmd/main.go <command>` | ||
| - Test: `go test ./...` | ||
|
|
||
| ## Project Structure | ||
| - `cmd/main.go` — entry point | ||
| - `internal/cmd/<command>/` — each CLI command in its own package | ||
| - `internal/cmdutil/` — shared command utilities (Factory, auth checks, spinner config) | ||
| - `pkg/api/` — GraphQL API client | ||
| - `pkg/model/` — data models (GraphQL struct tags) | ||
| - `internal/cmd/root/root.go` — root command, registers all subcommands | ||
|
|
||
| ## Important: Keep `help --all` in sync | ||
| When adding or modifying CLI commands, flags, or subcommands, the output of `zeabur help --all` automatically reflects changes (it walks the Cobra command tree at runtime). No manual update is needed for the help output itself. | ||
|
|
||
| However, when adding a **new subcommand**, you must: | ||
| 1. Create the command package under `internal/cmd/<parent>/<new>/` | ||
| 2. Register it in the parent command file (e.g., `internal/cmd/template/template.go`) | ||
|
|
||
| ## Conventions | ||
| - Each subcommand lives in its own package: `internal/cmd/<parent>/<sub>/<sub>.go` | ||
| - Commands support both interactive and non-interactive modes; if a flag is provided, skip the interactive prompt | ||
| - Use `cmdutil.SpinnerCharSet`, `cmdutil.SpinnerInterval`, `cmdutil.SpinnerColor` for spinners | ||
| - Models in `pkg/model/` use `graphql:"fieldName"` struct tags — only add fields that exist in the backend GraphQL schema | ||
| - Backend GraphQL schema lives in `../backend/internal/gateway/graphql/` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package help | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| func NewCmdHelp(rootCmd *cobra.Command) *cobra.Command { | ||
| var all bool | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "help [command]", | ||
| Short: "Help about any command", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if all { | ||
| printAllCommands(rootCmd, "") | ||
| return nil | ||
| } | ||
|
|
||
| // default: find the target command and show its help | ||
| target, _, err := rootCmd.Find(args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return target.Help() | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&all, "all", false, "Show all commands with their flags") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func printAllCommands(cmd *cobra.Command, prefix string) { | ||
| fullName := prefix + cmd.Name() | ||
|
|
||
| if cmd.Runnable() || len(cmd.Commands()) == 0 { | ||
| fmt.Printf("%s - %s\n", fullName, cmd.Short) | ||
| printFlags(cmd, fullName) | ||
| } | ||
|
|
||
| for _, child := range cmd.Commands() { | ||
| if child.Hidden || child.Name() == "help" { | ||
| continue | ||
| } | ||
| printAllCommands(child, fullName+" ") | ||
| } | ||
| } | ||
|
|
||
| func printFlags(cmd *cobra.Command, fullName string) { | ||
| var flags []string | ||
|
|
||
| cmd.LocalFlags().VisitAll(func(f *pflag.Flag) { | ||
| if f.Hidden { | ||
| return | ||
| } | ||
| entry := " --" + f.Name | ||
| if f.Shorthand != "" { | ||
| entry = " -" + f.Shorthand + ", --" + f.Name | ||
| } | ||
| if f.DefValue != "" && f.DefValue != "false" { | ||
| entry += fmt.Sprintf(" (default: %s)", f.DefValue) | ||
| } | ||
| entry += " " + f.Usage | ||
| flags = append(flags, entry) | ||
| }) | ||
|
|
||
| if len(flags) > 0 { | ||
| fmt.Println(strings.Join(flags, "\n")) | ||
| fmt.Println() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package search | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/briandowns/spinner" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/zeabur/cli/internal/cmdutil" | ||
| "github.com/zeabur/cli/pkg/model" | ||
| ) | ||
|
|
||
| type Options struct { | ||
| keyword string | ||
| } | ||
|
|
||
| func NewCmdSearch(f *cmdutil.Factory) *cobra.Command { | ||
| opts := Options{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "search [keyword]", | ||
| Short: "Search templates by keyword", | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) > 0 { | ||
| opts.keyword = args[0] | ||
| } | ||
| return runSearch(f, opts) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runSearch(f *cmdutil.Factory, opts Options) error { | ||
| if opts.keyword == "" { | ||
| if f.Interactive { | ||
| keyword, err := f.Prompter.Input("Search keyword: ", "") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opts.keyword = keyword | ||
| } else { | ||
| return fmt.Errorf("keyword is required") | ||
| } | ||
| } | ||
|
|
||
| s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval, | ||
| spinner.WithColor(cmdutil.SpinnerColor), | ||
| spinner.WithSuffix(" Searching templates..."), | ||
| ) | ||
| s.Start() | ||
| allTemplates, err := f.ApiClient.ListAllTemplates(context.Background()) | ||
| if err != nil { | ||
| s.Stop() | ||
| return err | ||
| } | ||
| s.Stop() | ||
|
|
||
| keyword := strings.ToLower(opts.keyword) | ||
| var matched model.Templates | ||
| for _, t := range allTemplates { | ||
| name := strings.ToLower(t.Name) | ||
| desc := strings.ToLower(t.Description) | ||
| if strings.Contains(name, keyword) || strings.Contains(desc, keyword) { | ||
| matched = append(matched, t) | ||
| } | ||
| } | ||
|
|
||
| sort.Slice(matched, func(i, j int) bool { | ||
| return matched[i].DeploymentCnt > matched[j].DeploymentCnt | ||
| }) | ||
|
|
||
| if len(matched) == 0 { | ||
| fmt.Println("No templates found") | ||
| return nil | ||
| } | ||
|
|
||
| header := []string{"Code", "Name", "Description", "Deployments"} | ||
| rows := make([][]string, 0, len(matched)) | ||
| for _, t := range matched { | ||
| rows = append(rows, []string{t.Code, t.Name, t.Description, strconv.Itoa(t.DeploymentCnt)}) | ||
| } | ||
| f.Printer.Table(header, rows) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Validate empty keyword after interactive prompt.
In interactive mode, if the user presses Enter without typing a keyword, the search proceeds with an empty string which matches all templates (since
strings.Contains(x, "")is always true). This is inconsistent with non-interactive mode which requires a keyword.Consider adding validation after the prompt, similar to how
template gethandles it:💡 Suggested fix
func runSearch(f *cmdutil.Factory, opts Options) error { if opts.keyword == "" { if f.Interactive { keyword, err := f.Prompter.Input("Search keyword: ", "") if err != nil { return err } opts.keyword = keyword + if opts.keyword == "" { + return fmt.Errorf("keyword is required") + } } else { return fmt.Errorf("keyword is required") } }📝 Committable suggestion
🤖 Prompt for AI Agents