@@ -49,59 +49,6 @@ func (c *Client) accountDeployCache(account client.Account, deployCache string)
4949 return fmt .Sprintf ("%s %s@%s:%d\n %s" , account .DeployMethod , account .Username , account .Host , account .Port , deployCache )
5050}
5151
52- func (c * Client ) deployAccounts (ctx context.Context , accounts ... client.Account ) (chan client.DeployProgress , error ) {
53- deployDatas , err := slicest .MapX (accounts , func (a client.Account ) (string , error ) {
54- return c .accountDeployData (ctx , a )
55- })
56- if err != nil {
57- return nil , err
58- }
59-
60- deployProgressChan := make (chan client.DeployProgress )
61- deployProgress := client.DeployProgress {
62- Accounts : slicest .ToMap (accounts , func (account client.Account ) (client.AccountId , * client.DeployAccountProgress ) {
63- return account .Id , & client.DeployAccountProgress {0 , "not started" , nil }
64- }),
65- }
66-
67- go func () {
68- for i , account := range accounts {
69- deployProgress .Accounts [account .Id ].Status = "deploying"
70- deployProgressChan <- deployProgress
71-
72- // simulate deplay
73- for _i := range 5 {
74- time .Sleep (time .Millisecond * 100 )
75- deployProgress .Accounts [account .Id ].Progress = float64 (_i + 1 ) / 10
76- deployProgressChan <- deployProgress
77- }
78-
79- _i , ok := slices .BinarySearchFunc (c .accounts , account .Id , func (a client.Account , id client.AccountId ) int { return int (a .Id - id ) })
80- if ! ok {
81- deployProgress .Accounts [account .Id ].Status = "error"
82- deployProgress .Accounts [account .Id ].Progress = 1
83- deployProgress .Accounts [account .Id ].Err = fmt .Errorf ("account with id %v not found" , account .Id )
84- deployProgressChan <- deployProgress
85- continue
86- }
87-
88- // simulate deplay
89- for _i := range 5 {
90- time .Sleep (time .Millisecond * 100 )
91- deployProgress .Accounts [account .Id ].Progress = float64 (_i + 6 ) / 10
92- deployProgressChan <- deployProgress
93- }
94-
95- c .accounts [_i ].DeployCache = c .accountDeployCache (account , deployDatas [i ])
96- deployProgress .Accounts [account .Id ].Status = "finished"
97- deployProgress .Accounts [account .Id ].Progress = 1
98- deployProgressChan <- deployProgress
99- }
100- }()
101-
102- return deployProgressChan , nil
103- }
104-
10552// --- Lifecycle & Initialization ---
10653
10754func NewClient () * Client {
@@ -237,9 +184,29 @@ func (c *Client) GetAccount(ctx context.Context, id client.AccountId) (client.Ac
237184}
238185
239186func (c * Client ) GetAccounts (ctx context.Context , ids ... client.AccountId ) ([]client.Account , error ) {
240- return slicest .Filter (c .accounts , func (account client.Account ) bool {
187+ accounts := slicest .Filter (c .accounts , func (account client.Account ) bool {
241188 return slices .Contains (ids , account .Id )
242- }), nil
189+ })
190+
191+ if len (accounts ) != len (ids ) {
192+ return nil , fmt .Errorf (
193+ "accounts with the following ids could not be found: %s" ,
194+ strings .Join (
195+ slicest .Map (
196+ slicest .Filter (ids , func (id client.AccountId ) bool {
197+ _ , ok := slices .BinarySearchFunc (accounts , id , func (account client.Account , id client.AccountId ) int {
198+ return int (account .Id - id )
199+ })
200+ return ! ok
201+ }),
202+ func (id client.AccountId ) string { return fmt .Sprint (id ) },
203+ ),
204+ ", " ,
205+ ),
206+ )
207+ }
208+
209+ return accounts , nil
243210}
244211
245212func (c * Client ) ListAccounts (ctx context.Context ) ([]client.Account , error ) {
@@ -359,24 +326,81 @@ func (c *Client) DecommisionAccount(ctx context.Context, id client.AccountId) (c
359326 return nil , errors .New ("client.DecommisionAccount not implemented" )
360327}
361328
362- func (c * Client ) DeployPublicKeys (ctx context.Context , publicKeyId ... client.PublicKeyId ) (chan client.DeployProgress , error ) {
363- return nil , errors .New ("client.DeployPublicKeys not implemented" )
329+ func (c * Client ) DeployAccount (ctx context.Context , accountId client.AccountId ) (chan client.DeployProgressAccount , error ) {
330+ dpc , err := c .DeployAccounts (ctx , accountId )
331+ if err != nil {
332+ return nil , err
333+ }
334+
335+ // convert channel to only report the single accounts progress
336+ dbac := make (chan client.DeployProgressAccount )
337+ go func () {
338+ defer close (dbac )
339+
340+ for dp := range dpc {
341+ dbac <- * dp .Accounts [accountId ]
342+ }
343+ }()
344+
345+ return dbac , nil
364346}
365347
366- func (c * Client ) DeployAccounts (ctx context.Context , accountIds ... client.AccountId ) (chan client.DeployProgress , error ) {
348+ func (c * Client ) DeployAccounts (ctx context.Context , accountIds ... client.AccountId ) (chan client.DeployProgressAccounts , error ) {
367349 accounts , err := c .GetAccounts (ctx , accountIds ... )
368350 if err != nil {
369351 return nil , err
370352 }
371353
372- return c .deployAccounts (ctx , accounts ... )
373- }
374-
375- func (c * Client ) DeployAll (ctx context.Context ) (chan client.DeployProgress , error ) {
376- accounts , err := c .ListAccounts (ctx )
354+ deployDatas , err := slicest .MapX (accounts , func (a client.Account ) (string , error ) {
355+ return c .accountDeployData (ctx , a )
356+ })
377357 if err != nil {
378358 return nil , err
379359 }
380360
381- return c .deployAccounts (ctx , accounts ... )
361+ deployProgressChan := make (chan client.DeployProgressAccounts )
362+ deployProgress := client.DeployProgressAccounts {
363+ Accounts : slicest .ToMap (accounts , func (account client.Account ) (client.AccountId , * client.DeployProgressAccount ) {
364+ return account .Id , & client.DeployProgressAccount {0 , "not started" , nil }
365+ }),
366+ }
367+
368+ go func () {
369+ defer close (deployProgressChan )
370+
371+ for i , account := range accounts {
372+ deployProgress .Accounts [account .Id ].Status = "deploying"
373+ deployProgressChan <- deployProgress
374+
375+ // simulate deplay
376+ for _i := range 5 {
377+ time .Sleep (time .Millisecond * 100 )
378+ deployProgress .Accounts [account .Id ].Progress = float64 (_i + 1 ) / 10
379+ deployProgressChan <- deployProgress
380+ }
381+
382+ _i , ok := slices .BinarySearchFunc (c .accounts , account .Id , func (a client.Account , id client.AccountId ) int { return int (a .Id - id ) })
383+ if ! ok {
384+ deployProgress .Accounts [account .Id ].Status = "error"
385+ deployProgress .Accounts [account .Id ].Progress = 1
386+ deployProgress .Accounts [account .Id ].Err = fmt .Errorf ("account with id %v not found" , account .Id )
387+ deployProgressChan <- deployProgress
388+ continue
389+ }
390+
391+ // simulate deplay
392+ for _i := range 5 {
393+ time .Sleep (time .Millisecond * 100 )
394+ deployProgress .Accounts [account .Id ].Progress = float64 (_i + 6 ) / 10
395+ deployProgressChan <- deployProgress
396+ }
397+
398+ c .accounts [_i ].DeployCache = c .accountDeployCache (account , deployDatas [i ])
399+ deployProgress .Accounts [account .Id ].Status = "finished"
400+ deployProgress .Accounts [account .Id ].Progress = 1
401+ deployProgressChan <- deployProgress
402+ }
403+ }()
404+
405+ return deployProgressChan , nil
382406}
0 commit comments