This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (84 loc) · 2.92 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
/*
* Copyright 2022 Kulkarni, Ashish <[email protected]>
* Author: Ashish Kulkarni
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//go:generate go run main.go
// git-describe dynamically generates version information
// needed at the time of build. This information should be injected during build time.
// go run github.com/thatInfrastructureGuy/git-describe@latest
// See https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
package main
import (
"flag"
"io"
"log"
"os"
"path/filepath"
"text/template"
"time"
"github.com/thatInfrastructureGuy/git-describe/helper"
)
const (
defaultFilename = "version/const.go"
defaultPackageName = "version"
defaultVariableName = "Version"
defaultCommand = "git describe --dirty --abbrev=7"
)
func main() {
fileName, packageName, variableName, command := parseFlags()
f, err := create(fileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
generateVersionFile(f, packageName, variableName, command)
}
func generateVersionFile(w io.Writer, packageName, variableName, command string) {
packageTemplate := template.Must(template.New("").Parse(`// THIS IS GENERATED CODE; DO NOT EDIT.
// Generated at {{ .Timestamp }}
package {{.PackageName}}
// {{.VariableName}} is generated by running 'git describe --abbrev=7 --dirty'
const {{.VariableName}} = "{{ .Version }}"
`))
packageTemplate.Execute(w, struct {
Timestamp time.Time
PackageName string
VariableName string
Version string
}{
Timestamp: time.Now(),
PackageName: packageName,
VariableName: variableName,
Version: helper.ExecCommand(command),
})
}
func create(filename string) (*os.File, error) {
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
return nil, err
}
f, err := os.Create(filename)
if err != nil {
return nil, err
}
return f, nil
}
func parseFlags() (string, string, string, string) {
filePtr := flag.String("filepath", defaultFilename, "Filepath where contents should get generated. It should be a .go file.")
packageNamePtr := flag.String("package", defaultPackageName, "Name of the package where file should be generated.")
variablePtr := flag.String("variable", defaultVariableName, "Variable name used to store version.")
CommandPtr := flag.String("command", defaultCommand, "Command to run. Flag format: --key=value.")
flag.Parse()
return *filePtr, *packageNamePtr, *variablePtr, *CommandPtr
}