diff options
| author | Jeff Carr <[email protected]> | 2025-01-10 07:40:24 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-10 07:40:24 -0600 |
| commit | e07c6a35fd5d2ca1b3a6c09d9781d21f676f52f2 (patch) | |
| tree | ce7a4fd38fff09571739bedd5d6dd00d99cff4b7 /addMutex.go | |
| parent | d9e5edb3a8486bc884841137095dd910e42b3a98 (diff) | |
detect that autogenpb has already been run and exit
Diffstat (limited to 'addMutex.go')
| -rw-r--r-- | addMutex.go | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/addMutex.go b/addMutex.go index 5e374f8..12ea1d0 100644 --- a/addMutex.go +++ b/addMutex.go @@ -21,6 +21,14 @@ func (pb *Files) addMutex(f *File) error { return err } + // check if autogenpb has already looked at this file + for _, line := range strings.Split(string(data), "\n") { + if strings.Contains(line, "autogenpb DO NOT EDIT") { + log.Info("autogenpb has already been run") + return nil + } + } + w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) pbHeaderComment(w) @@ -34,30 +42,20 @@ func (pb *Files) addMutex(f *File) error { // fmt.Fprintln(w, "package "+"main") continue } - var found bool - for _, msg := range f.MsgNames { - start := "type " + msg.Name + " struct {" - // marshalThing(w, msg.Name) - // log.Info("line:", line) - if strings.HasPrefix(line, start) { - msg.MutexFound = true - found = true - if argv.Mutex { - log.Info("Adding Mutex to line:", line) - fmt.Fprintln(w, line) - fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") - fmt.Fprintln(w, "") - } else { - log.Info("Skipping. Mutex = false for", msg.Name) - fmt.Fprintln(w, line) - fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg") - fmt.Fprintln(w, "") - } - // important to exit here. somehow this matched twice at one point. notsure how - break + + if f.structMatch(line) { + if argv.Mutex { + log.Info("Adding Mutex to:", line) + fmt.Fprintln(w, line) + fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") + fmt.Fprintln(w, "") + } else { + log.Info("Skipping. Mutex = false for:", line) + fmt.Fprintln(w, line) + fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg") + fmt.Fprintln(w, "") } - } - if !found { + } else { fmt.Fprintln(w, line) } } @@ -68,3 +66,15 @@ func (pb *Files) addMutex(f *File) error { } return nil } + +// is this struct supposed to have a Mutex added? +func (pf *File) structMatch(line string) bool { + for _, msg := range pf.MsgNames { + start := "type " + msg.Name + " struct {" + if strings.HasPrefix(line, start) { + msg.MutexFound = true + return true + } + } + return false +} |
