update_providers.go

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