Skip to content

Commit

Permalink
Merge pull request #304 from redhatrises/goreleaser_updates
Browse files Browse the repository at this point in the history
goreleaser updates
  • Loading branch information
shawndwells committed Jun 20, 2018
2 parents c47ef9a + ce8b798 commit 721c996
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/compliance-masonry
/masonry
*.swp
*.out
/build
23 changes: 17 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
builds:
- binary: masonry
main: ./cmd/masonry/masonry.go
ldflags: -s -w -X github.com/opencontrol/compliance-masonry/version.Version={{.Version}} -X github.com/opencontrol/compliance-masonry/version.Commit={{.Commit}} -X github.com/opencontrol/compliance-masonry/version.Date={{.Date}}
env:
- CGO_ENABLED=0
goos:
- windows
- darwin
- linux
goarch:
- amd64
- arm64

- binary: compliance-masonry
main: ./masonry-go.go ./diff.go
ldflags: -s -w -X github.com/opencontrol/compliance-masonry/version.Version={{.Version}}
main: ./cmd/compliance-masonry/compliance-masonry.go
ldflags: -s -w -X github.com/opencontrol/compliance-masonry/version.Version={{.Version}} -X github.com/opencontrol/compliance-masonry/version.Commit={{.Commit}} -X github.com/opencontrol/compliance-masonry/version.Date={{.Date}}
env:
- CGO_ENABLED=0
goos:
- windows
- darwin
- linux
goarch:
- 386
- amd64
- arm
- arm64

nfpm:
Expand All @@ -22,10 +33,10 @@ nfpm:
- rpm

brew:
name: oscalkit
name: compliance-masonry
github:
owner: opencontrol
name: homebrew-oscalkit
name: homebrew-compliance-masonry
commit_author:
name: OpenControl
email: [email protected]
Expand Down
17 changes: 11 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ BASHINSTALLDIR=${PREFIX}/share/bash-completion/completions
SELINUXOPT ?= $(shell selinuxenabled 2>/dev/null && echo -Z)

BUILD_DIR ?= ./build
BUILD_INFO := $(shell date +%s)
BUILD_INFO := $(shell date --iso-8601=s --utc)
BUILD_PLATFORMS = "windows/amd64" "windows/386" "darwin/amd64" "darwin/386" "linux/386" "linux/amd64" "linux/arm" "linux/arm64"

VERSION := $(shell grep -o "[0-9]*\.[0-9]*\.[0-9]*" version/version.go)
VERSION := $(shell git for-each-ref --format="%(refname:short)" --sort=-authordate --count=1 refs/tags | cut -d"v" -f2)
SHORTCOMMIT := $(shell git rev-parse --short HEAD)

# If GOPATH not specified, use one in the local directory
ifeq ($(GOPATH),)
Expand All @@ -35,11 +36,15 @@ ifeq ($(DEBUG), 1)
DEBUGFLAGS ?= -gcflags="-N -l"
endif

LDFLAGS ?= -ldflags="-s -w -X github.com/opencontrol/compliance-masonry/version.Version=$(VERSION) \
-X github.com/opencontrol/compliance-masonry/version.Commit=$(SHORTCOMMIT) \
-X github.com/opencontrol/compliance-masonry/version.Date=$(BUILD_INFO)"

build:
$(GO) build $(DEBUGFLAGS) \
$(GO) build $(DEBUGFLAGS) $(LDFLAGS)\
-o $(BUILD_DIR)/$(PROGNAME) \
cmd/masonry/masonry.go
$(GO) build $(DEBUGFLAGS) \
$(GO) build $(DEBUGFLAGS) $(LDFLAGS)\
-o $(BUILD_DIR)/compliance-masonry \
cmd/compliance-masonry/compliance-masonry.go

Expand All @@ -54,8 +59,8 @@ platforms:
[ $$GOOS = "windows" ] && output_name="$$output_name.exe"; \
[ $$GOOS = "windows" ] && legacy_name="$$legacy_name.exe"; \
echo "Building $(PROGNAME) version $(VERSION) for $$GOOS on $$GOARCH"; \
GOOS=$$GOOS GOARCH=$$GOARCH $(GO) build -o $$output_name cmd/masonry/masonry.go; \
GOOS=$$GOOS GOARCH=$$GOARCH $(GO) build -o $$legacy_name cmd/compliance-masonry/compliance-masonry.go; \
GOOS=$$GOOS GOARCH=$$GOARCH $(GO) build $(LDFLAGS) -o $$output_name cmd/masonry/masonry.go; \
GOOS=$$GOOS GOARCH=$$GOARCH $(GO) build $(LDFLAGS) -o $$legacy_name cmd/compliance-masonry/compliance-masonry.go; \
[ -d $(BUILD_DIR)/$$GOOS-$$GOARCH/ ] && cp {LICENSE.md,README.md} $(BUILD_DIR)/$$GOOS-$$GOARCH/; \
done

