| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
"debug/buildinfo" |
| 6 |
"encoding/hex" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"sync" |
| 11 |
) |
| 12 |
|
| 13 |
// instrumentorIdentity identifies this build of the wrapper, for the tool ID |
| 14 |
// reported through -V=full. |
| 15 |
var instrumentorIdentity = sync.OnceValues(func() (string, error) { |
| 16 |
self, err := os.Executable() |
| 17 |
if err != nil { |
| 18 |
return "", fmt.Errorf("locating this executable in order to identify it: %w", err) |
| 19 |
} |
| 20 |
|
| 21 |
// A binary installed at a version (go install [email protected]) carries it, and it |
| 22 |
// is stable across machines, so prefer it when it is there. |
| 23 |
if info, err := buildinfo.ReadFile(self); err == nil { |
| 24 |
if version := info.Main.Version; version != "" && version != "(devel)" { |
| 25 |
return "version=" + version, nil |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// If an artifact without a version is being used, use the SHA256 of the executable instead |
| 30 |
file, err := os.Open(self) |
| 31 |
if err != nil { |
| 32 |
return "", fmt.Errorf("reading this executable in order to identify it: %w", err) |
| 33 |
} |
| 34 |
defer file.Close() |
| 35 |
|
| 36 |
digest := sha256.New() |
| 37 |
if _, err := io.Copy(digest, file); err != nil { |
| 38 |
return "", fmt.Errorf("hashing this executable in order to identify it: %w", err) |
| 39 |
} |
| 40 |
return "content=" + hex.EncodeToString(digest.Sum(nil))[:16], nil |
| 41 |
}) |