Skip to content

Commit 92cdd54

Browse files
committed
Add versioned helper
1 parent a16c019 commit 92cdd54

File tree

6 files changed

+49
-69
lines changed

6 files changed

+49
-69
lines changed

.goreleaser.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ before:
1414
- go mod tidy
1515
# you may remove this if you don't need go generate
1616
- go generate ./...
17+
- echo '{"version":"{{ .Version }}", "tag":"{{ .Tag }}", "commit":"{{ .Commit }}", "date":"{{ .Date }}"}' >> pkg/VERSION.json
18+
- git add pkg/VERSION.json && git commit -m "Update VERSION.json"
1719

1820
builds:
1921
- env: []
2022
goos:
2123
- linux
2224
- windows
2325
- darwin
24-
ldflags: |
25-
-s -w -X cmd.version={{.Version}} -X cmd.commit={{.Commit}} -X cmd.date={{.Date}} -X cmd.builtBy=goreleaser
26+
ldflags:
27+
- "-s -w -X 'github.com/zph/polylint/cmd.version={{.Version}}' -X 'github.com/zph/polylint/cmd.commit={{.Commit}}' -X 'github.com/zph/polylint/cmd.date={{.Date}}' -X 'github.com/zph/polylint/cmd.builtBy=goreleaser'"
2628

2729
archives:
2830
- format: tar.gz

cmd/root.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,23 @@ import (
99

1010
"github.com/spf13/cobra"
1111
"github.com/spf13/viper"
12+
pkg "github.com/zph/polylint/pkg"
1213
)
1314

1415
var (
15-
version = "unknown"
16-
commit = "none"
17-
date = "unknown"
16+
version = "v0.0.1"
17+
commit = ""
18+
date = ""
1819
)
1920

2021
var cfgFile string
2122

2223
// rootCmd represents the base command when called without any subcommands
2324
var rootCmd = &cobra.Command{
2425
Use: "polylint",
25-
Short: "A brief description of your application",
26-
Long: `A longer description that spans multiple lines and likely contains
27-
examples and usage of using your application. For example:
28-
29-
Cobra is a CLI library for Go that empowers applications.
30-
This application is a tool to generate the needed files
31-
to quickly create a Cobra application.`,
32-
// Uncomment the following line if your bare application
33-
// has an action associated with it:
34-
// Run: func(cmd *cobra.Command, args []string) { },
26+
Short: "Polylint: Extensible generalized linter",
3527
}
3628

37-
// Execute adds all child commands to the root command and sets flags appropriately.
38-
// This is called by main.main(). It only needs to happen once to the rootCmd.
3929
func Execute() {
4030
err := rootCmd.Execute()
4131
if err != nil {
@@ -44,22 +34,20 @@ func Execute() {
4434
}
4535

4636
func init() {
37+
rootCmd.Version = version
4738
cobra.OnInitialize(initConfig)
4839
cobra.OnInitialize(setVersion)
49-
50-
// Here you will define your flags and configuration settings.
51-
// Cobra supports persistent flags, which, if defined here,
52-
// will be global for your application.
40+
rootCmd.SetVersionTemplate(fmt.Sprintf("polylint\nVersion: %s\nCommit: %s\nDate: %s\n", version, commit, date))
5341

5442
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.polylint.yaml)")
55-
56-
// Cobra also supports local flags, which will only run
57-
// when this action is called directly.
58-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
5943
}
6044

6145
func setVersion() {
46+
if version == "v0.0.1" {
47+
version = pkg.LibMetadata.Version
48+
}
6249
viper.Set("binary_version", version)
50+
rootCmd.Version = version
6351
}
6452

6553
// initConfig reads in config file and ENV variables if set.
@@ -80,8 +68,7 @@ func initConfig() {
8068

8169
viper.AutomaticEnv() // read in environment variables that match
8270

83-
// If a config file is found, read it in.
84-
if err := viper.ReadInConfig(); err == nil {
85-
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
71+
if err := viper.ReadInConfig(); err != nil {
72+
fmt.Fprintln(os.Stderr, "Error reading config file:", viper.ConfigFileUsed())
8673
}
8774
}

cmd/version.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Copyright © 2024 Zander Hill <[email protected]>
33
*/
44
package main
55

6-
import "github.com/zph/polylint/cmd"
6+
import (
7+
"github.com/zph/polylint/cmd"
8+
)
79

810
func main() {
911
cmd.Execute()

pkg/VERSION.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"0.0.1-rc1-SNAPSHOT-a16c019", "tag":"v0.0.1-rc1", "commit":"a16c0192fd6b2c802fa9993c318e6db78c1ea4d6", "date":"2024-04-12T04:26:24Z"}

pkg/entry.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package polylint
2+
3+
import (
4+
"embed"
5+
"encoding/json"
6+
)
7+
8+
//go:embed VERSION.json
9+
var f embed.FS
10+
11+
type Metadata struct {
12+
Version string `json:"version"`
13+
Tag string `json:"tag"`
14+
Commit string `json:"commit"`
15+
Date string `json:"date"`
16+
}
17+
18+
var LibMetadata Metadata
19+
20+
func init() {
21+
22+
var LibMetadata Metadata
23+
24+
data, _ := f.ReadFile("VERSION.json")
25+
if err := json.Unmarshal(data, &LibMetadata); err != nil {
26+
panic(err)
27+
}
28+
}

0 commit comments

Comments
 (0)