Skip to content

Commit

Permalink
add version subcommand to both binaries
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <josh@dolit.ski>
  • Loading branch information
jdolitsky committed Jul 29, 2021
1 parent 0dfbd6d commit 714fcda
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ builds:
flags:
- -trimpath
ldflags:
- "-s -w"
- "-s -w -X main.Version={{.Version}}"
goarch:
- amd64
- arm
Expand All @@ -29,7 +29,7 @@ builds:
flags:
- -trimpath
ldflags:
- "-s -w"
- "-s -w -X main.Version={{.Version}}"
goarch:
- amd64
- arm
Expand Down
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
SHELL = /usr/bin/env bash
GIT_SHA = $(shell git rev-parse --short HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)

VERSION = ${GIT_TAG}
ifeq ($(VERSION),)
VERSION = ${GIT_SHA}-devel
endif

.PHONY: fetch-helpers
fetch-helpers:
for i in $(shell find mappings -name '*.yml' -exec basename {} .yml \;); do \
Expand All @@ -14,18 +23,22 @@ vendor:

.PHONY: build-magic
build-magic:
go build -o bin/docker-credential-magic \
go build -ldflags="-X main.Version=$(VERSION)" \
-o bin/docker-credential-magic \
.../cmd/docker-credential-magic

.PHONY: build-magic-embedded
build-magic-embedded:
GOOS=linux GOARCH=amd64 \
go build -o pkg/magician/credential-helpers/docker-credential-magic \
go build -ldflags="-X main.Version=$(VERSION)" \
-o pkg/magician/credential-helpers/docker-credential-magic \
.../cmd/docker-credential-magic

.PHONY: build-magician
build-magician:
go build -o bin/docker-credential-magician .../cmd/docker-credential-magician
go build -ldflags="-X main.Version=$(VERSION)" \
-o bin/docker-credential-magician \
.../cmd/docker-credential-magician

.PHONY: test
test:
Expand Down
27 changes: 27 additions & 0 deletions cmd/docker-credential-magic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
)

var (
// Version can be set via:
// -ldflags="-X main.Version=$TAG"
Version string

errorInvalidDomain = errors.New("supplied domain is invalid")

// TODO: should use existing cred helper/docker config if no match
Expand All @@ -31,6 +35,19 @@ var (
)

func main() {
args := os.Args
if len(args) < 2 {
usage()
}
subcommand := args[1]
if subcommand != "get" && subcommand != "version" {
usage()
}
if subcommand == "version" {
version()
}

// Assume subcommand is "get" here and read from stdin
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
rawInput := scanner.Text()
Expand Down Expand Up @@ -61,6 +78,16 @@ func main() {
}
}

func usage() {
fmt.Println("Usage: docker-credential-magic <get|version>")
os.Exit(1)
}

func version() {
fmt.Println(Version)
os.Exit(0)
}

func parseDomain(s string) (string, error) {
parts := strings.Split(s, ".")
numParts := len(parts)
Expand Down
26 changes: 23 additions & 3 deletions cmd/docker-credential-magician/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package main

import (
"github.com/spf13/cobra"
"fmt"
"log"
"os"

"github.com/spf13/cobra"

"github.com/docker-credential-magic/docker-credential-magic/pkg/magician"
)

type mutateSettings struct {
Tag string
Tag string
IncludeHelpers []string
}

// Version can be set via:
// -ldflags="-X main.Version=$TAG"
var Version string

func main() {
var mutate mutateSettings

Expand All @@ -21,8 +27,22 @@ func main() {
Short: "Augment images with various credential helpers (including magic)",
}

versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version and exit",
RunE: func(cmd *cobra.Command, args []string) error {
if Version == "" {
fmt.Println("could not determine build information")
} else {
fmt.Println(Version)
}
return nil
},
}
rootCmd.AddCommand(versionCmd)

mutateCmd := &cobra.Command{
Use: "mutate",
Use: "mutate",
Short: "Augment an image with one or more credential helpers",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down

0 comments on commit 714fcda

Please sign in to comment.