templates_cmd.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package cli
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. "github.com/kotoyuuko/cc-switch-cli/internal/templates"
  6. )
  7. func newTemplatesCmd(app *appState) *cobra.Command {
  8. cmd := &cobra.Command{
  9. Use: "templates",
  10. Short: "Inspect built-in provider templates",
  11. }
  12. cmd.AddCommand(
  13. &cobra.Command{
  14. Use: "list",
  15. Short: "List template names",
  16. RunE: func(cmd *cobra.Command, args []string) error {
  17. for _, t := range templates.All() {
  18. fmt.Fprintf(app.stdout, "%s\t%s\n", t.Name, t.Description)
  19. }
  20. return nil
  21. },
  22. },
  23. &cobra.Command{
  24. Use: "show <name>",
  25. Short: "Show a template's env keys, hints, and defaults",
  26. Args: cobra.ExactArgs(1),
  27. RunE: func(cmd *cobra.Command, args []string) error {
  28. t, ok := templates.Get(args[0])
  29. if !ok {
  30. return fmt.Errorf(
  31. "template %q not found; run `cc-switch templates list`",
  32. args[0])
  33. }
  34. fmt.Fprintf(app.stdout, "%s — %s\n", t.Name, t.Description)
  35. fmt.Fprintln(app.stdout, "env:")
  36. for _, e := range t.Env {
  37. line := " " + e.Name
  38. if e.Default != "" {
  39. line += fmt.Sprintf(" [%s]", e.Default)
  40. }
  41. if e.Hint != "" {
  42. line += " # " + e.Hint
  43. }
  44. fmt.Fprintln(app.stdout, line)
  45. }
  46. return nil
  47. },
  48. },
  49. )
  50. return cmd
  51. }