Skip to content

Commit

Permalink
new command on listview: r (show resource type recommendations)
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Oct 27, 2021
1 parent 69c142e commit 0852150
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/meta/meta_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m MetaDummy) ListResource() ImportList {
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/Microsoft.Network/networkInterfaces/example-nic",
},
ImportItem{
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/virtualNetworks/example-network/subnets/internal",
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/Microsoft.Network/virtualNetworks/example-network/subnets/internal",
},
ImportItem{
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg",
Expand Down
1 change: 1 addition & 0 deletions internal/ui/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
var (
TitleStyle = lipgloss.NewStyle().Foreground(Cream).Background(Indigo)
SubtitleStyle = lipgloss.NewStyle().Foreground(Cream).Background(SubtleIndigo)
InfoStyle = lipgloss.NewStyle().Foreground(Cream).Background(NoColor)
QuitMsgStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#DDDADA", Dark: "#3C3C3C"})
ErrorMsgStyle = lipgloss.NewStyle().Foreground(Red)
)
61 changes: 56 additions & 5 deletions internal/ui/importlist/importlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package importlist

import (
"fmt"
"regexp"
"sort"
"strings"
"time"

"github.com/magodo/aztfy/internal/meta"
"github.com/magodo/aztfy/internal/ui/aztfyclient"
"github.com/magodo/aztfy/internal/ui/common"
"github.com/magodo/aztfy/mapping"
"github.com/magodo/aztfy/schema"

"github.com/charmbracelet/bubbles/key"
Expand All @@ -17,8 +20,9 @@ import (
)

type Model struct {
c meta.Meta
listkeys listKeyMap
c meta.Meta
listkeys listKeyMap
recommendations [][]string

list list.Model
}
Expand All @@ -31,6 +35,9 @@ func NewModel(c meta.Meta, l meta.ImportList, idx int) Model {
}
sort.Strings(candidates)

// Build the recommendation for each list item
recommendations := buildResourceRecommendations(l)

// Build list items
var items []list.Item
for idx, item := range l {
Expand Down Expand Up @@ -62,8 +69,9 @@ func NewModel(c meta.Meta, l meta.ImportList, idx int) Model {
)

return Model{
c: c,
listkeys: newListKeyMap(),
c: c,
listkeys: newListKeyMap(),
recommendations: recommendations,

list: list,
}
Expand All @@ -78,7 +86,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

switch msg := msg.(type) {
case tea.KeyMsg:
// Don't intercept the apply key (i.e. "w") when user is inputting.
// Don't intercept the keys (e.g. "w") when user is inputting.
if m.isUserTyping() {
break
}
Expand Down Expand Up @@ -111,6 +119,18 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
}
return m, aztfyclient.ShowImportError(selItem.v, selItem.idx, m.importList(false))
case key.Matches(msg, m.listkeys.recommendation):
sel := m.list.SelectedItem()
if sel == nil {
return m, nil
}
selItem := sel.(Item)

recs := m.recommendations[selItem.idx]
if len(recs) == 0 {
return m, m.list.NewStatusMessage(common.InfoStyle.Render("No resource type recommendation is avaialble..."))
}
return m, m.list.NewStatusMessage(common.InfoStyle.Render(fmt.Sprintf("Possible resource type(s): %s", strings.Join(recs, ","))))
}
case tea.WindowSizeMsg:
// The height here minus the height occupied by the title
Expand Down Expand Up @@ -180,3 +200,34 @@ func (m Model) importList(clearErr bool) meta.ImportList {
}
return out
}

func buildResourceRecommendations(l meta.ImportList) [][]string {
resourceToAzureIdMapping := mapping.ProviderResourceMapping
azureIdToResourcesMapping := map[string][]string{}
for k, v := range resourceToAzureIdMapping {
resources, ok := azureIdToResourcesMapping[v]
if !ok {
resources = []string{}
}
resources = append(resources, k)
azureIdToResourcesMapping[v] = resources
}
azureIdPatternToResourcesMapping := map[*regexp.Regexp][]string{}
for path, resources := range azureIdToResourcesMapping {
p := regexp.MustCompile("^" + strings.ReplaceAll(path, "{}", "[^/]+") + "$")
azureIdPatternToResourcesMapping[p] = resources
}

recommendations := [][]string{}
for _, item := range l {
var recommendation []string
for pattern, resources := range azureIdPatternToResourcesMapping {
if pattern.MatchString(strings.ToUpper(item.ResourceID)) {
recommendation = resources
break
}
}
recommendations = append(recommendations, recommendation)
}
return recommendations
}
10 changes: 8 additions & 2 deletions internal/ui/importlist/listkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package importlist
import "github.com/charmbracelet/bubbles/key"

type listKeyMap struct {
apply key.Binding
error key.Binding
apply key.Binding
error key.Binding
recommendation key.Binding
}

func newListKeyMap() listKeyMap {
Expand All @@ -17,12 +18,17 @@ func newListKeyMap() listKeyMap {
key.WithKeys("e"),
key.WithHelp("e", "show error"),
),
recommendation: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "show recommendation"),
),
}
}

func (m listKeyMap) ToBindings() []key.Binding {
return []key.Binding{
m.apply,
m.error,
m.recommendation,
}
}

0 comments on commit 0852150

Please sign in to comment.