Skip to content

Commit

Permalink
Merge branch 'main' into chore/issue-100
Browse files Browse the repository at this point in the history
  • Loading branch information
Pehesi97 authored Oct 18, 2023
2 parents 05e4c47 + 387ecbd commit 9571ca1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import (
//go:embed assets
var resources embed.FS

// Based on https://goreleaser.com/cookbooks/using-main.version/
// The values will be set at release time by the github action
var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
icli := cli.New(resources)
icli := cli.New(resources, cli.Release{Version: version, Commit: commit, Date: date})

if err := icli.Run(os.Args); err != nil {
log.Fatal(err)
Expand Down
22 changes: 20 additions & 2 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"io"
"io/fs"

Expand All @@ -18,6 +19,12 @@ import (
"github.com/urfave/cli/v2"
)

type Release struct {
Version string
Commit string
Date string
}

type icli struct {
Resources fs.FS
CWD string
Expand All @@ -27,18 +34,20 @@ type icli struct {
dockerImage docker.DockerImage
flags flags
Writer io.Writer
release Release
}

func NewWithOptions(resources fs.FS, logger *log.Logger, writer io.Writer) icli {
func NewWithOptions(resources fs.FS, logger *log.Logger, writer io.Writer, release Release) icli {
return icli{
Resources: resources,
Logger: logger,
Writer: writer,
flags: InitFlags(),
release: release,
}
}

func New(resources fs.FS) icli {
func New(resources fs.FS, release Release) icli {
return NewWithOptions(
resources,
log.NewWithOptions(os.Stderr, log.Options{
Expand All @@ -47,6 +56,7 @@ func New(resources fs.FS) icli {
ReportTimestamp: true,
}),
os.Stdout,
release,
)
}

Expand Down Expand Up @@ -141,6 +151,14 @@ func (c icli) Run(args []string) error {
c.OnBranchCMD(),
c.TemplateCMD(),
c.InitCMD(),
{
Name: "version",
Usage: "Return the version of the cli",
Action: func(ctx *cli.Context) error {
_, err := fmt.Fprintf(c.Writer, "version %s, commit %s, built at %s\n", c.release.Version, c.release.Commit, c.release.Date)
return err
},
},
},
Before: func(ctx *cli.Context) error {
if err := c.loadFlagsFromConfig(ctx); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions src/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cli

import (
"os"
"testing"

"gotest.tools/v3/assert"
)

func TestVersion(t *testing.T) {
expected := "version 1, commit unknown, built at today\n"
icli := geticliForTesting(os.DirFS("../.."))

reseticliBuffer(&icli)
icli.Run([]string{"initium", "version"})
output := icliOutput(icli)
assert.Assert(t, output == expected, "Expected %s, got %s", expected, output)
}
31 changes: 22 additions & 9 deletions src/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ runtime-version: null
}
}

func GeticliForTesting(resources fs.FS) icli {
func geticliForTesting(resources fs.FS) icli {
return NewWithOptions(
resources,
log.NewWithOptions(os.Stderr, log.Options{
Expand All @@ -45,11 +45,24 @@ func GeticliForTesting(resources fs.FS) icli {
ReportTimestamp: true,
}),
new(bytes.Buffer),
Release{
Version: "1",
Date: "today",
Commit: "unknown",
},
)
}

func reseticliBuffer(c *icli) {
c.Writer = new(bytes.Buffer)
}

func icliOutput(c icli) string {
return fmt.Sprint(c.Writer.(*bytes.Buffer))
}

func TestInitConfig(t *testing.T) {
icli := GeticliForTesting(os.DirFS("../.."))
icli := geticliForTesting(os.DirFS("../.."))
// Config file is read correctly

// Generate temporary file and add app-name parameter
Expand All @@ -66,7 +79,7 @@ func TestInitConfig(t *testing.T) {
t.Errorf("writing config content %v", err)
}

icli.Writer = new(bytes.Buffer)
reseticliBuffer(&icli)
if err = icli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config"}); err != nil {
t.Error(err)
}
Expand All @@ -77,14 +90,14 @@ func TestInitConfig(t *testing.T) {
os.Setenv("INITIUM_PROJECT_TYPE", "go")
defer os.Unsetenv("INITIUM_PROJECT_TYPE")
defer os.Unsetenv("INITIUM_APP_NAME") // Unset the environment variable at the end
icli.Writer = new(bytes.Buffer)
reseticliBuffer(&icli)
if err = icli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromEnv", registry, "go", icli.Writer)

// Command line argument wins over config and Environment variable
icli.Writer = new(bytes.Buffer)
reseticliBuffer(&icli)
if err = icli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config", "--app-name=FromParam", "--project-type=go"}); err != nil {
t.Error(err)
}
Expand All @@ -93,7 +106,7 @@ func TestInitConfig(t *testing.T) {
}

func TestRepoNameRetrocompatibiliy(t *testing.T) {
cli := GeticliForTesting(os.DirFS("../.."))
cli := geticliForTesting(os.DirFS("../.."))

// Generate temporary file and add repo-name parameter
f, err := os.CreateTemp("", "tmpfile-")
Expand All @@ -107,22 +120,22 @@ func TestRepoNameRetrocompatibiliy(t *testing.T) {
t.Errorf("writing config content %v", err)
}

cli.Writer = new(bytes.Buffer)
reseticliBuffer(&cli)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config", "--app-name=FromParam"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromParam", "FromFile", "go", cli.Writer)

//Override from parameter
cli.Writer = new(bytes.Buffer)
reseticliBuffer(&cli)
if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config", "--app-name=FromParam", "--container-registry=ghcr.io/nearform"}); err != nil {
t.Error(err)
}
compareConfig(t, "FromParam", "ghcr.io/nearform", "go", cli.Writer)
}

func TestAppName(t *testing.T) {
cli := GeticliForTesting(os.DirFS("../.."))
cli := geticliForTesting(os.DirFS("../.."))

err := cli.Run([]string{"initium", "build"})
if err == nil {
Expand Down

0 comments on commit 9571ca1

Please sign in to comment.