From 45c3bd3e1620a16707d429c1048c340368dd3a03 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 17 Jan 2025 02:51:16 -0600 Subject: [PATCH] parse out the uuid and version --- protoc.go | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/protoc.go b/protoc.go index 367ef64..01012cb 100644 --- a/protoc.go +++ b/protoc.go @@ -64,9 +64,12 @@ func ValidProtobuf(filename string) (string, string, error) { return "", "", fmt.Errorf("proto file does not have a UUID") } // ok, uuid is here - UUID := line - log.Info("found UUID:", line) - + UUID := parseUuid(line) + if UUID == "" { + noUuidExit(filename) + return "", "", fmt.Errorf("proto file does not have a UUID") + } + log.Info("found UUID:", UUID) scanner.Scan() line = scanner.Text() fields = strings.Fields(line) @@ -76,8 +79,12 @@ func ValidProtobuf(filename string) (string, string, error) { return "", "", fmt.Errorf("proto file does not have a version") } // found "version", the .proto file conforms - version := line - log.Info("found Version:", line) + version := parseVersion(line) + if version == "" { + noUuidExit(filename) + return "", "", fmt.Errorf("proto file does not have a version") + } + log.Info("found Version:", version) return UUID, version, nil } // right now, noPluralMessage does os.Exit(-1) @@ -85,6 +92,32 @@ func ValidProtobuf(filename string) (string, string, error) { return "", "", fmt.Errorf("proto file does not have message %s", pluralBase) } +func parseUuid(line string) string { + fields := strings.Split(line, "autogenpb:uuid:") + if len(fields) < 2 { + return "" + } + end := fields[1] + fields = strings.Fields(end) + uuid := fields[0] + uuid = strings.Trim(uuid, "`") + // log.Info("fhelp.parseUuid() uuid =", uuid) + return uuid +} + +func parseVersion(line string) string { + fields := strings.Split(line, "autogenpb:version:") + if len(fields) < 2 { + return "" + } + end := fields[1] + fields = strings.Fields(end) + ver := fields[0] + ver = strings.Trim(ver, "`") + // log.Info("fhelp.parseVersion() ver =", ver) + return ver +} + func noPluralMessageExit(filename string) { filebase := strings.TrimSuffix(filename, ".proto") base := cases.Title(language.English, cases.NoLower).String(filebase) @@ -129,7 +162,14 @@ func noUuidExit(filename string) { log.Info("") log.Info("If you don't have a UUID, you can use the randomly generated one here") log.Info("") + log.Info("If you wish to ignore this, set PROTOBUF_REGRET=true") + log.Info("You will probably regret not trying to follow the standard here. This format is used as a 'handshake'.") log.Info("###########################################################################") + if os.Getenv("PROTOBUF_REGRET") == "true" { + log.Info("PROTOBUF_REGRET=true was set") + log.Info("###########################################################################") + return + } badExit(fmt.Errorf("proto file error %s", filename)) }