From a496452e8fb3458d1aed5d185a8ae45b497750b2 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 5 Nov 2023 09:58:32 +0900 Subject: [PATCH 1/5] set optimize flag --- .goreleaser.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 820a8f4..a7e46dc 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -14,6 +14,8 @@ builds: goarch: - amd64 - arm64 + ldflags: + - -s -w archives: - format: zip From aa3a70d6843cf8ddafd4a5bff771f9c515283f4e Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 5 Nov 2023 12:35:12 +0900 Subject: [PATCH 2/5] pass version --- .goreleaser.yaml | 2 ++ checkmackerelmetric/checkmackerelmetric.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index a7e46dc..d8def86 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,6 +16,8 @@ builds: - arm64 ldflags: - -s -w + - -X main.version={{ .Version }} + - -X main.revision={{ .ShortCommit }} archives: - format: zip diff --git a/checkmackerelmetric/checkmackerelmetric.go b/checkmackerelmetric/checkmackerelmetric.go index 93e523b..3bb60c8 100644 --- a/checkmackerelmetric/checkmackerelmetric.go +++ b/checkmackerelmetric/checkmackerelmetric.go @@ -21,6 +21,13 @@ type mackerelMetricOpts struct { Critical uint `arg:"-c,--critical,required" help:"minute to be CRITICAL (MINUTE: 1-1441)" placeholder:"MINUTE"` } +var version string +var revision string + +func (mackerelMetricOpts) Version() string { + return fmt.Sprintf("Version %s (rev.%s)", version, revision) +} + func Do() { opts, err := parseArgs(os.Args[1:]) if err != nil { From 23dcf93c31fcc9018b25af65dbe1bd76b86729c4 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 5 Nov 2023 14:51:44 +0900 Subject: [PATCH 3/5] correct version option handling --- .goreleaser.yaml | 4 ++-- checkmackerelmetric/checkmackerelmetric.go | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d8def86..1a0c5e7 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,8 +16,8 @@ builds: - arm64 ldflags: - -s -w - - -X main.version={{ .Version }} - - -X main.revision={{ .ShortCommit }} + - -X github.com/mackerelio-labs/check-mackerel-metric/checkmackerelmetric.version={{ .Version }} + - -X github.com/mackerelio-labs/check-mackerel-metric/checkmackerelmetric.revision={{ .ShortCommit }} archives: - format: zip diff --git a/checkmackerelmetric/checkmackerelmetric.go b/checkmackerelmetric/checkmackerelmetric.go index 3bb60c8..a837c4d 100644 --- a/checkmackerelmetric/checkmackerelmetric.go +++ b/checkmackerelmetric/checkmackerelmetric.go @@ -25,7 +25,7 @@ var version string var revision string func (mackerelMetricOpts) Version() string { - return fmt.Sprintf("Version %s (rev.%s)", version, revision) + return fmt.Sprintf("version %s (rev %s)", version, revision) } func Do() { @@ -45,11 +45,14 @@ func parseArgs(args []string) (*mackerelMetricOpts, error) { p, _ := arg.NewParser(arg.Config{}, &mo) err := p.Parse(args) - if err == arg.ErrHelp { + switch { + case err == arg.ErrHelp: p.WriteHelp(os.Stdout) os.Exit(0) - } - if err != nil { + case err == arg.ErrVersion: + fmt.Println(mo.Version()) + os.Exit(0) + case err != nil: return &mo, err } From 50efb1e20e828850dfd1fb154869b8f705b9ad5e Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Sun, 5 Nov 2023 15:19:58 +0900 Subject: [PATCH 4/5] update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3df6b1b..47067af 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ command = ["check-mackerel-metric", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", " - `--warning MINUTE, -w MINUTE`: minute to be WARNING (MINUTE: 1-1441) - `--critical MINUTE, -c MINUTE`: minute to be CRITICAL (MINUTE: 1-1441) - `--help, -h`: display the help and exit +- `--version`: display version and exit - `--host` is for host metrics and `--service` is for service metrics. Choose one of these. - HOST_ID is displayed at the top of the Mackerel host screen, like `4Hkc5RWzXXX`. - METRIC_NAME can be looked up with `mkr metric-names -H HOST_ID`. @@ -94,6 +95,7 @@ command = ["check-mackerel-metric", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", " - `--warning MINUTE, -w MINUTE`: 指定の分数内にメトリックがなければWARNING(MINUTEは1〜1441) - `--critical MINUTE, -c MINUTE`: 指定の分数内にメトリックがなければCRITICAL(MINUTEは1〜1441) - `--help, -h`: ヘルプの表示と終了 +- `--version`: バージョンの表示と終了 - `--host`はホストメトリック用、`--service`はサービスメトリック用です。どちらか1つを選んでください。 - HOST_ID (ホストID) はMackerelのホスト画面の上部に `4Hkc5RWzXXX` のように表示されています。 - METRIC_NAME (メトリック名) は `mkr metric-names -H HOST_ID` で調べることができます。 From fdbb0c0eeacb8930681d22841e8732adef0256b6 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Mon, 6 Nov 2023 10:03:46 +0900 Subject: [PATCH 5/5] use Env value to shortcut library path --- .goreleaser.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1a0c5e7..c6c068b 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -7,6 +7,7 @@ before: builds: - env: - CGO_ENABLED=0 + - PACKAGE_PATH=github.com/mackerelio-labs/check-mackerel-metric/checkmackerelmetric goos: - linux - windows @@ -16,8 +17,8 @@ builds: - arm64 ldflags: - -s -w - - -X github.com/mackerelio-labs/check-mackerel-metric/checkmackerelmetric.version={{ .Version }} - - -X github.com/mackerelio-labs/check-mackerel-metric/checkmackerelmetric.revision={{ .ShortCommit }} + - -X {{ .Env.PACKAGE_PATH }}.version={{ .Version }} + - -X {{ .Env.PACKAGE_PATH }}.revision={{ .ShortCommit }} archives: - format: zip