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

     1  package main
     2  
     3  import (
     4  	"github.com/sagernet/sing-box/common/geosite"
     5  	"github.com/sagernet/sing-box/log"
     6  	E "github.com/sagernet/sing/common/exceptions"
     7  
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	commandGeoSiteFlagFile string
    13  	geositeReader          *geosite.Reader
    14  	geositeCodeList        []string
    15  )
    16  
    17  var commandGeoSite = &cobra.Command{
    18  	Use:   "geosite",
    19  	Short: "Geosite tools",
    20  	PersistentPreRun: func(cmd *cobra.Command, args []string) {
    21  		err := geositePreRun()
    22  		if err != nil {
    23  			log.Fatal(err)
    24  		}
    25  	},
    26  }
    27  
    28  func init() {
    29  	commandGeoSite.PersistentFlags().StringVarP(&commandGeoSiteFlagFile, "file", "f", "geosite.db", "geosite file")
    30  	mainCommand.AddCommand(commandGeoSite)
    31  }
    32  
    33  func geositePreRun() error {
    34  	reader, codeList, err := geosite.Open(commandGeoSiteFlagFile)
    35  	if err != nil {
    36  		return E.Cause(err, "open geosite file")
    37  	}
    38  	geositeReader = reader
    39  	geositeCodeList = codeList
    40  	return nil
    41  }