package cli import ( "fmt" "github.com/spf13/cobra" ) func newSubscriptionCmd() *cobra.Command { cmd := &cobra.Command{ Use: "subscription", Short: "Manage subscriptions", } cmd.AddCommand( newSubscriptionAddCmd(), newSubscriptionRefreshCmd(), newSubscriptionRmCmd(), newSubscriptionRenameCmd(), newSubscriptionSetURLCmd(), newSubscriptionGetURLCmd(), newSubscriptionActivateCmd(), newSubscriptionDeactivateCmd(), newSubscriptionMoveCmd(), newSubscriptionListCmd(), newSubscriptionStorageCmd(), ) return cmd } func newSubscriptionAddCmd() *cobra.Command { return &cobra.Command{ Use: "add ", Short: "Add a new subscription", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.AddSubscription(args[0], args[1]) return nil }, } } func newSubscriptionRefreshCmd() *cobra.Command { var backup, all bool c := &cobra.Command{ Use: "refresh [name]", Short: "Refresh a subscription (all active ones if name is omitted)", Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { if all && len(args) > 0 { return fmt.Errorf("cannot specify a subscription name together with --all") } m, err := newManagers("", "", "") if err != nil { return err } var names []string switch { case len(args) == 1: names = []string{args[0]} case all: names = m.Subscription.Data.OrderedNames() default: for _, sub := range m.Subscription.Data.GetActiveSubscriptions() { names = append(names, sub.Name) } } if len(names) == 0 { fmt.Println("❌ No active subscription found") return nil } for _, name := range names { m.Subscription.RefreshSubscription(name, backup) } m.Core.ReloadService() return nil }, } c.Flags().BoolVar(&backup, "backup", false, "Backup the existing file before refreshing") c.Flags().BoolVarP(&all, "all", "a", false, "Refresh every subscription, not just active ones") return c } func newSubscriptionRmCmd() *cobra.Command { return &cobra.Command{ Use: "rm ", Short: "Delete a subscription", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.DeleteSubscription(args[0]) return nil }, } } func newSubscriptionRenameCmd() *cobra.Command { return &cobra.Command{ Use: "rename ", Short: "Rename a subscription", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.RenameSubscription(args[0], args[1]) return nil }, } } func newSubscriptionSetURLCmd() *cobra.Command { return &cobra.Command{ Use: "set-url ", Short: "Update the URL for a subscription", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.SetSubscriptionURL(args[0], args[1]) return nil }, } } func newSubscriptionGetURLCmd() *cobra.Command { return &cobra.Command{ Use: "get-url ", Short: "Show the URL for a subscription", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.GetSubscriptionURL(args[0]) return nil }, } } func newSubscriptionActivateCmd() *cobra.Command { return &cobra.Command{ Use: "activate ", Short: "Activate a subscription (additive — other active subscriptions stay active)", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.ActivateSubscription(args[0]) if m.CoreConfig.Apply() { m.Core.ReloadService() } return nil }, } } func newSubscriptionDeactivateCmd() *cobra.Command { return &cobra.Command{ Use: "deactivate ", Short: "Deactivate a subscription", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.DeactivateSubscription(args[0]) if m.CoreConfig.Apply() { m.Core.ReloadService() } return nil }, } } func newSubscriptionMoveCmd() *cobra.Command { var before, after string c := &cobra.Command{ Use: "move --before|--after ", Short: "Move a subscription before or after another in the list (affects display and config apply merge order)", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { if (before == "") == (after == "") { return fmt.Errorf("specify exactly one of --before or --after") } target := before isBefore := true if after != "" { target = after isBefore = false } m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.MoveSubscription(args[0], target, isBefore) return nil }, } c.Flags().StringVar(&before, "before", "", "Move the subscription immediately before this one") c.Flags().StringVar(&after, "after", "", "Move the subscription immediately after this one") return c } func newSubscriptionListCmd() *cobra.Command { return &cobra.Command{ Use: "list", Short: "List all subscriptions", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.ListSubscriptions() return nil }, } } func newSubscriptionStorageCmd() *cobra.Command { return &cobra.Command{ Use: "storage", Short: "Show storage information", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { m, err := newManagers("", "", "") if err != nil { return err } m.Subscription.ShowStorageInfo() return nil }, } }