summaryrefslogtreecommitdiff
path: root/main.go
blob: 7c9db2d48d35296be71018575a16153e2d379973 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

// protobuf the way I am using them, require GO 1.20. I think. I could be wrong.
// The Go Protocol Buffers library embeds a sync.Mutex within the MessageState struct to prevent unintended shallow copies of message structs
// this optionally (but it is the default) inserts a mutex into the struct generated by protoc

// probably don't need these build lines anymore
//go:build go1.20
// +build go1.20

// go:generate autogenpb --proto file.proto

package main

import (
	"os"
	"strings"

	"go.wit.com/lib/fhelp"
	"go.wit.com/lib/gui/prep"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

// sent via -ldflags
var VERSION string
var BUILDTIME string

var ARGNAME string = "autogenpb"

func main() {
	me = new(mainType)
	me.sh = prep.Autocomplete(&argv) // adds shell auto complete to go-args
	me.pb = new(Files)

	if argv.Identify != "" {
		if err := doIdentify(argv.Identify); err != nil {
			badExit(err)
		}
		okExit("")
	}

	// you need a proto file
	if argv.Proto == "" {
		log.Info("you must provide --proto <filename>")
		me.sh.WriteHelp()
		os.Exit(-1)
	}

	if !shell.Exists(argv.Proto) {
		log.Info("protobuf", argv.Proto, "is missing")
		os.Exit(-1)
	}

	if !strings.HasSuffix(argv.Proto, ".proto") {
		log.Info("protobuf", argv.Proto, "must end in .proto")
		os.Exit(-1)
	}

	if path, err := fhelp.CheckCmd("goimports"); err != nil {
		log.Info("this tool requires goimports")
		if path != "" {
			log.Info("path might be:", path)
		}
		log.Info("this tool requires goimports")
		cmd := []string{"go", "install", "-v", "-x", "golang.org/x/tools/cmd/goimports@latest"}
		log.Info("Need to run:", cmd)
		if fhelp.QuestionUser("build goimports?") {
			shell.RunRealtime(cmd)
		} else {
			os.Exit(-1)
		}
	}

	doProto(argv.Proto)
}

func okExit(s string) {
	log.Info("autogenpb ok", s)
	os.Exit(0)
}

func badExit(err error) {
	log.Info("autogenpb error:", err)
	os.Exit(-1)
}