update_providers.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 updateProvidersCmd = &cobra.Command{
  11. Use: "update-providers [path-or-url]",
  12. Short: "Update providers",
  13. Long: `Update the list of providers from a specified local path or remote URL.`,
  14. Example: `
  15. # Update providers remotely from Catwalk
  16. crush update-providers
  17. # Update providers from a custom URL
  18. crush update-providers https://example.com/
  19. # Update providers from a local file
  20. crush update-providers /path/to/local-providers.json
  21. # Update providers from embedded version
  22. crush update-providers embedded
  23. `,
  24. RunE: func(cmd *cobra.Command, args []string) error {
  25. // NOTE(@andreynering): We want to skip logging output do stdout here.
  26. slog.SetDefault(slog.New(slog.DiscardHandler))
  27. var pathOrURL string
  28. if len(args) > 0 {
  29. pathOrURL = args[0]
  30. }
  31. if err := config.UpdateProviders(pathOrURL); err != nil {
  32. return err
  33. }
  34. // NOTE(@andreynering): This style is more-or-less copied from Fang's
  35. // error message, adapted for success.
  36. headerStyle := lipgloss.NewStyle().
  37. Foreground(charmtone.Butter).
  38. Background(charmtone.Guac).
  39. Bold(true).
  40. Padding(0, 1).
  41. Margin(1).
  42. MarginLeft(2).
  43. SetString("SUCCESS")
  44. textStyle := lipgloss.NewStyle().
  45. MarginLeft(2).
  46. SetString("Providers updated successfully.")
  47. fmt.Printf("%s\n%s\n\n", headerStyle.Render(), textStyle.Render())
  48. return nil
  49. },
  50. }