-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (62 loc) · 1.34 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"github.com/Sirupsen/logrus"
)
var (
isFile bool
)
func init() {
// parse flags
flag.BoolVar(&isFile, "r", false, "read from file")
flag.Parse()
if len(flag.Args()) < 1 {
usageAndExit(1, "must pass a version string or version file\nex. %s v0.1.0", os.Args[0])
}
}
func main() {
version := flag.Arg(0)
if isFile {
b, err := ioutil.ReadFile(version) // just pass the file name
if err != nil {
logrus.Fatal(err)
}
version = string(b)
re := regexp.MustCompile(`\r?\n`)
version = re.ReplaceAllString(version, "")
}
if len(flag.Args()) > 1 {
for _, file := range flag.Args()[1:] {
writeOut(version, file)
}
return
}
fmt.Printf("%s", version)
}
func writeOut(version, file string) {
re := regexp.MustCompile(`v?\d*\.\d*\.\d*`)
input, err := ioutil.ReadFile(file)
if err != nil {
logrus.Fatal(err)
}
output := re.ReplaceAllString(string(input), version)
err = ioutil.WriteFile(file, []byte(output), 0666)
if err != nil {
log.Fatalln(err)
}
}
func usageAndExit(exitCode int, message string, args ...interface{}) {
if message != "" {
fmt.Fprintf(os.Stderr, message, args...)
fmt.Fprint(os.Stderr, "\n\n")
}
fmt.Println("vupdate <version|version file> <file to update>...")
flag.Usage()
fmt.Fprintln(os.Stderr, "")
os.Exit(exitCode)
}