mirror of https://github.com/artifacthub/hub.git
Add version subcommand to CLI tool (#1479)
Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
This commit is contained in:
parent
dab778e1cc
commit
01776bc2b1
|
|
@ -49,6 +49,8 @@ jobs:
|
|||
- name: Build ah image
|
||||
run: |
|
||||
docker build \
|
||||
--build-arg VERSION=${{steps.extract_tag_name.outputs.tag}} \
|
||||
--build-arg GIT_COMMIT=$GITHUB_SHA \
|
||||
-f cmd/ah/Dockerfile \
|
||||
-t artifacthub/ah:${{steps.extract_tag_name.outputs.tag}} \
|
||||
-t artifacthub/ah:latest .
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
# Build ah
|
||||
FROM golang:1.16-alpine3.14 AS ah-builder
|
||||
ARG VERSION
|
||||
ARG GIT_COMMIT
|
||||
WORKDIR /go/src/github.com/artifacthub/ah
|
||||
COPY go.* ./
|
||||
COPY cmd/ah cmd/ah
|
||||
COPY internal internal
|
||||
RUN cd cmd/ah && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /ah .
|
||||
RUN cd cmd/ah && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-X main.version=$VERSION -X main.gitCommit=$GIT_COMMIT" -o /ah .
|
||||
|
||||
# Final stage
|
||||
FROM alpine:3.14
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ func main() {
|
|||
}
|
||||
rootCmd.AddCommand(
|
||||
newLintCmd(),
|
||||
newVersionCmd(),
|
||||
)
|
||||
|
||||
// Execute root command
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
update = flag.Bool("update", false, "Update tests golden files")
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
flag.Parse()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
|
@ -12,15 +11,6 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
update = flag.Bool("update", false, "Update tests golden files")
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
flag.Parse()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestLintCmd(t *testing.T) {
|
||||
testCases := []struct {
|
||||
kind string
|
||||
|
|
@ -65,13 +55,13 @@ func TestLintCmd(t *testing.T) {
|
|||
cmd.SilenceUsage = true
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SetOut(&b)
|
||||
cmd.SetArgs([]string{"--kind", tc.kind, "--path", filepath.Join("testdata", tc.path, "pkgs")})
|
||||
cmd.SetArgs([]string{"--kind", tc.kind, "--path", filepath.Join("testdata", "lint", tc.path, "pkgs")})
|
||||
cmdErr := cmd.Execute()
|
||||
|
||||
// Read command output and check it matches what we expect
|
||||
cmdOutput, err := io.ReadAll(&b)
|
||||
require.NoError(t, err)
|
||||
goldenPath := filepath.Join("testdata", tc.path, "output.golden")
|
||||
goldenPath := filepath.Join("testdata", "lint", tc.path, "output.golden")
|
||||
if *update {
|
||||
// Update tests golden files
|
||||
golden, err := os.Create(goldenPath)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
✓ test 0.0.1 (testdata/test1/pkgs)
|
||||
✓ test 0.0.1 (testdata/lint/test1/pkgs)
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Package lint SUCCEEDED!
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
✓ test1 0.0.1 (testdata/test2/pkgs/chart1)
|
||||
✓ test1 0.0.1 (testdata/lint/test2/pkgs/chart1)
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Package lint SUCCEEDED!
|
||||
|
|
@ -36,7 +36,7 @@ Package lint SUCCEEDED!
|
|||
! Sign key: *** NOT PROVIDED ***
|
||||
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
✓ test2 0.0.1 (testdata/test2/pkgs/chart2)
|
||||
✓ test2 0.0.1 (testdata/lint/test2/pkgs/chart2)
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Package lint SUCCEEDED!
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
✗ test 0.0.1 (testdata/test3/pkgs)
|
||||
✗ test 0.0.1 (testdata/lint/test3/pkgs)
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Package lint FAILED. 1 error(s) occurred:
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Version: 0.0.1
|
||||
Git commit: aaabbbcccdddeeefff
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Version:
|
||||
Git commit:
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
// These vars represent some version information and they are meant to be
|
||||
// overwritten with LDFLAGS.
|
||||
version string
|
||||
gitCommit string
|
||||
)
|
||||
|
||||
// newVersionCmd creates a new version command.
|
||||
func newVersionCmd() *cobra.Command {
|
||||
versionCmd := &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print version information",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
printVersion(cmd.OutOrStdout())
|
||||
},
|
||||
}
|
||||
return versionCmd
|
||||
}
|
||||
|
||||
// printVersion prints some version information to the writer provided.
|
||||
func printVersion(out io.Writer) {
|
||||
fmt.Fprintf(out, "Version: %s\n", version)
|
||||
fmt.Fprintf(out, "Git commit: %s\n", gitCommit)
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestVersionCmd(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
version string
|
||||
gitCommit string
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
"0.0.1",
|
||||
"aaabbbcccdddeeefff",
|
||||
},
|
||||
{
|
||||
"test2",
|
||||
"",
|
||||
"",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Prepare command and execute it
|
||||
version = tc.version
|
||||
gitCommit = tc.gitCommit
|
||||
var b bytes.Buffer
|
||||
cmd := newVersionCmd()
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SetOut(&b)
|
||||
cmd.SetArgs([]string{})
|
||||
cmdErr := cmd.Execute()
|
||||
|
||||
// Read command output and check it matches what we expect
|
||||
cmdOutput, err := io.ReadAll(&b)
|
||||
require.NoError(t, err)
|
||||
goldenPath := filepath.Join("testdata", "version", tc.name, "output.golden")
|
||||
if *update {
|
||||
// Update tests golden files
|
||||
golden, err := os.Create(goldenPath)
|
||||
require.NoError(t, err)
|
||||
_, err = golden.Write(cmdOutput)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
expectedOutput, err := os.ReadFile(goldenPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, expectedOutput, cmdOutput)
|
||||
assert.Equal(t, nil, cmdErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
# Build docker images
|
||||
GIT_SHA=$(git rev-parse HEAD)
|
||||
docker build -f cmd/ah/Dockerfile -t artifacthub/ah -t artifacthub/ah:$GIT_SHA .
|
||||
docker build -f cmd/ah/Dockerfile -t artifacthub/ah -t artifacthub/ah:$GIT_SHA --build-arg VERSION=devel --build-arg GIT_COMMIT=$GIT_SHA .
|
||||
docker build -f cmd/hub/Dockerfile -t artifacthub/hub -t artifacthub/hub:$GIT_SHA .
|
||||
docker build -f database/migrations/Dockerfile -t artifacthub/db-migrator -t artifacthub/db-migrator:$GIT_SHA .
|
||||
docker build -f cmd/scanner/Dockerfile -t artifacthub/scanner -t artifacthub/scanner:$GIT_SHA .
|
||||
|
|
|
|||
Loading…
Reference in New Issue