go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/cli/components/assetselect.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package components
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	tea "github.com/charmbracelet/bubbletea"
    11  	"github.com/rs/zerolog/log"
    12  	"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
    13  )
    14  
    15  func AssetSelect(assetList []*inventory.Asset) *inventory.Asset {
    16  	list := make([]string, len(assetList))
    17  
    18  	// map asset name to list
    19  	for i := range assetList {
    20  		a := assetList[i]
    21  		name := a.Name
    22  		if a.Platform != nil {
    23  			name = fmt.Sprintf("%s (%s)", a.Name, a.Platform.Name)
    24  		}
    25  		list[i] = name
    26  	}
    27  
    28  	selection := -1 // make sure we have an invalid index
    29  	model := NewListModel("Available assets", list, func(s int) {
    30  		selection = s
    31  	})
    32  	_, err := tea.NewProgram(model, tea.WithInputTTY()).Run()
    33  	if err != nil {
    34  		fmt.Println("Error running program:", err)
    35  		os.Exit(1)
    36  	}
    37  
    38  	if selection == -1 {
    39  		return nil
    40  	}
    41  	selected := assetList[selection]
    42  	log.Info().Int("selection", selection).Str("asset", selected.Name).Msg("selected asset")
    43  	return selected
    44  }