Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix shell command #3342

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions apps/cnquery/cmd/shell.go
Original file line number Diff line number Diff line change
@@ -108,11 +108,17 @@

connectAsset := filteredAssets[0]
if len(filteredAssets) > 1 {
invAssets := make([]*inventory.Asset, 0, len(filteredAssets))
for _, a := range filteredAssets {
invAssets = append(invAssets, a.Asset)
}

isTTY := isatty.IsTerminal(os.Stdout.Fd())
if isTTY {
connectAsset = components.AssetSelect(filteredAssets)
selectedAsset := components.AssetSelect(invAssets)
connectAsset = filteredAssets[selectedAsset]
} else {
fmt.Println(components.AssetList(theme.OperatingSystemTheme, filteredAssets))
fmt.Println(components.AssetList(theme.OperatingSystemTheme, invAssets))
log.Fatal().Msg("cannot connect to more than one asset, use --platform-id to select a specific asset")
}
}
@@ -121,19 +127,12 @@
log.Fatal().Msg("no asset selected")
}

err = runtime.Connect(&plugin.ConnectReq{
Features: conf.Features,
Asset: connectAsset,
Upstream: conf.UpstreamConfig,
})
if err != nil {
log.Fatal().Err(err).Msg("failed to connect to asset")
}
log.Info().Msgf("connected to %s", runtime.Provider.Connection.Asset.Platform.Title)
log.Info().Msgf("connected to %s", connectAsset.Runtime.Provider.Connection.Asset.Platform.Title)

Check failure on line 130 in apps/cnquery/cmd/shell.go

GitHub Actions / golangci-lint

SA5011: possible nil pointer dereference (staticcheck)

// when we close the shell, we need to close the backend and store the recording
onCloseHandler := func() {
runtime.Close()
connectAsset.Runtime.Close()
providers.Coordinator.Shutdown()
}

@@ -142,7 +141,7 @@
shellOptions = append(shellOptions, shell.WithFeatures(conf.Features))
shellOptions = append(shellOptions, shell.WithUpstreamConfig(conf.UpstreamConfig))

sh, err := shell.New(runtime, shellOptions...)
sh, err := shell.New(connectAsset.Runtime, shellOptions...)
if err != nil {
log.Error().Err(err).Msg("failed to initialize interactive shell")
}
6 changes: 3 additions & 3 deletions cli/components/assetselect.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
)

func AssetSelect(assetList []*inventory.Asset) *inventory.Asset {
func AssetSelect(assetList []*inventory.Asset) int {
list := make([]string, len(assetList))

// map asset name to list
@@ -36,9 +36,9 @@ func AssetSelect(assetList []*inventory.Asset) *inventory.Asset {
}

if selection == -1 {
return nil
return -1
}
selected := assetList[selection]
log.Info().Int("selection", selection).Str("asset", selected.Name).Msg("selected asset")
return selected
return selection
}
6 changes: 3 additions & 3 deletions explorer/scan/discovery.go
Original file line number Diff line number Diff line change
@@ -57,12 +57,12 @@ func (d *DiscoveredAssets) AddError(asset *inventory.Asset, err error) {
d.Errors = append(d.Errors, &AssetWithError{Asset: asset, Err: err})
}

func (d *DiscoveredAssets) GetAssetsByPlatformID(platformID string) []*inventory.Asset {
var assets []*inventory.Asset
func (d *DiscoveredAssets) GetAssetsByPlatformID(platformID string) []*AssetWithRuntime {
var assets []*AssetWithRuntime
for _, a := range d.Assets {
for _, p := range a.Asset.PlatformIds {
if platformID == "" || p == platformID {
assets = append(assets, a.Asset)
assets = append(assets, a)
break
}
}
Loading