-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (60 loc) · 1.35 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
//usr/bin/go run $0 $@ ; exit
package main
import (
"errors"
"fmt"
"os"
"path"
"strings"
"github.com/xyproto/ainur"
)
const versionString = "cdetect 0.6.0"
func usage() {
fmt.Println(versionString + `
Detect the compiler version, given an executable (ELF)
Usage:
cdetect [OPTION]... [FILE]
Options:
-v, --version - version info
-h, --help - this help output
`)
}
// Check if the given filename exists.
// If it exists in $PATH, return the full path,
// else return an empty string.
func which(filename string) (string, error) {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
return filename, nil
}
for _, directory := range strings.Split(os.Getenv("PATH"), ":") {
fullPath := path.Join(directory, filename)
if _, err := os.Stat(fullPath); !os.IsNotExist(err) {
return fullPath, nil
}
}
return "", errors.New(filename + ": no such file or directory")
}
func main() {
if len(os.Args) == 2 {
switch os.Args[1] {
case "-h", "--help":
usage()
case "-v", "--version":
fmt.Println(versionString)
default:
filepath, err := which(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
compilerVersion, err := ainur.Examine(filepath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(compilerVersion)
}
} else {
usage()
}
}