Skip to content

Commit a827a33

Browse files
synced deploy and verify implementation & introduced remote state simulation
1 parent 0e02946 commit a827a33

3 files changed

Lines changed: 60 additions & 31 deletions

File tree

client/testui/client.go

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import (
1919

2020
type Client struct {
2121
// local temporary repository for testing ui features
22-
publicKeys []client.PublicKey
23-
accounts []client.Account
24-
links []client.Link
22+
publicKeys []client.PublicKey
23+
accounts []client.Account
24+
links []client.Link
25+
remoteStates map[client.AccountId]string
2526

2627
// id counter to simulate serial
2728
publicKeyIdCounter client.PublicKeyId
@@ -52,7 +53,7 @@ func (c *Client) accountDeployCache(account client.Account, deployCache string)
5253
// --- Lifecycle & Initialization ---
5354

5455
func NewClient() *Client {
55-
return &Client{}
56+
return &Client{remoteStates: make(map[client.AccountId]string)}
5657
}
5758

5859
func (c *Client) Close(ctx context.Context) error {
@@ -369,10 +370,10 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
369370
go func() {
370371
defer close(deployProgressChan)
371372

372-
AccountLoop:
373+
accountLoop:
373374
for i, account := range accounts {
374375
if checkContextCanceled(account.Id, deployProgress) {
375-
continue AccountLoop
376+
continue accountLoop
376377
}
377378

378379
deployProgress.Accounts[account.Id].Status = "deploying"
@@ -382,7 +383,7 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
382383
for _i := range 5 {
383384
time.Sleep(time.Millisecond * 100)
384385
if checkContextCanceled(account.Id, deployProgress) {
385-
continue AccountLoop
386+
continue accountLoop
386387
}
387388
deployProgress.Accounts[account.Id].Progress = float64(_i+1) / 10
388389
deployProgressChan <- deployProgress
@@ -401,13 +402,16 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
401402
for _i := range 5 {
402403
time.Sleep(time.Millisecond * 100)
403404
if checkContextCanceled(account.Id, deployProgress) {
404-
continue AccountLoop
405+
continue accountLoop
405406
}
406407
deployProgress.Accounts[account.Id].Progress = float64(_i+6) / 10
407408
deployProgressChan <- deployProgress
408409
}
409410

410-
c.accounts[_i].DeployCache = c.accountDeployCache(account, deployDatas[i])
411+
// simulate deploying data to remote
412+
c.remoteStates[account.Id] = c.accountDeployCache(account, deployDatas[i])
413+
414+
c.accounts[_i].DeployCache = c.remoteStates[account.Id]
411415
deployProgress.Accounts[account.Id].Status = "finished"
412416
deployProgress.Accounts[account.Id].Progress = 1
413417
deployProgressChan <- deployProgress
@@ -457,16 +461,39 @@ func (c *Client) VerifyAccounts(ctx context.Context, accountIds ...client.Accoun
457461
}),
458462
}
459463

464+
checkContextCanceled := func(accountId client.AccountId, deployProgress client.DeployProgressAccounts) bool {
465+
if ctx.Err() != nil {
466+
deployProgress.Accounts[accountId].Progress = 1
467+
if errors.Is(ctx.Err(), context.Canceled) {
468+
deployProgress.Accounts[accountId].Status = "canceled"
469+
deployProgress.Accounts[accountId].Err = errors.New("canceled")
470+
} else {
471+
deployProgress.Accounts[accountId].Status = "error"
472+
deployProgress.Accounts[accountId].Err = ctx.Err()
473+
}
474+
return true
475+
}
476+
return false
477+
}
478+
460479
go func() {
461480
defer close(verifyProgressChan)
462481

482+
accountLoop:
463483
for i, account := range accounts {
484+
if checkContextCanceled(account.Id, verifyProgress) {
485+
continue accountLoop
486+
}
487+
464488
verifyProgress.Accounts[account.Id].Status = "verifing"
465489
verifyProgressChan <- verifyProgress
466490

467491
// simulate deplay
468492
for _i := range 5 {
469493
time.Sleep(time.Millisecond * 100)
494+
if checkContextCanceled(account.Id, verifyProgress) {
495+
continue accountLoop
496+
}
470497
verifyProgress.Accounts[account.Id].Progress = float64(_i+1) / 10
471498
verifyProgressChan <- verifyProgress
472499
}
@@ -483,15 +510,18 @@ func (c *Client) VerifyAccounts(ctx context.Context, accountIds ...client.Accoun
483510
// simulate deplay
484511
for _i := range 5 {
485512
time.Sleep(time.Millisecond * 100)
513+
if checkContextCanceled(account.Id, verifyProgress) {
514+
continue accountLoop
515+
}
486516
verifyProgress.Accounts[account.Id].Progress = float64(_i+6) / 10
487517
verifyProgressChan <- verifyProgress
488518
}
489519

490-
// simulate getting deployCache from remote
491-
deployCache := c.accountDeployCache(account, deployDatas[i])
520+
// simulate getting remoteState from remote
521+
remoteState, hasState := c.remoteStates[account.Id]
492522

493-
if c.accountDeployCache(account, deployDatas[i]) != deployCache {
494-
c.accounts[_i].DeployCache = deployCache // update local DeployCache to reflect the remotes state
523+
if !hasState || c.accountDeployCache(account, deployDatas[i]) != remoteState {
524+
c.accounts[_i].DeployCache = remoteState // update local DeployCache to reflect the remotes state
495525
verifyProgress.Accounts[account.Id].Status = "error"
496526
verifyProgress.Accounts[account.Id].Err = errors.New("account is out of sync")
497527
} else {
@@ -501,6 +531,7 @@ func (c *Client) VerifyAccounts(ctx context.Context, accountIds ...client.Accoun
501531
verifyProgress.Accounts[account.Id].Progress = 1
502532
verifyProgressChan <- verifyProgress
503533
}
534+
verifyProgressChan <- verifyProgress
504535
}()
505536

506537
return verifyProgressChan, nil

ui/tui/models/helpers/deploy/deploy.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func DeployAll(ctx context.Context, c client.Client) tea.Cmd {
2121
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
2222
}
2323

24-
return DeployMany(ctx, c, accounts...)
24+
return Deploy(ctx, c, accounts...)
2525
}
2626

2727
func DeployDirty(ctx context.Context, c client.Client) tea.Cmd {
@@ -30,12 +30,10 @@ func DeployDirty(ctx context.Context, c client.Client) tea.Cmd {
3030
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
3131
}
3232

33-
return DeployMany(ctx, c, accounts...)
33+
return Deploy(ctx, c, accounts...)
3434
}
3535

36-
// func DeployOne(ctx context.Context, c client.Client, ids ...client.AccountId) tea.Cmd
37-
38-
func DeployMany(ctx context.Context, c client.Client, accounts ...client.Account) tea.Cmd {
36+
func Deploy(ctx context.Context, c client.Client, accounts ...client.Account) tea.Cmd {
3937
if len(accounts) == 0 {
4038
return popupviews.OpenMessage(popupviews.MessageInfo, "No Accounts found for deployment.", nil)
4139
}

ui/tui/models/helpers/deploy/verify.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func VerifyAll(ctx context.Context, c client.Client) tea.Cmd {
2121
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
2222
}
2323

24-
return VerifyMany(ctx, c, accounts...)
24+
return Verify(ctx, c, accounts...)
2525
}
2626

2727
func VerifyDirty(ctx context.Context, c client.Client) tea.Cmd {
@@ -30,30 +30,28 @@ func VerifyDirty(ctx context.Context, c client.Client) tea.Cmd {
3030
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
3131
}
3232

33-
return VerifyMany(ctx, c, accounts...)
33+
return Verify(ctx, c, accounts...)
3434
}
3535

36-
// func VerifyOne(ctx context.Context, c client.Client, ids ...client.AccountId) tea.Cmd
37-
38-
func VerifyMany(ctx context.Context, c client.Client, accounts ...client.Account) tea.Cmd {
36+
func Verify(ctx context.Context, c client.Client, accounts ...client.Account) tea.Cmd {
3937
if len(accounts) == 0 {
40-
return popupviews.OpenMessage(popupviews.MessageInfo, "No Accounts found for verifyment.", nil)
38+
return popupviews.OpenMessage(popupviews.MessageInfo, "No Accounts found for verify.", nil)
4139
}
4240

4341
ids := slicest.Map(accounts, func(account client.Account) client.AccountId { return account.Id })
4442
accountNamesMap := slicest.ToMap(accounts, func(account client.Account) (client.AccountId, string) { return account.Id, account.String() })
4543
accountNamesWidth := slicest.Reduce(slicest.MapValues(accountNamesMap), func(accountName string, width int) int { return max(width, len(accountName)) })
4644
accountNameRenderer := lipgloss.NewStyle().Width(accountNamesWidth)
4745

48-
dpc, err := c.VerifyAccounts(ctx, ids...)
49-
if err != nil {
50-
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
51-
}
52-
5346
return popupviews.OpenProgress(
5447
popupviews.ProgressBar,
5548
"Verifying Accounts",
56-
func(_ context.Context, pc popupviews.ProgressChan) tea.Cmd {
49+
func(ctx context.Context, pc popupviews.ProgressChan) tea.Cmd {
50+
dpc, err := c.VerifyAccounts(ctx, ids...)
51+
if err != nil {
52+
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
53+
}
54+
5755
var dp client.VerifyProgressAccounts
5856

5957
// map [client.VerifyProgressAccounts] chan to [popupviews.Progress] chan
@@ -79,7 +77,7 @@ func VerifyMany(ctx context.Context, c client.Client, accounts ...client.Account
7977
strings.Join(
8078
slicest.Map(ids, func(id client.AccountId) string {
8179
if dp.Accounts[id].Err != nil {
82-
return fmt.Sprintf("%s %s", accountNameRenderer.Render(accountNamesMap[id]), dp.Accounts[id].Err.Error())
80+
return fmt.Sprintf("%s Error: %s", accountNameRenderer.Render(accountNamesMap[id]), dp.Accounts[id].Err.Error())
8381
}
8482
return fmt.Sprintf("%s Success", accountNameRenderer.Render(accountNamesMap[id]))
8583
}),
@@ -88,5 +86,7 @@ func VerifyMany(ctx context.Context, c client.Client, accounts ...client.Account
8886
nil,
8987
)
9088
},
89+
popupviews.WithContext(ctx),
90+
popupviews.WithCancel(),
9191
)
9292
}

0 commit comments

Comments
 (0)