Skip to content

Commit b8795e2

Browse files
committed
Try setups for inlining the version
1 parent 1711284 commit b8795e2

File tree

9 files changed

+182
-5
lines changed

9 files changed

+182
-5
lines changed

.config/brew/Formula/.gitkeep

Whitespace-only changes.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/bin/polylint
2+
3+
dist/

.goreleaser.yaml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
# The lines below are called `modelines`. See `:help modeline`
5+
# Feel free to remove those if you don't want/need to use them.
6+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
7+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
8+
9+
version: 1
10+
11+
before:
12+
hooks:
13+
# You may remove this if you don't use go modules.
14+
- go mod tidy
15+
# you may remove this if you don't need go generate
16+
- go generate ./...
17+
18+
builds:
19+
- env: []
20+
goos:
21+
- linux
22+
- windows
23+
- darwin
24+
ldflags: |
25+
-s -w -X cmd.version={{.Version}} -X cmd.commit={{.Commit}} -X cmd.date={{.Date}} -X cmd.builtBy=goreleaser
26+
27+
archives:
28+
- format: tar.gz
29+
# this name template makes the OS and Arch compatible with the results of `uname`.
30+
name_template: >-
31+
{{ .ProjectName }}_
32+
{{- title .Os }}_
33+
{{- if eq .Arch "amd64" }}x86_64
34+
{{- else if eq .Arch "386" }}i386
35+
{{- else }}{{ .Arch }}{{ end }}
36+
{{- if .Arm }}v{{ .Arm }}{{ end }}
37+
# use zip for windows archives
38+
format_overrides:
39+
- goos: windows
40+
format: zip
41+
42+
changelog:
43+
sort: asc
44+
filters:
45+
exclude:
46+
- "^docs:"
47+
- "^test:"
48+
49+
brews:
50+
- name: polylint
51+
52+
# Git author used to commit to the repository.
53+
commit_author:
54+
name: goreleaserbot
55+
56+
57+
# The project name and current git tag are used in the format string.
58+
#
59+
# Templates: allowed
60+
commit_msg_template: "Brew formula update for {{ .ProjectName }} version {{ .Tag }}"
61+
62+
# Directory inside the repository to put the formula.
63+
directory: .config/brew/Formula
64+
65+
# Your app's homepage.
66+
homepage: "https://github.com/zph/polylint"
67+
68+
# Your app's description.
69+
#
70+
# Templates: allowed
71+
description: "Polylint: Extensible generic linter"
72+
73+
# SPDX identifier of your app's license.
74+
license: "MIT"
75+
76+
# Custom block for brew.
77+
# Can be used to specify alternate downloads for devel or head releases.
78+
custom_block: |
79+
head "https://github.com/zph/polylint.git"
80+
81+
# So you can `brew test` your formula.
82+
#
83+
# Template: allowed
84+
test: |
85+
system "#{bin}/polylint --version"
86+
87+
# Repository to push the generated files to.
88+
repository:
89+
# Repository owner.
90+
# Templates: allowed
91+
owner: zph
92+
93+
# Repository name.
94+
#
95+
# Templates: allowed
96+
name: polylint
97+
98+
# Optionally a branch can be provided.
99+
#
100+
# Default: default repository branch
101+
# Templates: allowed
102+
branch: main
103+
104+
# Sets up pull request creation instead of just pushing to the given branch.
105+
# Make sure the 'branch' property is different from base before enabling
106+
# it.
107+
#
108+
# Since: v1.17
109+
pull_request:
110+
# Whether to enable it or not.
111+
enabled: true
112+
113+
# Whether to open the PR as a draft or not.
114+
#
115+
# Since: v1.19
116+
draft: true

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ test-watch:
2222

2323
benchmark:
2424
hyperfine --ignore-failure -- "./bin/polylint --config examples/simple.yaml run ~/src/runbook"
25+
26+
build-dry-run:
27+
goreleaser release --clean --skip publish

