Skip to content

Commit

Permalink
chore: send notification after execution
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed Jul 31, 2024
1 parent 7864105 commit f516141
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
6 changes: 5 additions & 1 deletion internal/lpac/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"os/exec"
Expand Down Expand Up @@ -44,7 +45,10 @@ func (c *Cmd) Run(arguments []string, dst any, progress Progress) error {
if err := cmd.Wait(); err != nil {
slog.Error("command wait error", "error", err, "stderr", stderr.String())
}
return cmdErr
if cmdErr != nil {
return fmt.Errorf("%w %s", cmdErr, &stderr)
}
return nil
}

func (c *Cmd) process(output io.ReadCloser, input io.WriteCloser, dst any, progress Progress) error {
Expand Down
47 changes: 16 additions & 31 deletions internal/lpac/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lpac

import (
"errors"
"log/slog"
)

type ActivationCode struct {
Expand Down Expand Up @@ -69,12 +70,12 @@ func (c *Cmd) ProfileDownload(activationCode *ActivationCode, progress Progress)
arguments = append(arguments, "-i", activationCode.IMEI)
}

return c.sendNotificationAfterDownloading(func() error {
return c.sendNotificationAfterExecution(func() error {
return c.Run(arguments, nil, progress)
})
}, true)
}

func (c *Cmd) sendNotificationAfterDownloading(action func() error) error {
func (c *Cmd) sendNotificationAfterExecution(action func() error, remove bool) error {
notifications, err := c.NotificationList()
if err != nil {
return err
Expand All @@ -94,44 +95,28 @@ func (c *Cmd) sendNotificationAfterDownloading(action func() error) error {
if err != nil {
return err
}

var installationNotificationSeqNumber int
for _, notification := range notifications {
if notification.SeqNumber > lastSeqNumber && notification.ProfileManagementOperation == NotificationProfileManagementOperationInstall {
installationNotificationSeqNumber = notification.SeqNumber
break
if notification.SeqNumber > lastSeqNumber {
slog.Debug("processing notification", "seqNumber", notification.SeqNumber, "ICCID", notification.ICCID, "operation", notification.ProfileManagementOperation, "remove", remove)
if err := c.NotificationProcess(notification.SeqNumber, remove, nil); err != nil {
slog.Error("failed to process notification", "seqNumber", notification.SeqNumber, "ICCID", notification.ICCID, "operation", notification.ProfileManagementOperation, "remove", remove, "error", err)
return err
}
}
}
if installationNotificationSeqNumber > 0 {
return c.NotificationProcess(installationNotificationSeqNumber, true, nil)
}
return nil
}

func (c *Cmd) ProfileDelete(ICCID string) error {
if err := c.Run([]string{"profile", "delete", ICCID}, nil, nil); err != nil {
return err
}

notifications, err := c.NotificationList()
if err != nil {
return err
}
var deletionNotificationSeqNumber int
for _, notification := range notifications {
if notification.ICCID == ICCID && notification.ProfileManagementOperation == NotificationProfileManagementOperationDelete {
deletionNotificationSeqNumber = notification.SeqNumber
break
}
}
if deletionNotificationSeqNumber < 0 {
return ErrDeletionNotificationNotFound
}
return c.NotificationProcess(deletionNotificationSeqNumber, false, nil)
return c.sendNotificationAfterExecution(func() error {
return c.Run([]string{"profile", "delete", ICCID}, nil, nil)
}, false)
}

func (c *Cmd) ProfileEnable(ICCID string) error {
return c.Run([]string{"profile", "enable", ICCID}, nil, nil)
return c.sendNotificationAfterExecution(func() error {
return c.Run([]string{"profile", "enable", ICCID}, nil, nil)
}, true)
}

func (c *Cmd) ProfileSetNickname(ICCID string, nickname string) error {
Expand Down

0 comments on commit f516141

Please sign in to comment.