Skip to content

Commit 318129c

Browse files
committed
corrected usage of runtime/debug for version
1 parent daab285 commit 318129c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

ssa_analyzer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package pal
1616

1717
import (
1818
"flag"
19+
"fmt"
20+
"os"
1921
"reflect"
2022

2123
"github.com/go-air/pal/indexing"
@@ -26,6 +28,7 @@ import (
2628
)
2729

2830
var flagSet = flag.NewFlagSet("pal", flag.ExitOnError)
31+
var palVersion = flagSet.Bool("V", false, "print out pal version")
2932

3033
type resultType int
3134

@@ -51,6 +54,15 @@ func SSAAnalyzer() *analysis.Analyzer {
5154
}
5255

5356
func run(pass *analysis.Pass) (interface{}, error) {
57+
//flagSet.Parse(pass.Analyzer.Flags.Args())
58+
if *palVersion {
59+
v, e := Version()
60+
if e != nil {
61+
return nil, e
62+
}
63+
fmt.Printf("%s\n", v)
64+
os.Exit(0)
65+
}
5466
pal, err := ssa2pal.New(pass, indexing.ConstVals())
5567
if err != nil {
5668
return nil, err

version.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@ package pal
1717
import (
1818
"fmt"
1919
"runtime/debug"
20+
"strings"
2021
)
2122

23+
const modPath = "github.com/go-air/pal"
24+
25+
func tryMod(mod *debug.Module) string {
26+
if strings.HasPrefix(mod.Path, modPath) {
27+
return fmt.Sprintf("%s %s %s",
28+
mod.Path, mod.Version, mod.Sum)
29+
}
30+
return ""
31+
}
32+
2233
func Version() (string, error) {
2334
// something around this will be needed once we put in
2435
// place per-package caching.
2536
bi, ok := debug.ReadBuildInfo()
2637
if !ok {
2738
return "", fmt.Errorf("couldn't read build info (are you running go test?)")
2839
}
29-
return fmt.Sprintf("%s %s %s\n%v\n", bi.Main.Path, bi.Main.Version, bi.Main.Sum, bi), nil
40+
s := tryMod(&bi.Main)
41+
if s != "" {
42+
return s, nil
43+
}
44+
for _, dep := range bi.Deps {
45+
s = tryMod(dep)
46+
if s != "" {
47+
return s, nil
48+
}
49+
}
50+
return "", fmt.Errorf("could not find %s in debug modules", modPath)
3051
}

0 commit comments

Comments
 (0)