-
Notifications
You must be signed in to change notification settings - Fork 172
/
progress.go
146 lines (123 loc) · 3.21 KB
/
progress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package progress
import (
"context"
"fmt"
"github.com/Azure/aztfexport/pkg/meta"
"github.com/Azure/aztfexport/internal/ui/aztfexportclient"
"github.com/Azure/aztfexport/internal/ui/common"
prog "github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
)
type result struct {
item meta.ImportItem
emoji string
}
type Model struct {
ctx context.Context
c meta.Meta
l meta.ImportList
idx int
parallelism int
results []result
progress prog.Model
}
func NewModel(ctx context.Context, c meta.Meta, parallelism int, l meta.ImportList) Model {
return Model{
ctx: ctx,
c: c,
l: l,
idx: 0,
parallelism: parallelism,
results: make([]result, common.ProgressShowLastResults),
progress: prog.NewModel(prog.WithDefaultGradient()),
}
}
func (m Model) Init() tea.Cmd {
if m.iterationDone() {
return aztfexportclient.FinishImport(m.l)
}
n := m.parallelism
if m.idx+m.parallelism > len(m.l) {
n = len(m.l) - m.idx
}
return tea.Batch(
aztfexportclient.ImportItems(m.ctx, m.c, m.l[m.idx:m.idx+n]),
)
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.progress.Width = msg.Width - 4
return m, nil
// FrameMsg is sent when the progress bar wants to animate itself
case prog.FrameMsg:
progressModel, cmd := m.progress.Update(msg)
m.progress = progressModel.(prog.Model)
return m, cmd
case aztfexportclient.ImportItemsDoneMsg:
var cmds []tea.Cmd
// Update results
items := msg.Items
for i := range items {
m.l[m.idx+i] = items[i]
emoji := common.RandomHappyEmoji()
if items[i].ImportError != nil {
emoji = common.WarningEmoji
}
res := result{
item: items[i],
emoji: emoji,
}
m.results = append(m.results[1:], res)
}
m.idx += m.parallelism
if m.iterationDone() {
cmds = append(cmds, m.progress.SetPercent(1))
cmds = append(cmds, aztfexportclient.FinishImport(m.l))
return m, tea.Batch(cmds...)
}
cmds = append(cmds, m.progress.SetPercent(float64(m.idx)/float64(len(m.l))))
n := m.parallelism
if m.idx+m.parallelism > len(m.l) {
n = len(m.l) - m.idx
}
cmds = append(cmds, aztfexportclient.ImportItems(m.ctx, m.c, m.l[m.idx:m.idx+n]))
return m, tea.Batch(cmds...)
default:
return m, nil
}
}
func (m Model) View() string {
msg := ""
if len(m.l) > m.idx {
item := m.l[m.idx]
if item.Skip() {
msg = fmt.Sprintf(" Skipping %s...", item.TFResourceId)
} else {
msg = fmt.Sprintf(" Importing %s...", item.TFResourceId)
}
}
s := fmt.Sprintf(" %s\n\n", msg)
for _, res := range m.results {
// This indicates the state before the item is inserted as the to results.
if res.item.TFResourceId == "" {
s += "...\n"
} else {
switch {
case res.item.Skip():
s += fmt.Sprintf("%s %s skipped\n", res.emoji, res.item.TFResourceId)
default:
if res.item.ImportError == nil {
s += fmt.Sprintf("%s %s import successfully\n", res.emoji, res.item.TFResourceId)
} else {
s += fmt.Sprintf("%s %s import failed\n", res.emoji, res.item.TFResourceId)
}
}
}
}
s += "\n\n" + m.progress.View()
return s
}
func (m Model) iterationDone() bool {
return m.idx >= len(m.l)
}