From d007884328a571c6b0185ddf8ad9e3371dbcfeab Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Fri, 1 Oct 2021 14:52:46 -0300 Subject: [PATCH] improve info: kool, docker, docker-compose version/paths --- commands/info.go | 60 +++++++++++++++++++++++++++++++++++++++++-- commands/info_test.go | 56 +++++++++++++++------------------------- commands/root_test.go | 5 +--- 3 files changed, 80 insertions(+), 41 deletions(-) diff --git a/commands/info.go b/commands/info.go index 8b04b8f2..6754eb9a 100644 --- a/commands/info.go +++ b/commands/info.go @@ -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" @@ -12,6 +16,7 @@ type KoolInfo struct { DefaultKoolService envStorage environment.EnvStorage + cmdDocker, cmdDockerCompose builder.Command } // NewInfoCmd initializes new kool info command @@ -32,6 +37,8 @@ func NewKoolInfo() *KoolInfo { return &KoolInfo{ *newDefaultKoolService(), environment.NewEnvStorage(), + builder.NewCommand("docker", "-v"), + builder.NewCommand("docker-compose", "-v"), } } @@ -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 } diff --git a/commands/info_test.go b/commands/info_test.go index fabf9052..3fb17e0f 100644 --- a/commands/info_test.go +++ b/commands/info_test.go @@ -1,59 +1,56 @@ 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) @@ -61,27 +58,16 @@ func TestFilteredInfo(t *testing.T) { 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 } diff --git a/commands/root_test.go b/commands/root_test.go index 700eedc8..0e726c2c 100644 --- a/commands/root_test.go +++ b/commands/root_test.go @@ -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)