summaryrefslogtreecommitdiff
path: root/main.go
blob: 6645fb7a1eec8f0a59d831c3d79d95f91db13387 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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 (
	"errors"
	"os"
	"strings"

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

func main() {
	me = new(mainType)
	argvpb.Init(&argv, APPNAME, BUILDTIME, VERSION) // adds shell auto-complete
	me.pb = new(Files)
	var s string
	var err error

	if argv.Identify != "" {
		err = doIdentify(argv.Identify)
	}

	// check if it's ok to run autogenpb
	s, err = doPrecheck()

	if err == nil {
		// it's safe to run
		s, err = doProto(argv.Proto)
	}

	// safe exits back to your shell (with timing and toolkit close)
	if err != nil {
		argvpb.BadExit(s, err)
	}
	argvpb.GoodExit(s)
}

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

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

func doPrecheck() (string, error) {
	var s string
	var err error
	// you need a proto file
	if argv.Proto == "" {
		// todo: run on every .proto file
		log.Info("todo: run on all .proto files")
		me.pp.WriteHelp(os.Stdout)
		return "you must provide --proto <protoname>.proto", errors.New("need .proto")
	}

	if !shell.Exists(argv.Proto) {
		s = log.Sprintf("protobuf %s is missing", argv.Proto)
		return "missing file", errors.New(s)
	}

	if !strings.HasSuffix(argv.Proto, ".proto") {
		s = "wrote filetype"
		err = errors.New(log.Sprintf("protobuf %s doesn't end in '.proto'", argv.Proto))
		return s, err
	}

	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 into ~/go/bin/ ?") {
			shell.RunRealtime(cmd)
		} else {
			s = "autogenpb missing goimports"
			err = errors.New("need goimports")
		}
	}
	return s, err
}