TODO.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@
3939
- [ ] JSON reporter
4040
- [ ] Configurable logging (log levels for debugging and k/v log values)
4141
- [ ] Add `init` command to create a default named config file
42+
- [ ] Remove panics that are poor programming style
4243
- [ ] Add testing for config files... lines, path
44+
- [ ] Setup goreleaser for releases
45+
- [ ] Setup version bumper
46+
- [ ] Ensure version is embedded into cobra cli and available for --version
47+
- [ ] Add way to pipe to exec process and non-zero exit is a failing to provide builtin, js, exec mechanisms
48+
- [ ] Setup version reading https://goreleaser.com/cookbooks/using-main.version/

cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/spf13/viper"
1212
)
1313

14+
var (
15+
version = "unknown"
16+
commit = "none"
17+
date = "unknown"
18+
)
19+
1420
var cfgFile string
1521

1622
// rootCmd represents the base command when called without any subcommands
@@ -39,6 +45,7 @@ func Execute() {
3945

4046
func init() {
4147
cobra.OnInitialize(initConfig)
48+
cobra.OnInitialize(setVersion)
4249

4350
// Here you will define your flags and configuration settings.
4451
// Cobra supports persistent flags, which, if defined here,
@@ -51,6 +58,10 @@ func init() {
5158
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
5259
}
5360

61+
func setVersion() {
62+
viper.Set("binary_version", version)
63+
}
64+
5465
// initConfig reads in config file and ENV variables if set.
5566
func initConfig() {
5667
if cfgFile != "" {

cmd/version.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
)
12+
13+
// versionCmd represents the version command
14+
var versionCmd = &cobra.Command{
15+
Use: "version",
16+
Short: "A brief description of your command",
17+
Long: `A longer description that spans multiple lines and likely contains examples
18+
and usage of using your command. For example:
19+
20+
Cobra is a CLI library for Go that empowers applications.
21+
This application is a tool to generate the needed files
22+
to quickly create a Cobra application.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
fmt.Printf("polylint version %s and %s, commit %s, built at %s", version, viper.GetString("binary_version"), commit, date)
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(versionCmd)
30+
31+
// Here you will define your flags and configuration settings.
32+
33+
// Cobra supports Persistent Flags which will work for this command
34+
// and all subcommands, e.g.:
35+
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")
36+
37+
// Cobra supports local flags which will only run when this command
38+
// is called directly, e.g.:
39+
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
40+
}

main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3-
2+
Copyright © 2024 Zander Hill <[email protected]>
43
*/
54
package main
65

pkg/processors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/dop251/goja"
16+
"github.com/spf13/viper"
1617
"golang.org/x/mod/semver"
1718
"gopkg.in/yaml.v2"
1819
)
@@ -21,7 +22,6 @@ import (
2122
func extractIgnoresFromLine(line string, lineNo int, f *FileReport) error {
2223
if strings.Contains(line, "polylint disable") {
2324
chunks := strings.SplitN(line, "=", 2)
24-
// chunks[0] = `# polylint disable.*` up to the equals sign
2525
directive := strings.TrimSpace(strings.SplitN(chunks[0], "polylint", 2)[1])
2626
ignoresStr := strings.TrimSpace(chunks[1])
2727
if directive == "disable-for-file" {
@@ -108,14 +108,14 @@ func LoadConfigFile(content string) (ConfigFile, error) {
108108
}
109109

110110
if !semver.IsValid(rawConfig.Version) {
111-
fmt.Printf("Error: Config version %s is newer than binary version %s\n", rawConfig.Version, PolylintVersion)
111+
fmt.Printf("Error: Config version %s is newer than binary version %s\n", rawConfig.Version, viper.GetString("binary_version"))
112112
fmt.Println(semver.IsValid(rawConfig.Version))
113113
panic("Invalid version due to semver incompatibility")
114114
}
115115

116116
// If version file is too new for binary version
117117
if semver.Compare(rawConfig.Version, PolylintVersion) == 1 {
118-
fmt.Printf("Warning: config file version %s is newer than binary version %s\n", rawConfig.Version, PolylintVersion)
118+
fmt.Printf("Warning: config file version %s is newer than binary version %s\n", rawConfig.Version, viper.GetString("binary_version"))
119119
}
120120

121121
config.Version = rawConfig.Version

0 commit comments

Comments
 (0)