Skip to content

Commit

Permalink
improve info: kool, docker, docker-compose version/paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciojs committed Oct 1, 2021
1 parent d1fb2ae commit d007884
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 41 deletions.
60 changes: 58 additions & 2 deletions commands/info.go
Original file line number Diff line number Diff line change
@@ -1,7 1,11 @@
package commands

import (
"fmt"
"kool-dev/kool/core/builder"
"kool-dev/kool/core/environment"
"os"
"os/exec"
"strings"

"github.com/spf13/cobra"
Expand All @@ -12,6 16,7 @@ type KoolInfo struct {
DefaultKoolService

envStorage environment.EnvStorage
cmdDocker, cmdDockerCompose builder.Command
}

// NewInfoCmd initializes new kool info command
Expand All @@ -32,6 37,8 @@ func NewKoolInfo() *KoolInfo {
return &KoolInfo{
*newDefaultKoolService(),
environment.NewEnvStorage(),
builder.NewCommand("docker", "-v"),
builder.NewCommand("docker-compose", "-v"),
}
}

Expand All @@ -41,16 48,65 @@ func AddKoolInfo(root *cobra.Command) {

// Execute executes info logic
func (i *KoolInfo) Execute(args []string) (err error) {
var filter string = "KOOL_"
var (
filter string = "KOOL_"
output string
)

if len(args) > 0 {
filter = args[0]
}

// kool CLI info
i.Println("Kool Version ", version)
if output, err = os.Executable(); err != nil {
fmt.Println("err1")
return
}
i.Println("Kool Bin Path:", output)

i.Println("")
// docker CLI info
if output, err = i.Exec(i.cmdDocker); err != nil {
return
}
i.Println(output)

if err = i.shell.LookPath(i.cmdDocker); err != nil {
return
}
output, _ = exec.LookPath(i.cmdDocker.Cmd())

i.Println("Docker Bin Path:", output)

i.Println("")

// docker-compose CLI info
if output, err = i.Exec(i.cmdDockerCompose); err != nil {
// just alert missing docker-compose, but don't elevate error
i.Warning("Docker Compose:", err.Error())
i.Warning("It's okay not having docker-compose installed, as kool will fallback to using a container for it when necessary.")
err = nil
} else {
i.Println(output)
output, _ = exec.LookPath("docker-compose")
i.Println("Docker Compose Bin Path:", output)
}

i.Println("")
i.Println("Environment Variables of Interest:")
i.Println("")

for _, envVar := range i.envStorage.All() {
if strings.Contains(envVar, filter) {
i.Println(envVar)
// keep from printing out known to be sensitive values
if strings.Contains(envVar, "KOOL_API_TOKEN") {
i.Warning("KOOL_API_TOKEN=***************** [redacted]")
} else {
i.Println(envVar)
}
}
}

return
}
56 changes: 21 additions & 35 deletions commands/info_test.go
Original file line number Diff line number Diff line change
@@ -1,87 1,73 @@
package commands

import (
"bytes"
"io"
"kool-dev/kool/core/builder"
"kool-dev/kool/core/environment"
"sort"
"kool-dev/kool/core/shell"
"strings"
"testing"

"github.com/spf13/cobra"
)

const testingEnv string = `
KOOL_FILTER_TESTING=1
KOOL_TESTING=1
`

func setup(f *KoolInfo) {
f.envStorage.Set("KOOL_FILTER_TESTING", "1")
f.envStorage.Set("KOOL_TESTING", "1")
}

func TestInfo(t *testing.T) {
f := &KoolInfo{
*newDefaultKoolService(),
func fakeKoolInfo() *KoolInfo {
return &KoolInfo{
*newFakeKoolService(),
environment.NewFakeEnvStorage(),
&builder.FakeCommand{},
&builder.FakeCommand{},
}
}

func TestInfo(t *testing.T) {
f := fakeKoolInfo()

setup(f)

output, err := execInfoCommand(NewInfoCmd(f))
output, err := execInfoCommand(NewInfoCmd(f), f)

if err != nil {
t.Fatal(err)
}

expected := strings.Trim(testingEnv, "\n")

if output != expected {
t.Errorf("Expected '%s', got '%s'", expected, output)
for _, expected := range []string{"KOOL_FILTER_TESTING=1", "KOOL_TESTING=1"} {
if !strings.Contains(output, expected) {
t.Errorf("Expected '%s', got '%s'", expected, output)
}
}
}

func TestFilteredInfo(t *testing.T) {
f := &KoolInfo{
*newDefaultKoolService(),
environment.NewFakeEnvStorage(),
}
f := fakeKoolInfo()

setup(f)

cmd := NewInfoCmd(f)
cmd.SetArgs([]string{"FILTER"})

output, err := execInfoCommand(cmd)
output, err := execInfoCommand(cmd, f)

if err != nil {
t.Fatal(err)
}

expected := "KOOL_FILTER_TESTING=1"

if output != expected {
if !strings.Contains(output, expected) {
t.Errorf("Expected '%s', got '%s'", expected, output)
}
}

func execInfoCommand(cmd *cobra.Command) (output string, err error) {
b := bytes.NewBufferString("")
cmd.SetOut(b)

func execInfoCommand(cmd *cobra.Command, f *KoolInfo) (output string, err error) {
if err = cmd.Execute(); err != nil {
return
}

var out []byte
if out, err = io.ReadAll(b); err != nil {
return
}

envs := strings.Split(strings.Trim(string(out), "\n"), "\n")
sort.Strings(envs)

output = strings.Join(envs, "\n")
output = strings.Join(f.shell.(*shell.FakeShell).OutLines, "\n")
return
}
5 changes: 1 addition & 4 deletions commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 175,7 @@ func TestMultipleServicesFailingDefaultCommandRunFunction(t *testing.T) {
func TestVerboseFlagRootCommand(t *testing.T) {
fakeEnv := environment.NewFakeEnvStorage()

fInfo := &KoolInfo{
*newFakeKoolService(),
fakeEnv,
}
fInfo := fakeKoolInfo()

root := NewRootCmd(fakeEnv)
info := NewInfoCmd(fInfo)
Expand Down

0 comments on commit d007884

Please sign in to comment.