From 151d6ec71878a7b7e54656efe5ecdb6b4107415c Mon Sep 17 00:00:00 2001 From: Enda Phelan Date: Tue, 20 Apr 2021 08:30:07 +0100 Subject: [PATCH] refactor: make values configurable through go build --- Makefile | 9 +++++++++ internal/build/build.go | 27 ++++++++++++++++++++------- pkg/cmd/kafka/create/create.go | 8 +++----- pkg/cmd/version/version.go | 20 ++++++++++++++++++-- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index fd66e78b8d..1954c23a5a 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,18 @@ .DEFAULT_GOAL := help SHELL = bash +# see internal/build.go on build configurations RHOAS_VERSION ?= "dev" +REPOSITORY_OWNER ?= "redhat-developer" +REPOSITORY_NAME ?= "app-services-cli" +TERMS_REVIEW_EVENT_CODE ?= "onlineService" +TERMS_REVIEW_SITE_CODE ?= "ocm" GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.Version=$(RHOAS_VERSION) $(GO_LDFLAGS) +GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.RepositoryOwner=$(REPOSITORY_OWNER) $(GO_LDFLAGS) +GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.RepositoryName=$(REPOSITORY_NAME) $(GO_LDFLAGS) +GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.TermsReviewEventCode=$(TERMS_REVIEW_EVENT_CODE) $(GO_LDFLAGS) +GO_LDFLAGS := -X github.com/redhat-developer/app-services-cli/internal/build.TermsReviewSiteCode=$(TERMS_REVIEW_SITE_CODE) $(GO_LDFLAGS) BUILDFLAGS := diff --git a/internal/build/build.go b/internal/build/build.go index 6c87388bcc..d7618b2162 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -10,15 +10,26 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/logging" ) -// Version is dynamically set by the toolchain or overridden by the Makefile. -var Version = "dev" -var Language = "en" +// Define public variables here which you wish to be configurable at build time +var ( + // Version is dynamically set by the toolchain or overridden by the Makefile. + Version = "dev" -// RepositoryOwner is the remote GitHub organization for the releases -var RepositoryOwner = "redhat-developer" + // Language used, can be overridden by Makefile or CI + Language = "en" -// RepositoryName is the remote GitHub repository for the releases -var RepositoryName = "app-services-cli" + // RepositoryOwner is the remote GitHub organization for the releases + RepositoryOwner = "redhat-developer" + + // RepositoryName is the remote GitHub repository for the releases + RepositoryName = "app-services-cli" + + // TermsReviewEventCode is the event code used when checking the terms review + TermsReviewEventCode = "onlineService" + + // TermsReviewSiteCode is the site code used when checking the terms review + TermsReviewSiteCode = "ocm" +) func init() { if isDevBuild() { @@ -70,6 +81,8 @@ func isDevBuild() bool { return Version == "dev" } +// check if the tag is a pre-release tag +// true it if contains anything other than MAJOR.MINOR.PATCH func isPreRelease(tag string) bool { match, _ := regexp.MatchString("^[0-9]+\\.[0-9]+\\.[0-9]+$", tag) return !match diff --git a/pkg/cmd/kafka/create/create.go b/pkg/cmd/kafka/create/create.go index 2efde9781e..e6a8ff49d5 100644 --- a/pkg/cmd/kafka/create/create.go +++ b/pkg/cmd/kafka/create/create.go @@ -13,6 +13,7 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/connection" "github.com/redhat-developer/app-services-cli/pkg/kafka" + "github.com/redhat-developer/app-services-cli/internal/build" "github.com/redhat-developer/app-services-cli/internal/localizer" kasclient "github.com/redhat-developer/app-services-cli/pkg/api/kas/client" @@ -305,14 +306,11 @@ func checkTermsAccepted(connFunc factory.ConnectionFunc) (accepted bool, redirec return false, "", err } - eventCode := "onlineService" - siteCode := "ocm" - termsReview, _, err := conn.API().AccountMgmt(). ApiAuthorizationsV1SelfTermsReviewPost(context.Background()). SelfTermsReview(amsclient.SelfTermsReview{ - EventCode: &eventCode, - SiteCode: &siteCode, + EventCode: &build.TermsReviewEventCode, + SiteCode: &build.TermsReviewSiteCode, }). Execute() if err != nil { diff --git a/pkg/cmd/version/version.go b/pkg/cmd/version/version.go index 38546ade84..d5b688d5fd 100644 --- a/pkg/cmd/version/version.go +++ b/pkg/cmd/version/version.go @@ -1,22 +1,27 @@ package version import ( + "context" "fmt" "github.com/redhat-developer/app-services-cli/internal/build" "github.com/redhat-developer/app-services-cli/internal/localizer" + "github.com/redhat-developer/app-services-cli/pkg/cmd/debug" "github.com/redhat-developer/app-services-cli/pkg/cmd/factory" "github.com/redhat-developer/app-services-cli/pkg/iostreams" + "github.com/redhat-developer/app-services-cli/pkg/logging" "github.com/spf13/cobra" ) type Options struct { - IO *iostreams.IOStreams + IO *iostreams.IOStreams + Logger func() (logging.Logger, error) } func NewVersionCmd(f *factory.Factory) *cobra.Command { opts := &Options{ - IO: f.IOStreams, + IO: f.IOStreams, + Logger: f.Logger, } cmd := &cobra.Command{ @@ -40,5 +45,16 @@ func runCmd(opts *Options) (err error) { "Version": build.Version, }, })) + + logger, err := opts.Logger() + if err != nil { + return nil + } + + // debug mode checks this for a version update also. + // so we check if is enabled first so as not to print it twice + if !debug.Enabled() { + build.CheckForUpdate(context.Background(), logger) + } return nil }