Files
ss/internal/cli/subscription.go

232 lines
5.4 KiB
Go

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 <name> <url>",
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 bool
c := &cobra.Command{
Use: "refresh <name>",
Short: "Refresh a subscription",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
m, err := newManagers("", "", "")
if err != nil {
return err
}
m.Subscription.RefreshSubscription(args[0], backup)
m.Core.ReloadService("mihomo")
return nil
},
}
c.Flags().BoolVar(&backup, "backup", false, "Backup the existing file before refreshing")
return c
}
func newSubscriptionRmCmd() *cobra.Command {
return &cobra.Command{
Use: "rm <name>",
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 <name> <new_name>",
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 <name> <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 <name>",
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 <name>",
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("mihomo")
}
return nil
},
}
}
func newSubscriptionDeactivateCmd() *cobra.Command {
return &cobra.Command{
Use: "deactivate <name>",
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("mihomo")
}
return nil
},
}
}
func newSubscriptionMoveCmd() *cobra.Command {
var before, after string
c := &cobra.Command{
Use: "move <name> --before|--after <target>",
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
},
}
}