diff --git a/Makefile b/Makefile index d9214a2..c6e9863 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,13 @@ +TIMESTAMP = $(shell date +"%Y%m%d%H%M%S") +BRANCH = $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null) +COMMIT = $(shell git rev-parse HEAD) +VERSION = $(shell git describe --tags) +LDFLAGSPREFIX = github.com/AliyunContainerService/image-syncer/pkg/utils +LDFLAGS = "-X $(LDFLAGSPREFIX).Version=$(VERSION) -X $(LDFLAGSPREFIX).Branch=$(BRANCH) -X $(LDFLAGSPREFIX).Commit=$(COMMIT) -X $(LDFLAGSPREFIX).Timestamp=$(TIMESTAMP)" .PHONY: cmd clean cmd: $(wildcard ./pkg/client/*.go ./pkg/sync/*.go ./pkg/tools/*.go ./cmd/*.go ./*.go) - go build -o image-syncer ./main.go + go build -ldflags $(LDFLAGS) -o image-syncer ./main.go clean: rm image-syncer \ No newline at end of file diff --git a/cmd/image-syncer.go b/cmd/image-syncer.go index 466f976..4e309a1 100644 --- a/cmd/image-syncer.go +++ b/cmd/image-syncer.go @@ -17,7 +17,7 @@ var ( osFilterList, archFilterList []string - forceUpdate bool + forceUpdate, version bool ) // RootCmd describes "image-syncer" command @@ -31,6 +31,14 @@ var RootCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceErrors = true + if version { + fmt.Println("app-version: ", utils.Version) + fmt.Println("git-branch: ", utils.Branch) + fmt.Println("git-commit: ", utils.Commit) + fmt.Println("build-timestamp: ", utils.Timestamp) + return nil + } + // work starts here client, err := client.NewSyncClient(configFile, authFile, imagesFile, logPath, successImagesFile, outputImagesFormat, procNum, retries, utils.RemoveEmptyItems(osFilterList), utils.RemoveEmptyItems(archFilterList), forceUpdate) @@ -55,6 +63,7 @@ func init() { RootCmd.PersistentFlags().BoolVar(&forceUpdate, "force", false, "force update manifest whether the destination manifest exists") RootCmd.PersistentFlags().StringVar(&successImagesFile, "output-success-images", "", "output success images in a new file") RootCmd.PersistentFlags().StringVar(&outputImagesFormat, "output-images-format", "yaml", "success images output format, json or yaml") + RootCmd.PersistentFlags().BoolVar(&version, "version", false, "print app version") } // Execute executes the RootCmd diff --git a/pkg/utils/version.go b/pkg/utils/version.go new file mode 100644 index 0000000..8934555 --- /dev/null +++ b/pkg/utils/version.go @@ -0,0 +1,8 @@ +package utils + +var ( + Version = "v0.1.0" + Branch = "" + Commit = "" + Timestamp = "" +)