mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-10-28 01:31:19 +00:00
✨ The convert command has a new flag: config
🐛 Fix Fetch func cannot return err 🐛 Fix cannot read templates correctly
This commit is contained in:
138
cmd/convert.go
138
cmd/convert.go
@@ -1,35 +1,42 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sub2sing-box/common"
|
||||
"sub2sing-box/model"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var subscriptions []string
|
||||
var proxies []string
|
||||
var template string
|
||||
var output string
|
||||
var delete string
|
||||
var rename map[string]string
|
||||
var group bool
|
||||
var groupType string
|
||||
var sortKey string
|
||||
var sortType string
|
||||
var (
|
||||
subscriptions []string
|
||||
proxies []string
|
||||
template string
|
||||
output string
|
||||
delete string
|
||||
rename map[string]string
|
||||
group bool
|
||||
groupType string
|
||||
sortKey string
|
||||
sortType string
|
||||
config string
|
||||
)
|
||||
|
||||
func init() {
|
||||
convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", []string{}, "subscription urls")
|
||||
convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", []string{}, "common proxies")
|
||||
convertCmd.Flags().StringSliceVarP(&subscriptions, "subscription", "s", nil, "subscription URLs")
|
||||
convertCmd.Flags().StringSliceVarP(&proxies, "proxy", "p", nil, "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")
|
||||
convertCmd.Flags().StringToStringVarP(&rename, "rename", "r", nil, "rename proxy with regex")
|
||||
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")
|
||||
convertCmd.Flags().StringVarP(&config, "config", "c", "", "config file path")
|
||||
RootCmd.AddCommand(convertCmd)
|
||||
}
|
||||
|
||||
@@ -37,32 +44,95 @@ var convertCmd = &cobra.Command{
|
||||
Use: "convert",
|
||||
Long: "Convert common proxy to sing-box proxy",
|
||||
Short: "Convert common proxy to sing-box proxy",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
result := ""
|
||||
var err error
|
||||
result, err = common.Convert(
|
||||
subscriptions,
|
||||
proxies,
|
||||
template,
|
||||
delete,
|
||||
rename,
|
||||
group,
|
||||
groupType,
|
||||
sortKey,
|
||||
sortType,
|
||||
)
|
||||
Run: convertRun,
|
||||
}
|
||||
|
||||
func convertRun(cmd *cobra.Command, args []string) {
|
||||
loadConfig()
|
||||
result, err := common.Convert(
|
||||
subscriptions,
|
||||
proxies,
|
||||
template,
|
||||
delete,
|
||||
rename,
|
||||
group,
|
||||
groupType,
|
||||
sortKey,
|
||||
sortType,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println("Conversion error:", err)
|
||||
return
|
||||
}
|
||||
if output != "" {
|
||||
err = os.WriteFile(output, []byte(result), 0666)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Error writing to file:", err)
|
||||
return
|
||||
}
|
||||
if output != "" {
|
||||
err = os.WriteFile(output, []byte(result), 0666)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Config has been saved in:", output)
|
||||
} else {
|
||||
fmt.Println(result)
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() {
|
||||
if config == "" {
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
config = filepath.Join(wd, "sub2sing-box.json")
|
||||
if _, err := os.Stat(config); os.IsNotExist(err) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
fmt.Println(string(result))
|
||||
fmt.Println("Error getting working directory:", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
bytes, err := os.ReadFile(config)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading config file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var cfg model.ConvertRequest
|
||||
if err := json.Unmarshal(bytes, &cfg); err != nil {
|
||||
fmt.Println("Error parsing config JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
mergeConfig(cfg)
|
||||
}
|
||||
|
||||
func mergeConfig(cfg model.ConvertRequest) {
|
||||
if len(subscriptions) == 0 {
|
||||
subscriptions = cfg.Subscriptions
|
||||
}
|
||||
if len(proxies) == 0 {
|
||||
proxies = cfg.Proxies
|
||||
}
|
||||
if template == "" {
|
||||
template = cfg.Template
|
||||
}
|
||||
if delete == "" {
|
||||
delete = cfg.Delete
|
||||
}
|
||||
if len(rename) == 0 {
|
||||
rename = cfg.Rename
|
||||
}
|
||||
if !group {
|
||||
group = cfg.Group
|
||||
}
|
||||
if groupType == "" {
|
||||
groupType = cfg.GroupType
|
||||
}
|
||||
if sortKey == "" {
|
||||
sortKey = cfg.SortKey
|
||||
}
|
||||
if sortType == "" {
|
||||
sortType = cfg.SortType
|
||||
}
|
||||
if output == "" {
|
||||
output = cfg.Output
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user