-
Notifications
You must be signed in to change notification settings - Fork 0
/
otium.go
57 lines (48 loc) · 1.4 KB
/
otium.go
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
// Package otium allows incremental automation of manual procedures
package otium
import (
"errors"
"runtime/debug"
)
var (
// ErrUnrecoverable tells the REPL to exit.
//
// From client code, use the %w verb of fmt.Errorf:
// func(bag otium.Bag) error {
// return fmt.Errorf("failed to X... %w", otium.ErrUnrecoverable)
// },
ErrUnrecoverable = errors.New("(unrecoverable)")
version = "something-went-wrong"
)
// Internal errors and control flow.
var (
errBack = errors.New("go back (sentinel)")
)
func init() {
info, ok := debug.ReadBuildInfo()
if !ok {
version = "ReadBuildInfo-failed"
return
}
version = findOtiumVersion(info)
}
func findOtiumVersion(info *debug.BuildInfo) string {
// FIXME I should get this at runtime instead of hardcoding, but how?
const otiumPath = "github.com/marco-m/otium"
// If called by a program that uses otium as a dependency, then the otium
// version is in one of the dependency modules.
// Not robust if "replace" directive is used in go.mod
for _, mod := range info.Deps {
if mod.Path == otiumPath {
return mod.Version
}
}
// If called from one of the examples in the otium module, then the
// otium version is in the Main module and will always be "(devel)",
// due to a Go limitation.
// Not robust if "replace" directive is used in go.mod
if info.Main.Path == otiumPath {
return info.Main.Version
}
return "findOtiumVersion-internal-error"
}