Skip to content

Commit

Permalink
oras version (#69)
Browse files Browse the repository at this point in the history
support version with makefile, dockerfile, and goreleaser updated
  • Loading branch information
shizhMSFT committed Apr 17, 2019
1 parent bc1ed35 commit bf2ff5d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ builds:
- windows
goarch:
- amd64
ldflags:
# one-line ldflags to bypass the goreleaser bugs
# the git tree state is guaranteed to be clean by goreleaser
- -w -X github.com/deislabs/oras/internal/version.Version={{.Version}} -X github.com/deislabs/oras/internal/version.GitCommit={{.FullCommit}} -X github.com/deislabs/oras/internal/version.BuildMetadata= -X github.com/deislabs/oras/internal/version.GitTreeState=clean

archive:
format: tar.gz
Expand Down
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM golang:1.11-alpine as builder
ADD . /go/src/github.com/deislabs/oras
WORKDIR /go/src/github.com/deislabs/oras
RUN go install github.com/deislabs/oras/cmd/oras
RUN apk add git make
ENV ORASPKG /go/src/github.com/deislabs/oras
ADD . ${ORASPKG}
WORKDIR ${ORASPKG}
RUN make build-linux
RUN mv ${ORASPKG}/bin/linux/amd64/oras /go/bin/oras

FROM alpine
LABEL maintainer="shizh@microsoft.com"
Expand Down
26 changes: 20 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
CLI_EXE = oras
CLI_PKG = github.com/deislabs/oras/cmd/oras
DEP = $(GOPATH)/bin/dep
PROJECT_PKG = github.com/deislabs/oras
CLI_EXE = oras
CLI_PKG = $(PROJECT_PKG)/cmd/oras
DEP = $(GOPATH)/bin/dep
GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")

LDFLAGS = -w
ifdef VERSION
LDFLAGS += -X $(PROJECT_PKG)/internal/version.BuildMetadata=$(VERSION)
endif
ifneq ($(GIT_TAG),)
LDFLAGS += -X $(PROJECT_PKG)/internal/version.BuildMetadata=
endif
LDFLAGS += -X $(PROJECT_PKG)/internal/version.GitCommit=${GIT_COMMIT}
LDFLAGS += -X $(PROJECT_PKG)/internal/version.GitTreeState=${GIT_DIRTY}

.PHONY: test
test: vendor
Expand All @@ -19,17 +33,17 @@ build: build-linux build-mac build-windows vendor

.PHONY: build-linux
build-linux: vendor
GOARCH=amd64 CGO_ENABLED=0 GOOS=linux go build -v --ldflags="-w" \
GOARCH=amd64 CGO_ENABLED=0 GOOS=linux go build -v --ldflags="$(LDFLAGS)" \
-o bin/linux/amd64/$(CLI_EXE) $(CLI_PKG)

.PHONY: build-mac
build-mac: vendor
GOARCH=amd64 CGO_ENABLED=0 GOOS=darwin go build -v --ldflags="-w" \
GOARCH=amd64 CGO_ENABLED=0 GOOS=darwin go build -v --ldflags="$(LDFLAGS)" \
-o bin/darwin/amd64/$(CLI_EXE) $(CLI_PKG)

.PHONY: build-windows
build-windows: vendor
GOARCH=amd64 CGO_ENABLED=0 GOOS=windows go build -v --ldflags="-w" \
GOARCH=amd64 CGO_ENABLED=0 GOOS=windows go build -v --ldflags="$(LDFLAGS)" \
-o bin/windows/amd64/$(CLI_EXE).exe $(CLI_PKG)

$(DEP):
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
Use: "oras [command]",
SilenceUsage: true,
}
cmd.AddCommand(pullCmd(), pushCmd(), loginCmd(), logoutCmd())
cmd.AddCommand(pullCmd(), pushCmd(), loginCmd(), logoutCmd(), versionCmd())
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
54 changes: 54 additions & 0 deletions cmd/oras/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"runtime"
"strings"

"github.com/deislabs/oras/internal/version"

"github.com/spf13/cobra"
)

func versionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Show the oras version information",
Long: `Show the oras version information
Example - print version:
oras version
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runVersion()
},
}

return cmd
}

func runVersion() error {
items := [][]string{
{"Version", version.GetVersion()},
{"Go version", runtime.Version()},
}
if version.GitCommit != "" {
items = append(items, []string{"Git commit", version.GitCommit})
}
if version.GitTreeState != "" {
items = append(items, []string{"Git tree state", version.GitTreeState})
}

size := 0
for _, item := range items {
if length := len(item[0]); length > size {
size = length
}
}
for _, item := range items {
fmt.Println(item[0] + ": " + strings.Repeat(" ", size-len(item[0])) + item[1])
}

return nil
}
20 changes: 20 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package version

var (
// Version is the current version of the oras.
Version = "0.4.0"
// BuildMetadata is the extra build time data
BuildMetadata = "unreleased"
// GitCommit is the git sha1
GitCommit = ""
// GitTreeState is the state of the git tree
GitTreeState = ""
)

// GetVersion returns the semver string of the version
func GetVersion() string {
if BuildMetadata == "" {
return Version
}
return Version + "+" + BuildMetadata
}

0 comments on commit bf2ff5d

Please sign in to comment.