feat: migrate to golang
This commit is contained in:
175
internal/cli/subscription.go
Normal file
175
internal/cli/subscription.go
Normal file
@ -0,0 +1,175 @@
|
||||
package cli
|
||||
|
||||
import "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(),
|
||||
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",
|
||||
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 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
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user