update_providers.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package cmd
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "charm.land/lipgloss/v2"
  6. "github.com/charmbracelet/crush/internal/config"
  7. "github.com/charmbracelet/x/exp/charmtone"
  8. "github.com/spf13/cobra"
  9. )
  10. var updateProvidersSource string
  11. var updateProvidersCmd = &cobra.Command{
  12. Use: "update-providers [path-or-url]",
  13. Short: "Update providers",
  14. Long: `Update provider information from a specified local path or remote URL.`,
  15. Example: `
  16. # Update Catwalk providers remotely (default)
  17. crush update-providers
  18. # Update Catwalk providers from a custom URL
  19. crush update-providers https://example.com/providers.json
  20. # Update Catwalk providers from a local file
  21. crush update-providers /path/to/local-providers.json
  22. # Update Catwalk providers from embedded version
  23. crush update-providers embedded
  24. # Update Hyper provider information
  25. crush update-providers --source=hyper
  26. # Update Hyper from a custom URL
  27. crush update-providers --source=hyper https://hyper.example.com
  28. `,
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. // NOTE(@andreynering): We want to skip logging output do stdout here.
  31. slog.SetDefault(slog.New(slog.DiscardHandler))
  32. var pathOrURL string
  33. if len(args) > 0 {
  34. pathOrURL = args[0]
  35. }
  36. var err error
  37. switch updateProvidersSource {
  38. case "catwalk":
  39. err = config.UpdateProviders(pathOrURL)
  40. case "hyper":
  41. err = config.UpdateHyper(pathOrURL)
  42. default:
  43. return fmt.Errorf("invalid source %q, must be 'catwalk' or 'hyper'", updateProvidersSource)
  44. }
  45. if err != nil {
  46. return err
  47. }
  48. // NOTE(@andreynering): This style is more-or-less copied from Fang's
  49. // error message, adapted for success.
  50. headerStyle := lipgloss.NewStyle().
  51. Foreground(charmtone.Butter).
  52. Background(charmtone.Guac).
  53. Bold(true).
  54. Padding(0, 1).
  55. Margin(1).
  56. MarginLeft(2).
  57. SetString("SUCCESS")
  58. textStyle := lipgloss.NewStyle().
  59. MarginLeft(2).
  60. SetString(fmt.Sprintf("%s provider updated successfully.", updateProvidersSource))
  61. fmt.Printf("%s\n%s\n\n", headerStyle.Render(), textStyle.Render())
  62. return nil
  63. },
  64. }
  65. func init() {
  66. updateProvidersCmd.Flags().StringVar(&updateProvidersSource, "source", "catwalk", "Provider source to update (catwalk or hyper)")
  67. }