1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-25 08:50:54 -05:00
sub2sing-box/cmd/convert.go

69 lines
1.8 KiB
Go
Raw Normal View History

2024-03-11 05:31:29 -04:00
package cmd
import (
"fmt"
"os"
2024-03-11 11:39:58 -04:00
. "sub2sing-box/pkg/util"
2024-03-11 05:31:29 -04:00
"github.com/spf13/cobra"
)
2024-03-11 11:39:58 -04:00
var subscriptions []string
var proxies []string
var template string
var output string
var delete string
var rename map[string]string
2024-03-19 09:02:53 -04:00
var group bool
var groupType string
var sortKey string
var sortType string
2024-03-11 11:39:58 -04:00
func init() {
convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", []string{}, "subscription urls")
convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", []string{}, "common proxies")
convertCmd.Flags().StringVarP(&template, "template", "t", "", "template file path")
convertCmd.Flags().StringVarP(&output, "output", "o", "", "output file path")
convertCmd.Flags().StringVarP(&delete, "delete", "d", "", "delete proxy with regex")
convertCmd.Flags().StringToStringVarP(&rename, "rename", "r", map[string]string{}, "rename proxy with regex")
2024-03-19 09:02:53 -04:00
convertCmd.Flags().BoolVarP(&group, "group", "g", false, "group proxies by country")
convertCmd.Flags().StringVarP(&groupType, "group-type", "G", "selector", "group type, selector or urltest")
convertCmd.Flags().StringVarP(&sortKey, "sort", "S", "tag", "sort key, tag or num")
convertCmd.Flags().StringVarP(&sortType, "sort-type", "T", "asc", "sort type, asc or desc")
2024-03-11 11:39:58 -04:00
RootCmd.AddCommand(convertCmd)
}
2024-03-11 09:00:13 -04:00
2024-03-11 05:31:29 -04:00
var convertCmd = &cobra.Command{
Use: "convert",
2024-03-11 07:56:50 -04:00
Long: "Convert common proxy to sing-box proxy",
Short: "Convert common proxy to sing-box proxy",
2024-03-11 05:31:29 -04:00
Run: func(cmd *cobra.Command, args []string) {
2024-03-11 09:00:13 -04:00
result := ""
var err error
2024-03-19 09:02:53 -04:00
result, err = Convert(
subscriptions,
proxies,
template,
delete,
rename,
group,
groupType,
sortKey,
sortType,
)
2024-03-11 09:00:13 -04:00
if err != nil {
fmt.Println(err)
return
}
if output != "" {
err = os.WriteFile(output, []byte(result), 0666)
2024-03-11 05:31:29 -04:00
if err != nil {
fmt.Println(err)
return
}
2024-03-11 09:00:13 -04:00
} else {
fmt.Println(string(result))
2024-03-11 05:31:29 -04:00
}
},
}