Skip to content

Commit

Permalink
Merge pull request elastic#280 from Ben-Aldrich/dynamic
Browse files Browse the repository at this point in the history
Dynamic rewrite of proto files for go with a go script
  • Loading branch information
Ben Aldrich committed Mar 9, 2015
2 parents 14ae130 + 91238cc commit fe61564
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 6 deletions.
8 changes: 4 additions & 4 deletions protofiles/stats/stats.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ option java_outer_classname = "CounterMsg";

message Counter {
required string name = 1;
optional int64 value = 2;
optional double value = 2;
optional int64 time = 3;
optional int32 sampleRate = 4;
}

message Sample {
required string name = 1;
optional int64 value = 2;
optional double value = 2;
optional int32 sampleRate = 3;
optional int64 time = 4;
}

message Measure {
required string name = 1;
optional int64 min = 2;
optional int64 value = 3;
optional double value = 3;
optional int64 max = 4;
optional int32 sampleRate = 5;
optional int64 time = 4;
optional int64 time = 6;
}

message Stats {
Expand Down
6 changes: 4 additions & 2 deletions scripts/buildProtoFilesGo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ if [ ! -d "$goSrc" ]; then
cd $startDir
fi

(cd "$protoFileDir"/stats; cp "$protoFileDir"/stats/* "$goSrc"/stats/ )
(cd "$protoFileDir"; cp `ls | grep -v Config | grep -v ESData | grep -v RuleConf | grep -v DpiMsgLRproto | grep -v Applications | grep -v BaseConfMsg | grep -v stats` "$goSrc"/ )
#(cd "$protoFileDir"/stats; cp "$protoFileDir"/stats/* "$goSrc"/stats/ )
#(cd "$protoFileDir"; cp `ls | grep -v Config | grep -v ESData | grep -v RuleConf | grep -v DpiMsgLRproto | grep -v Applications | grep -v BaseConfMsg | grep -v stats` "$goSrc"/ )

(cd "$protoFileDir"; "$scriptsDir"/RewriteProto/RewriteProto . $goSrc/)

( cd $goSrc; protoc -I="$GOPATH"/src/:/usr/local/include:/usr/include:$goSrc:/$goSrc/stats/ --gogo_out=$GOPATH/src/ "$goSrc"/*.proto )
( cd "$goSrc"/stats; protoc -I="$GOPATH"/src/:/usr/local/include:/usr/include:$goSrc:$goSrc/stats/ --gogo_out=$GOPATH/src/ "$goSrc"/stats/*.proto )
Expand Down
Binary file added scripts/rewriteProto/RewriteProto
Binary file not shown.
21 changes: 21 additions & 0 deletions scripts/rewriteProto/c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"gopkg.in/yaml.v2"
"io/ioutil"
)

type Conf struct {
Regex string `yaml:"regex"`
Text string `yaml:"text"`
Exclude []string `yaml:"exclude"`
}

func GetConf(path string) (c Conf, err error) {
data, err := ioutil.ReadFile(path)
if nil != err {
return c, err
}
err = yaml.Unmarshal([]byte(data), &c)
return c, err
}
31 changes: 31 additions & 0 deletions scripts/rewriteProto/c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
regex: ^package.*;
text: |
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
//go options that improve code generation
option (gogoproto.gostring_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
exclude:
- RuleConf
- DpiMsgLRproto
- Applications
- BaseConfMsg
- ConfTypeMsg
- ConfigType
- ConfigDefaults
- ConfigReplyMsg
- ConfigRequestMsg
- ESDataMsg
- ESDataCollectionMsg
- IndexManagerConfig
- MaintenanceConfig
- PortRange
- MetricsConfig
144 changes: 144 additions & 0 deletions scripts/rewriteProto/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"bufio"
"flag"
"fmt"
"regexp"
// "io"
"os"
"strings"
)

func CopyFileWithNewImport(source string, dest string, conf Conf) (err error) {
packageLine := regexp.MustCompile(conf.Regex)
sourcefile, err := os.Open(source)
if err != nil {
return err
}

defer sourcefile.Close()
os.Remove(dest) // ignore error but delete file if it already existed
destfile, err := os.Create(dest)
if err != nil {
return err
}

defer destfile.Close()

scanner := bufio.NewScanner(sourcefile)
scanner.Split(bufio.ScanLines)
writer := bufio.NewWriter(destfile)
defer writer.Flush()
for scanner.Scan() {
// destfile.WriteString(scanner.Text())
fmt.Fprintln(writer, scanner.Text())
if packageLine.MatchString(scanner.Text()) {
writer.WriteString(conf.Text)
}
}

return err
}

func CopyDir(source string, dest string, conf Conf) (err error) {

// get properties of source dir
sourceinfo, err := os.Stat(source)
if err != nil {
return err
}

// create dest dir

err = os.MkdirAll(dest, sourceinfo.Mode())
if err != nil {
return err
}

directory, _ := os.Open(source)

objects, err := directory.Readdir(-1)

loop:
for _, obj := range objects {

sourcefilepointer := source + "/" + obj.Name()

destinationfilepointer := dest + "/" + obj.Name()

if obj.IsDir() {
// create sub-directories - recursively
err = CopyDir(sourcefilepointer, destinationfilepointer, conf)
if err != nil {
fmt.Println(err)
}
} else {
for _, exclude := range conf.Exclude {
if strings.Contains(sourcefilepointer, exclude) {
fmt.Println("Excluding: ", sourcefilepointer)
continue loop
}
}
// perform copy
err = CopyFileWithNewImport(sourcefilepointer, destinationfilepointer, conf)
if err != nil {
fmt.Println(err)
}
}

}
return
}

var confPath = flag.String("conf", "../scripts/RewriteProto/c.yml", "../scripts/RewriteProto/c.yml")

func main() {
flag.Parse() // get the source and destination directory
fmt.Println(*confPath)
conf, err := GetConf(*confPath)
if nil != err {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(conf)
source_dir := flag.Arg(0) // get the source directory from 1st argument

dest_dir := flag.Arg(1) // get the destination directory from the 2nd argument

if len(source_dir) <= 0 {
fmt.Println("No source directory given")
os.Exit(1)
}

if len(dest_dir) <= 0 {
fmt.Println("No destination directory given")
os.Exit(1)
}

fmt.Println("Source :" + source_dir)

// check if the source dir exist
src, err := os.Stat(source_dir)
if err != nil {
panic(err)
}

if !src.IsDir() {
fmt.Println("Source is not a directory")
os.Exit(1)
}

// create the destination directory
fmt.Println("Destination :" + dest_dir)

_, err = os.Open(dest_dir)

err = CopyDir(source_dir, dest_dir, conf)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Directory copied")
}

}

0 comments on commit fe61564

Please sign in to comment.