mirror of
https://github.com/arkadiyt/protodump.git
synced 2026-10-21 09:46:52 +02:00
Initial commit
This commit is contained in:
commit
8df134ab79
10 files changed
+447
No files matched your search
+63
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/arkadiyt/protodump/pkg/proto"
|
||||
"github.com/arkadiyt/protodump/pkg/scan"
|
||||
)
|
||||
|
||||
var debug bool
|
||||
|
||||
func Debug(str string, a ...any) (int, error) {
|
||||
if debug {
|
||||
return fmt.Printf(str, a...)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't determine current working directory: %v\n", err)
|
||||
}
|
||||
|
||||
var file = flag.String("file", "", "The file to extract definitions from")
|
||||
var output = flag.String("output", cwd, "The output directory to save definitions in (will be created if it doesn't exist). Defaults to current directory.")
|
||||
flag.BoolVar(&debug, "v", false, "Verbose output")
|
||||
flag.Parse()
|
||||
|
||||
if *file == "" {
|
||||
fmt.Printf("Usage:\n")
|
||||
flag.PrintDefaults()
|
||||
return
|
||||
}
|
||||
|
||||
results, err := scan.ScanFile(*file)
|
||||
if err != nil {
|
||||
log.Fatalf("Got error scanning: %v\n", err)
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
definition, err := proto.NewFromBytes(result)
|
||||
if err != nil {
|
||||
Debug("Got error parsing definition: %v\n", err)
|
||||
} else {
|
||||
filename := definition.Filename()
|
||||
if strings.HasSuffix(filename, ".proto") {
|
||||
dir := path.Join(*output, path.Dir(filename))
|
||||
final := path.Join(dir, path.Base(filename))
|
||||
os.MkdirAll(dir, 0700)
|
||||
os.WriteFile(final, []byte(definition.String()), 0700)
|
||||
fmt.Printf("Wrote %s\n", final)
|
||||
} else {
|
||||
// Need to investigate further
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user