forked from logrhythm/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#280 from Ben-Aldrich/dynamic
Dynamic rewrite of proto files for go with a go script
- Loading branch information
Showing
6 changed files
with
204 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |