github.com/sagernet/sing-box@v1.9.0-rc.20/cmd/sing-box/cmd_geosite_list.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"sort"
     6  
     7  	"github.com/sagernet/sing-box/log"
     8  	F "github.com/sagernet/sing/common/format"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var commandGeositeList = &cobra.Command{
    14  	Use:   "list <category>",
    15  	Short: "List geosite categories",
    16  	Run: func(cmd *cobra.Command, args []string) {
    17  		err := geositeList()
    18  		if err != nil {
    19  			log.Fatal(err)
    20  		}
    21  	},
    22  }
    23  
    24  func init() {
    25  	commandGeoSite.AddCommand(commandGeositeList)
    26  }
    27  
    28  func geositeList() error {
    29  	var geositeEntry []struct {
    30  		category string
    31  		items    int
    32  	}
    33  	for _, category := range geositeCodeList {
    34  		sourceSet, err := geositeReader.Read(category)
    35  		if err != nil {
    36  			return err
    37  		}
    38  		geositeEntry = append(geositeEntry, struct {
    39  			category string
    40  			items    int
    41  		}{category, len(sourceSet)})
    42  	}
    43  	sort.SliceStable(geositeEntry, func(i, j int) bool {
    44  		return geositeEntry[i].items < geositeEntry[j].items
    45  	})
    46  	for _, entry := range geositeEntry {
    47  		os.Stdout.WriteString(F.ToString(entry.category, " (", entry.items, ")\n"))
    48  	}
    49  	return nil
    50  }