Skip to content

Commit

Permalink
Merge pull request #66 from rancherfederal/cli-version
Browse files Browse the repository at this point in the history
add version command to cli
  • Loading branch information
joshrwolf authored Nov 11, 2021
2 parents 933af22 + 83d989a commit 49eb9e2
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 69 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
6 changes: 6 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ before:
hooks:
- go mod tidy
- go mod download

env:
- vpkg=github.com/rancherfederal/hauler/pkg/version

builds:
- main: cmd/hauler/main.go
goos:
Expand All @@ -12,6 +16,8 @@ builds:
goarch:
- amd64
- arm64
ldflags:
- -s -w -X {{ .Env.vpkg }}.GitVersion={{ .Version }} -X {{ .Env.vpkg }}.commit={{ .ShortCommit }} -X {{ .Env.vpkg }}.buildDate={{ .Date }}
env:
- CGO_ENABLED=0

Expand Down
1 change: 1 addition & 0 deletions cmd/hauler/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func New() *cobra.Command {
// Add subcommands
addDownload(cmd)
addStore(cmd)
addVersion(cmd)

return cmd
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hauler/cli/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Cmd(ctx context.Context, o *Opts, reference string) error {

lgr.Infof("downloaded [%s] to [%s]", ref.Name(), outputFile)

case types.FileMediaType:
case types.FileConfigMediaType:
lgr.Infof("identified [file] (%s) content", manifest.Config.MediaType)

fs := content.NewFileStore(o.DestinationDir)
Expand Down
37 changes: 37 additions & 0 deletions cmd/hauler/cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/rancherfederal/hauler/pkg/version"
)

func addVersion(parent *cobra.Command) {
var json bool

cmd := &cobra.Command{
Use: "version",
Short: "Print current hauler version",
Long: "Print current hauler version",
Aliases: []string{"v"},
RunE: func(cmd *cobra.Command, args []string) error {
v := version.GetVersionInfo()
response := v.String()
if json {
data, err := v.JSONString()
if err != nil {
return err
}
response = data
}
fmt.Print(response)
return nil
},
}

cmd.Flags().BoolVar(&json, "json", false, "toggle output in JSON")

parent.AddCommand(cmd)
}
23 changes: 17 additions & 6 deletions pkg/artifact/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ package types
const (
OCIManifestSchema1 = "application/vnd.oci.image.manifest.v1+json"
DockerManifestSchema2 = "application/vnd.docker.distribution.manifest.v2+json"
DockerConfigJSON = "application/vnd.docker.container.image.v1+json"
UnknownManifest = "application/vnd.hauler.cattle.io.unknown.v1+json"

UnknownLayer = "application/vnd.content.hauler.unknown.layer"
FileLayerMediaType = "application/vnd.content.hauler.file.layer.v1"
FileMediaType = "application/vnd.content.hauler.file.config.v1+json"
DockerConfigJSON = "application/vnd.docker.container.image.v1+json"

// ConfigMediaType is the reserved media type for the Helm chart manifest config
// ChartConfigMediaType is the reserved media type for the Helm chart manifest config
ChartConfigMediaType = "application/vnd.cncf.helm.config.v1+json"

// ChartLayerMediaType is the reserved media type for Helm chart package content
Expand All @@ -19,6 +15,21 @@ const (
// ProvLayerMediaType is the reserved media type for Helm chart provenance files
ProvLayerMediaType = "application/vnd.cncf.helm.chart.provenance.v1.prov"

// FileLayerMediaType is the reserved media type for File content layers
FileLayerMediaType = "application/vnd.content.hauler.file.layer.v1"

// FileConfigMediaType is the reserved media type for File config
FileConfigMediaType = "application/vnd.content.hauler.file.config.v1+json"

// WasmArtifactLayerMediaType is the reserved media type for WASM artifact layers
WasmArtifactLayerMediaType = "application/vnd.wasm.content.layer.v1+wasm"

// WasmConfigMediaType is the reserved media type for WASM configs
WasmConfigMediaType = "application/vnd.wasm.config.v1+json"

UnknownManifest = "application/vnd.hauler.cattle.io.unknown.v1+json"
UnknownLayer = "application/vnd.content.hauler.unknown.layer"

OCIVendorPrefix = "vnd.oci"
DockerVendorPrefix = "vnd.docker"
HaulerVendorPrefix = "vnd.hauler"
Expand Down
4 changes: 2 additions & 2 deletions pkg/content/file/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c config) Descriptor() (gv1.Descriptor, error) {
}

return gv1.Descriptor{
MediaType: types.FileMediaType,
MediaType: types.FileConfigMediaType,
Size: c.size,
Digest: c.hash,
URLs: c.URLs,
Expand All @@ -47,7 +47,7 @@ func (c config) Digest() (gv1.Hash, error) {
}

func (c config) MediaType() (gtypes.MediaType, error) {
return types.FileMediaType, nil
return types.FileConfigMediaType, nil
}

func (c config) Size() (int64, error) {
Expand Down
61 changes: 61 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package version

import (
"encoding/json"
"fmt"
"path"
"runtime"
"strings"
"text/tabwriter"
)

var (
GitVersion = "devel"
commit = "unknown"
buildDate = "unknown"
)

type Info struct {
GitVersion string
GitCommit string
BuildDate string

GoVersion string
Compiler string
Platform string
}

func GetVersionInfo() Info {
return Info{
GitVersion: GitVersion,
GitCommit: commit,
BuildDate: buildDate,

GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: path.Join(runtime.GOOS, runtime.GOARCH),
}
}

func (i Info) String() string {
b := strings.Builder{}
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)

fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler)
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform)

w.Flush()
return b.String()
}

func (i Info) JSONString() (string, error) {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
18 changes: 0 additions & 18 deletions scripts/save-docker-image-tar.sh

This file was deleted.

17 changes: 0 additions & 17 deletions scripts/save-docker-image.sh

This file was deleted.

25 changes: 0 additions & 25 deletions scripts/save-k3s-artifacts.sh

This file was deleted.

0 comments on commit 49eb9e2

Please sign in to comment.