github.com/wulonghui/docker@v1.8.0-rc2/pkg/plugins/pluginrpc-gen/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"go/format"
     8  	"io/ioutil"
     9  	"os"
    10  	"unicode"
    11  	"unicode/utf8"
    12  )
    13  
    14  type stringSet struct {
    15  	values map[string]struct{}
    16  }
    17  
    18  func (s stringSet) String() string {
    19  	return ""
    20  }
    21  
    22  func (s stringSet) Set(value string) error {
    23  	s.values[value] = struct{}{}
    24  	return nil
    25  }
    26  func (s stringSet) GetValues() map[string]struct{} {
    27  	return s.values
    28  }
    29  
    30  var (
    31  	typeName   = flag.String("type", "", "interface type to generate plugin rpc proxy for")
    32  	rpcName    = flag.String("name", *typeName, "RPC name, set if different from type")
    33  	inputFile  = flag.String("i", "", "input file path")
    34  	outputFile = flag.String("o", *inputFile+"_proxy.go", "output file path")
    35  
    36  	skipFuncs   map[string]struct{}
    37  	flSkipFuncs = stringSet{make(map[string]struct{})}
    38  
    39  	flBuildTags = stringSet{make(map[string]struct{})}
    40  )
    41  
    42  func errorOut(msg string, err error) {
    43  	if err == nil {
    44  		return
    45  	}
    46  	fmt.Fprintf(os.Stderr, "%s: %v\n", msg, err)
    47  	os.Exit(1)
    48  }
    49  
    50  func checkFlags() error {
    51  	if *outputFile == "" {
    52  		return fmt.Errorf("missing required flag `-o`")
    53  	}
    54  	if *inputFile == "" {
    55  		return fmt.Errorf("missing required flag `-i`")
    56  	}
    57  	return nil
    58  }
    59  
    60  func main() {
    61  	flag.Var(flSkipFuncs, "skip", "skip parsing for function")
    62  	flag.Var(flBuildTags, "tag", "build tags to add to generated files")
    63  	flag.Parse()
    64  	skipFuncs = flSkipFuncs.GetValues()
    65  
    66  	errorOut("error", checkFlags())
    67  
    68  	pkg, err := Parse(*inputFile, *typeName)
    69  	errorOut(fmt.Sprintf("error parsing requested type %s", *typeName), err)
    70  
    71  	var analysis = struct {
    72  		InterfaceType string
    73  		RPCName       string
    74  		BuildTags     map[string]struct{}
    75  		*parsedPkg
    76  	}{toLower(*typeName), *rpcName, flBuildTags.GetValues(), pkg}
    77  	var buf bytes.Buffer
    78  
    79  	errorOut("parser error", generatedTempl.Execute(&buf, analysis))
    80  	src, err := format.Source(buf.Bytes())
    81  	errorOut("error formating generated source", err)
    82  	errorOut("error writing file", ioutil.WriteFile(*outputFile, src, 0644))
    83  }
    84  
    85  func toLower(s string) string {
    86  	if s == "" {
    87  		return ""
    88  	}
    89  	r, n := utf8.DecodeRuneInString(s)
    90  	return string(unicode.ToLower(r)) + s[n:]
    91  }