From e8b7dedbe3ad8668f32e99d86799c4897c488edf Mon Sep 17 00:00:00 2001 From: cezkuj Date: Mon, 15 Apr 2019 21:07:42 +0000 Subject: [PATCH 1/2] Resolving #21 issue. Adding ko version and simple release script --- README.md | 3 +++ cmd/ko/commands.go | 8 ++++++++ cmd/ko/version.go | 41 +++++++++++++++++++++++++++++++++++++++++ hack/release.sh | 30 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 cmd/ko/version.go create mode 100755 hack/release.sh diff --git a/README.md b/README.md index 6909ab3f22..9f6a829df6 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,9 @@ This flag is still experimental, and feedback is very welcome. `ko delete` simply passes through to `kubectl delete`. It is exposed purely out of convenience for cleaning up resources created through `ko apply`. +### `ko version` + +`ko version` prints version of ko. For not released binaries it will print hash of latest commit in current git tree. ## With `minikube` diff --git a/cmd/ko/commands.go b/cmd/ko/commands.go index c04a1dc899..18239733c5 100644 --- a/cmd/ko/commands.go +++ b/cmd/ko/commands.go @@ -64,6 +64,14 @@ func addKubeCommands(topLevel *cobra.Command) { }, }) + topLevel.AddCommand(&cobra.Command{ + Use: "version", + Short: `Print ko version.`, + Run: func(cmd *cobra.Command, args []string) { + version() + }, + }) + koApplyFlags := []string{} lo := &LocalOptions{} bo := &BinaryOptions{} diff --git a/cmd/ko/version.go b/cmd/ko/version.go new file mode 100644 index 0000000000..e5a120d2b5 --- /dev/null +++ b/cmd/ko/version.go @@ -0,0 +1,41 @@ +// Copyright 2019 Google LLC All Rights Reserved. +// +// 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. + +package main + +import ( + "fmt" + "log" + "os/exec" +) + +// provided by govvv in compile-time +var Version string + +func version() { + if Version == "" { + hash, err := gitRevParseHead() + if err != nil { + log.Fatalf("error during command execution: %v", err) + } + fmt.Printf("version: %v", string(hash)) + } else { + fmt.Printf("version: %v\n", Version) + } +} + +func gitRevParseHead() ([]byte, error) { + cmd := exec.Command("git", "rev-parse", "HEAD") + return cmd.Output() +} diff --git a/hack/release.sh b/hack/release.sh new file mode 100755 index 0000000000..bcf350e8ea --- /dev/null +++ b/hack/release.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# Copyright 2019 Google LLC All Rights Reserved. +# +# 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. + +# Usage: +# hack/release.sh $VERSION +# +# Example: +# hack/release.sh v0.2 + +VERSION=$1 + +KO_ROOT="$(cd "$(dirname "$0")" && pwd)/.." + +go get github.com/ahmetb/govvv +govvv build -o $KO_ROOT/build/ko $KO_ROOT/cmd/ko -version $VERSION +git tag $VERSION +git push origin $VERSION From 672e47860281a7f7be0dbda50692924c6f3abd74 Mon Sep 17 00:00:00 2001 From: cezkuj Date: Fri, 17 May 2019 05:53:06 +0000 Subject: [PATCH 2/2] Resolving #21 issue. Adding git-dir flag to always execute inside ko's directory in GOPATH --- pkg/commands/version.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/commands/version.go b/pkg/commands/version.go index b5f21bbe54..e07fe7e2b0 100644 --- a/pkg/commands/version.go +++ b/pkg/commands/version.go @@ -17,6 +17,7 @@ package commands import ( "fmt" "log" + "os" "os/exec" "strings" @@ -39,7 +40,8 @@ func addVersion(topLevel *cobra.Command) { func version() { if Version == "" { - hash, err := exec.Command("git", "rev-parse", "HEAD").Output() + gitDir := fmt.Sprintf("--git-dir=%v/src/github.com/google/ko/.git", os.Getenv("GOPATH")) + hash, err := exec.Command("git", gitDir, "rev-parse", "HEAD").Output() if err != nil { log.Fatalf("error during command execution: %v", err) }