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

     1  package engineering
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/v2fly/v2ray-core/v5/app/subscription/entries/nonnative"
    10  	"github.com/v2fly/v2ray-core/v5/main/commands/base"
    11  )
    12  
    13  var cmdNonNativeLinkExecInputName *string
    14  
    15  var cmdNonNativeLinkExecTemplatePath *string
    16  
    17  var cmdNonNativeLinkExec = &base.Command{
    18  	UsageLine: "{{.Exec}} engineering nonnativelinkexec",
    19  	Flag: func() flag.FlagSet {
    20  		fs := flag.NewFlagSet("", flag.ExitOnError)
    21  		cmdNonNativeLinkExecInputName = fs.String("name", "", "")
    22  		cmdNonNativeLinkExecTemplatePath = fs.String("templatePath", "", "path for template directory (WARNING: This will not stop templates from reading file outside this directory)")
    23  		return *fs
    24  	}(),
    25  	Run: func(cmd *base.Command, args []string) {
    26  		cmd.Flag.Parse(args)
    27  
    28  		content, err := io.ReadAll(os.Stdin)
    29  		if err != nil {
    30  			base.Fatalf("%s", err)
    31  		}
    32  		flattenedLink := nonnative.ExtractAllValuesFromBytes(content)
    33  
    34  		matcher := nonnative.NewDefMatcher()
    35  		if *cmdNonNativeLinkExecTemplatePath != "" {
    36  			osFs := os.DirFS(*cmdNonNativeLinkExecTemplatePath)
    37  			err = matcher.LoadDefinitions(osFs)
    38  			if err != nil {
    39  				base.Fatalf("%s", err)
    40  			}
    41  		} else {
    42  			err = matcher.LoadEmbeddedDefinitions()
    43  			if err != nil {
    44  				base.Fatalf("%s", err)
    45  			}
    46  		}
    47  
    48  		spec, err := matcher.ExecuteNamed(flattenedLink, *cmdNonNativeLinkExecInputName)
    49  		if err != nil {
    50  			base.Fatalf("%s", err)
    51  		}
    52  		io.Copy(os.Stdout, bytes.NewReader(spec))
    53  	},
    54  }