github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/main/commands/all/engineering/nonnativelinkextract.go (about)

     1  package engineering
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"sort"
     9  	"strings"
    10  
    11  	"github.com/v2fly/v2ray-core/v5/app/subscription/entries/nonnative"
    12  	"github.com/v2fly/v2ray-core/v5/main/commands/base"
    13  )
    14  
    15  type valueContainer struct {
    16  	key, value string
    17  }
    18  
    19  type orderedValueContainer []valueContainer
    20  
    21  func (o *orderedValueContainer) Len() int {
    22  	return len(*o)
    23  }
    24  
    25  func (o *orderedValueContainer) Less(i, j int) bool {
    26  	return strings.Compare((*o)[i].key, (*o)[j].key) < 0
    27  }
    28  
    29  func (o *orderedValueContainer) Swap(i, j int) {
    30  	(*o)[i], (*o)[j] = (*o)[j], (*o)[i]
    31  }
    32  
    33  var cmdNonNativeLinkExtract = &base.Command{
    34  	UsageLine: "{{.Exec}} engineering nonnativelinkextract",
    35  	Flag: func() flag.FlagSet {
    36  		fs := flag.NewFlagSet("", flag.ExitOnError)
    37  		return *fs
    38  	}(),
    39  	Run: func(cmd *base.Command, args []string) {
    40  		content, err := io.ReadAll(os.Stdin)
    41  		if err != nil {
    42  			base.Fatalf("%s", err)
    43  		}
    44  		flattenedLink := nonnative.ExtractAllValuesFromBytes(content)
    45  		var valueContainerOrdered orderedValueContainer
    46  
    47  		for key, value := range flattenedLink.Values {
    48  			valueContainerOrdered = append(valueContainerOrdered, valueContainer{key, value})
    49  		}
    50  		sort.Sort(&valueContainerOrdered)
    51  		for _, valueContainer := range valueContainerOrdered {
    52  			io.WriteString(os.Stdout, fmt.Sprintf("%s=%s\n", valueContainer.key, valueContainer.value))
    53  		}
    54  	},
    55  }