summaryrefslogtreecommitdiff
path: root/changed.go
blob: 32159cd3b996600e9da597a15ebd800754c36735 (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
56
57
58
59
package config

import (
	"os"
	"runtime"
	"strings"
	"sync"
)

// this package can provide a trivial way to track which
// protobufs have been modified and need to be written to disk
// todo: autogenpb could generate code to work with this

var saveMap map[string]bool
var saveLock sync.Mutex

func init() {
	// init() should be avoided, but this package and for making
	// this small string map, it seems a sensible exception
	saveMap = make(map[string]bool)
}

func SetChanged(name string, b bool) {
	saveLock.Lock()
	defer saveLock.Unlock()
	saveMap[name] = b
}

func HasChanged(name string) bool {
	return saveMap[name]
}

// a simple function name shortcut
func Exists(filename string) bool {
	_, err := os.Stat(Path(filename))
	if os.IsNotExist(err) {
		return false
	}
	return true
}

// simple function name shortcut
func IsDir(dirname string) bool {
	info, err := os.Stat(Path(dirname))
	if os.IsNotExist(err) {
		return false
	}
	return info.IsDir()
}

// notsure if this is a thing anymore. don't care much either
func Path(filename string) string {
	// log.Log(INFO, "Path() START filename =", filename)
	if runtime.GOOS == "windows" {
		filename = strings.Replace(filename, "/", "\\", -1)
	}
	// log.Log(INFO, "Path() END filename =", filename)
	return filename
}