-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (106 loc) · 3.25 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2022 Jonas Kwiedor. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// Package main is used for reading and checking the tags also the
// different statements are called over it and the return value is passed on.
package main
import (
"flag"
"github.com/gowizzard/compver/v5/build_information"
"github.com/gowizzard/compver/v5/output"
"github.com/gowizzard/compver/v5/statement"
"log"
"os"
"reflect"
)
// version, compare, core is to save the boolean for the statements.
// version1, version2 is to save the version numbers from the flags.
// visit is to store the specified flags as a positive value.
// action checks if it is a GitHub action.
// logger is to write the log data to command line.
// data is to store the data from the GitHub output file.
var (
version, trim, compare, core bool
version1, version2, prefix, block string
visit = make(map[string]bool)
action = os.Getenv("GITHUB_ACTIONS") == "true"
logger = log.New(os.Stderr, "", 0)
data []byte
)
// init is to parse the versions from the flags and check all visited flags.
func init() {
flag.BoolVar(&version, "version", false, "Get the current version")
flag.BoolVar(&trim, "trim", false, "To trim the version prefix")
flag.BoolVar(&compare, "compare", false, "Set the statement to compare the version numbers")
flag.BoolVar(&core, "core", false, "Set the statement to get a block from version core")
flag.StringVar(&version1, "version1", "1.1.0", "Set the first version number")
flag.StringVar(&version2, "version2", "1.0.5", "Set the second version number")
flag.StringVar(&prefix, "prefix", "v", "Set the prefix to trim from the version number")
flag.StringVar(&block, "block", "major", "Set the desired block")
flag.Parse()
flag.Visit(func(f *flag.Flag) {
visit[f.Name] = true
})
if action {
var err error
data, err = output.Read()
if err != nil {
logger.Fatal(err)
}
}
}
// main is to check the flags from the command line interface
// and execute the statements or return the no statement message.
func main() {
if version {
logger.Printf("version: %s", build_information.Version)
return
}
if trim && visit["prefix"] {
if visit["version1"] {
version1 = statement.Prefix(version1, prefix)
}
if visit["version2"] {
version2 = statement.Prefix(version2, prefix)
}
}
if compare && visit["version1"] && visit["version2"] {
result, err := statement.Compare(version1, version2)
if err != nil {
logger.Fatal(err)
}
switch {
case action:
err = output.Write("compare_result", result, data)
if err != nil {
logger.Fatal(err)
}
default:
logger.Print(result)
}
return
}
if core && visit["version1"] {
result, err := statement.Core(version1, block)
if err != nil {
logger.Fatal(err)
}
switch result.(type) {
case string:
if reflect.ValueOf(result).Len() == 0 {
result = "not found"
}
}
switch {
case action:
err = output.Write("core_result", result, data)
if err != nil {
logger.Fatal(err)
}
default:
logger.Print(result)
}
return
}
logger.Fatal("no statement flags were specified")
}