Skip to content

Commit

Permalink
refactor: make values configurable through go build
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda Phelan committed Apr 20, 2021
1 parent db14429 commit 151d6ec
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 :=

Expand Down
27 changes: 20 additions & 7 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
}

0 comments on commit 151d6ec

Please sign in to comment.