Expand Down
File renamed without changes.
11 changes: 8 additions & 3 deletions cmd/masonry/masonry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ import (

var usage = `
Usage:
masonry [command]
masonry [global-options] COMMAND [command-options]
Available Commands:
Commands:
diff Compliance Diff Gap Analysis
docs Create compliance documentation
export Export to consolidated output
get Install compliance dependencies
help Help about any command
version Display version
Flags:
Options:
-h, --help help for masonry
--verbose Run with verbosity
-v, --version Print the version
See "masonry help <TOPIC>" for more information on a specific topic.
See "masonry <COMMAND> --help" for more information about a command.
`

var _ = Describe("Masonry CLI", func() {
Expand Down
24 changes: 24 additions & 0 deletions pkg/cli/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright (C) 2018 OpenControl Contributors. See LICENSE.md for license.
*/

package version

import (
"io"

"github.com/opencontrol/compliance-masonry/version"
"github.com/spf13/cobra"
)

// NewCmdVersion prints out the version
func NewCmdVersion(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Display version",
Run: func(cmd *cobra.Command, args []string) {
version.PrintVersion()
},
}
return cmd
}
30 changes: 27 additions & 3 deletions pkg/cmd/masonry/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package masonry

import (
"fmt"
"io"
"io/ioutil"
"log"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/opencontrol/compliance-masonry/pkg/cli/docs"
"github.com/opencontrol/compliance-masonry/pkg/cli/export"
"github.com/opencontrol/compliance-masonry/pkg/cli/get"
cliversion "github.com/opencontrol/compliance-masonry/pkg/cli/version"
"github.com/opencontrol/compliance-masonry/version"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -43,8 +43,13 @@ the OpenControl Schema`,
err := RunGlobalFlags(out, cmd)
clierrors.CheckError(err)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

cmds.SetUsageTemplate(usageTemplate)
cmds.ResetFlags()
// Global Options
cmds.PersistentFlags().BoolVarP(&Verbose, "verbose", "", false, "Run with verbosity")
Expand All @@ -55,6 +60,9 @@ the OpenControl Schema`,
cmds.AddCommand(docs.NewCmdDocs(out))
cmds.AddCommand(export.NewCmdExport(out))
cmds.AddCommand(get.NewCmdGet(out))
cmds.AddCommand(cliversion.NewCmdVersion(out))

disableFlagsInUseLine(cmds)

return cmds
}
Expand All @@ -70,10 +78,26 @@ func RunGlobalFlags(out io.Writer, cmd *cobra.Command) error {
}

if flagVersion {
fmt.Printf("compliance-masonry: version %s\n", version.Version)
os.Exit(0)
version.PrintVersion()
}

return nil

}

// disableFlagsInUseLine do not add a `[flags]` to the end of the usage line.
func disableFlagsInUseLine(cmd *cobra.Command) {
visitAll(cmd, func(cmds *cobra.Command) {
cmds.DisableFlagsInUseLine = true
})
}

// visitAll will traverse all commands from the root.
// This is different from the VisitAll of cobra.Command where only parents
// are checked.
func visitAll(cmds *cobra.Command, fn func(*cobra.Command)) {
for _, cmd := range cmds.Commands() {
visitAll(cmd, fn)
}
fn(cmds)
}
30 changes: 30 additions & 0 deletions pkg/cmd/masonry/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (C) 2018 OpenControl Contributors. See LICENSE.md for license.
*/

package masonry

var usageTemplate = `{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}Usage:
{{.CommandPath}} [global-options]{{if .HasAvailableSubCommands}} COMMAND{{end}}{{if .HasAvailableFlags}} [command-options]{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Options:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableLocalFlags}}
Options:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}
{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
See "{{.CommandPath}} help <TOPIC>" for more information on a specific topic.
See "{{.CommandPath}} <COMMAND> --help" for more information about a command.
{{end}}
`
1 change: 1 addition & 0 deletions pkg/tests/test_masonry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func Masonry(args ...string) *gexec.Session {
_ = buffer.Flush()
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
gexec.CleanupBuildArtifacts()
return session
}
18 changes: 17 additions & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,21 @@

package version

import (
"fmt"
"os"
)

// Version is the version of the build.
const Version = "1.1.3"
// Build details
var (
Version string
Commit string
Date string
)

// PrintVersion returns the version for the command version and --version flag
func PrintVersion() {
fmt.Printf("masonry version: %s, build: %s, date: %s\n", Version, Commit, Date)
os.Exit(0)
}

0 comments on commit 721c996

Please sign in to comment.