-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.go
More file actions
72 lines (61 loc) · 1.5 KB
/
generator.go
File metadata and controls
72 lines (61 loc) · 1.5 KB
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
package main
import (
_ "embed"
"fmt"
"io"
"os"
"runtime/debug"
"strings"
"text/template"
)
//go:embed stub.tmpl.bash
var stubTemplateText string
var stubTemplate = template.Must(template.New("toolstub").Parse(stubTemplateText))
type Generator struct {
ToolImport string
Exe string
ModDir string
}
func (g *Generator) GoToolImportPath() string {
value, version, found := strings.Cut(g.ToolImport, "@")
if found {
fmt.Fprintf(os.Stderr, "[WARN] toolstub does not currently support version directive, ignoring version directive %q\n", "@"+version)
}
return value
}
func (g *Generator) WriteTo(w io.Writer) (int64, error) {
// Is there a point to conform with io.WriterTo interface?
// Kinda silly since template.(*Template).Execute doesn't return the written size...
if err := stubTemplate.Execute(w, g); err != nil {
return 0, err
}
return 1, nil
}
func (t *Generator) ToolstubInfo() string {
bi, ok := debug.ReadBuildInfo()
if !ok {
return "toolstub"
}
version := bi.Main.Version
rev := ""
for _, i := range bi.Settings {
switch i.Key {
case "vcs.revision":
rev = i.Value
}
}
if rev != "" {
if version == "(devel)" {
version = rev
} else {
version = fmt.Sprintf("%s-%s", version, rev)
}
}
return fmt.Sprintf("%s@%s", bi.Path, version)
}
func (t *Generator) ToolstubCmd() string {
// This is gross and should probably be an attribute set by caller.
args := os.Args[1:len(os.Args)]
args = append([]string{"toolstub"}, args...)
return strings.Join(args, " ")
}