This repository was archived by the owner on Jun 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectversionparser.go
More file actions
77 lines (69 loc) · 1.59 KB
/
projectversionparser.go
File metadata and controls
77 lines (69 loc) · 1.59 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
73
74
75
76
77
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"log"
"os"
"path/filepath"
"pyprojectversionparser/parsers"
)
var opts struct {
Type string `short:"t" long:"type" description:"Project type to parse" choice:"pyproject.toml" choice:"package.json"`
// Example of positional arguments
Args struct {
Path string `positional-arg-name:"path/to/project/file"`
} `positional-args:"yes"`
}
var parserMap = map[string]parsers.IParser{
"pyproject.toml": parsers.PyProjectDotToml{},
"package.json": parsers.PackageDotJson{},
}
func main() {
var err error
_, err = flags.Parse(&opts)
if err != nil {
log.Print(err)
os.Exit(-2)
}
var details *parsers.Details
if opts.Args.Path == "" && opts.Type != "" {
details, err = parserMap[opts.Type].Parse(opts.Type)
if err != nil {
os.Exit(-1)
}
} else if opts.Args.Path != "" && opts.Type != "" {
details, err = parserMap[opts.Type].Parse(opts.Args.Path)
if err != nil {
os.Exit(-1)
}
} else if opts.Args.Path != "" && opts.Type == "" {
file := filepath.Base(opts.Args.Path)
if val, ok := parserMap[file]; ok {
details, err = val.Parse(opts.Args.Path)
if err != nil {
os.Exit(-1)
}
} else {
log.Printf("Can't find parser for %s", file)
os.Exit(-1)
}
} else {
var done = false
for key, val := range parserMap {
if _, err := os.Stat(key); err == nil {
details, err = val.Parse(key)
if err != nil {
os.Exit(-1)
}
done = true
break
}
}
if !done {
log.Print("No valid project files found!")
os.Exit(-1)
}
}
fmt.Println(details.Version)
fmt.Println(details.Name)
}