mirror of
https://github.com/majd/ipatool
synced 2026-11-26 17:27:52 +08:00
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/avast/retry-go"
|
|
"github.com/majd/ipatool/v2/pkg/appstore"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// nolint:wrapcheck
|
|
func purchaseCmd() *cobra.Command {
|
|
var bundleID string
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "purchase",
|
|
Short: "Obtain a license for the app from the App Store",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var lastErr error
|
|
var acc appstore.Account
|
|
|
|
return retry.Do(func() error {
|
|
infoResult, err := dependencies.AppStore.AccountInfo()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
acc = infoResult.Account
|
|
|
|
if errors.Is(lastErr, appstore.ErrPasswordTokenExpired) {
|
|
loginResult, err := dependencies.AppStore.Login(appstore.LoginInput{Email: acc.Email, Password: acc.Password})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
acc = loginResult.Account
|
|
}
|
|
|
|
lookupResult, err := dependencies.AppStore.Lookup(appstore.LookupInput{Account: acc, BundleID: bundleID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = dependencies.AppStore.Purchase(appstore.PurchaseInput{Account: acc, App: lookupResult.App})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dependencies.Logger.Log().Bool("success", true).Send()
|
|
|
|
return nil
|
|
},
|
|
retry.LastErrorOnly(true),
|
|
retry.DelayType(retry.FixedDelay),
|
|
retry.Delay(time.Millisecond),
|
|
retry.Attempts(2),
|
|
retry.RetryIf(func(err error) bool {
|
|
lastErr = err
|
|
|
|
return errors.Is(err, appstore.ErrPasswordTokenExpired)
|
|
}),
|
|
)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&bundleID, "bundle-identifier", "b", "", "Bundle identifier of the target iOS app (required)")
|
|
_ = cmd.MarkFlagRequired("bundle-identifier")
|
|
|
|
return cmd
|
|
}
|