summaryrefslogtreecommitdiff
path: root/addMutex.go
blob: fc1af97a0c4944ab067cd93dbe4d853db389c65f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

// will this help things?
// this is a hack for testing for now
// cram a mutex in the pb.go file

import (
	"errors"
	"fmt"
	"os"
	"strings"

	"go.wit.com/log"
)

func (pb *Files) addMutex(f *File) error {
	fullname := f.Filebase + ".pb.go"
	log.Info("fullname:", fullname)
	data, err := os.ReadFile(fullname)
	if err != nil {
		// log.Info("open config file :", err)
		return err
	}

	w, _ := os.OpenFile(f.Filebase+".pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)

	var found bool

	lines := strings.Split(string(data), "\n")
	for _, line := range lines {
		if strings.HasPrefix(line, "package ") {
			log.Info("CHANGING package:", line, "to package:", f.Package)
			fmt.Fprintln(w, "package "+f.Package)
			// log.Info("CHANGING package:", line, "to package:main")
			// fmt.Fprintln(w, "package "+"main")
			continue
		}
		// log.Info("line:", line)
		start := "type " + "sunshine" + " struct {"
		if strings.HasSuffix(line, start) {
			found = true
			log.Info("FOUND line:", line)
			fmt.Fprintln(w, line)
			fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
			fmt.Fprintln(w, "")
		} else {
			fmt.Fprintln(w, line)
		}
	}
	// os.Exit(-1)
	if found {
		return nil
	}
	return errors.New("addMutex() parse didn't work")
}