diff --git a/cmd/finch/version.go b/cmd/finch/version.go index 79f362342..20c18d076 100644 --- a/cmd/finch/version.go +++ b/cmd/finch/version.go @@ -88,8 +88,9 @@ func (va *versionAction) printVersion() error { if status != lima.Running { return errors.New("detailed version info is unavailable because VM is not running") } - - limaArgs := []string{"shell", limaInstanceName, "sudo", "nerdctl", "version", "--format", "json"} + // Add -E to sudo command in order to preserve existing environment variables, more info: + // https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo/8633575#8633575 + limaArgs := []string{"shell", limaInstanceName, "sudo", "-E", "nerdctl", "version", "--format", "json"} out, err := va.creator.CreateWithoutStdio(limaArgs...).Output() if err != nil { return fmt.Errorf("failed to create the nerdctl version command: %w", err) diff --git a/cmd/finch/version_test.go b/cmd/finch/version_test.go index 56b6b608f..f5f4c31f5 100644 --- a/cmd/finch/version_test.go +++ b/cmd/finch/version_test.go @@ -52,7 +52,7 @@ func TestVersionAction_runAdaptor(t *testing.T) { logger.EXPECT().Debugf("Status of virtual machine: %s", "Running") command := mocks.NewCommand(ctrl) - lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "nerdctl", "version", "--format", "json").Return(command) + lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "-E", "nerdctl", "version", "--format", "json").Return(command) //nolint: lll // Version output format is larger than 500 command.EXPECT().Output().Return([]byte(`{"Client":{"Version":"v1.0.0","GitCommit":"c00780a1f5b905b09812722459c54936c9e070e6","GoVersion":"go1.19.2","Os":"linux","Arch":"arm64","Components":[{"Name":"buildctl","Version":"v0.10.5","Details":{"GitCommit":"bc26045116045516ff2427201abd299043eaf8f7"}}]},"Server":{"Components":[{"Name":"containerd","Version":"v1.6.8","Details":{"GitCommit":"9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6"}},{"Name":"runc","Version":"1.1.4","Details":{"GitCommit":"v1.1.4-0-g5fd4c4d1"}}]}}`), nil) }, @@ -98,7 +98,7 @@ func TestVersionAction_run(t *testing.T) { logger.EXPECT().Debugf("Status of virtual machine: %s", "Running") command := mocks.NewCommand(ctrl) - lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "nerdctl", "version", "--format", "json").Return(command) + lcc.EXPECT().CreateWithoutStdio("shell", limaInstanceName, "sudo", "-E", "nerdctl", "version", "--format", "json").Return(command) command.EXPECT().Output().Return([]byte(`{ "Client":{ "Version":"v1.0.0",