mirror of
https://github.com/majd/ipatool
synced 2026-11-28 18:27:52 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74e7588c6d | ||
|
|
67e557e9a7 | ||
|
|
bf33e0d1c4 | ||
|
|
95c3aa6629 | ||
|
|
cf926d7911 | ||
|
|
9a361cf7a0 | ||
|
|
574d5b5148 | ||
|
|
5abc5d1777 | ||
|
|
8762eaf192 | ||
|
|
4f3115d60f | ||
|
|
0e38e43d8b | ||
|
|
73e9bd90d1 | ||
|
|
e76dbf044e | ||
|
|
d82e819d4e | ||
|
|
19ac47d098 | ||
|
|
fece9ea79c | ||
|
|
1b31ee6556 | ||
|
|
fbd9d74729 | ||
|
|
8d2467491e | ||
|
|
4eba914bff | ||
|
|
56cc64fcef | ||
|
|
7813e78e3c | ||
|
|
50a1e546ef | ||
|
|
094ff93693 | ||
|
|
5d014266e3 | ||
|
|
8dd1a8508d |
No files matched your search
@@ -1 +0,0 @@
|
||||
github: majd
|
||||
@@ -6,13 +6,12 @@ on:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
build_windows:
|
||||
name: Build for Windows
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ darwin, linux, windows ]
|
||||
arch: [ arm64, amd64 ]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@@ -20,10 +19,43 @@ jobs:
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: ./tools/version.sh
|
||||
- run: go build -o ipatool-${{ matrix.os }}-${{ matrix.arch }}
|
||||
- run: go build -o ipatool-$GOOS-$GOARCH.exe
|
||||
env:
|
||||
GOOS: ${{ matrix.os }}
|
||||
GOOS: windows
|
||||
GOARCH: ${{ matrix.arch }}
|
||||
build_linux:
|
||||
name: Build for Linux
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
arch: [ arm64, amd64 ]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: go build -o ipatool-$GOOS-$GOARCH
|
||||
env:
|
||||
GOOS: linux
|
||||
GOARCH: ${{ matrix.arch }}
|
||||
build_macos:
|
||||
name: Build for macOS
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
arch: [ arm64, amd64 ]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: go build -o ipatool-$GOOS-$GOARCH
|
||||
env:
|
||||
GOOS: darwin
|
||||
GOARCH: ${{ matrix.arch }}
|
||||
CGO_CFLAGS: -mmacosx-version-min=10.15
|
||||
CGO_LDFLAGS: -mmacosx-version-min=10.15
|
||||
@@ -11,27 +11,23 @@ jobs:
|
||||
runs-on: macos-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ darwin, linux, windows ]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: ./tools/version.sh
|
||||
- run: go build -o ipatool-${{ matrix.os }}
|
||||
- run: go build -o ipatool
|
||||
env:
|
||||
GOOS: ${{ matrix.os }}
|
||||
CGO_CFLAGS: -mmacosx-version-min=10.15
|
||||
CGO_LDFLAGS: -mmacosx-version-min=10.15
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ipatool-${{ matrix.os }}
|
||||
path: ipatool-${{ matrix.os }}
|
||||
name: ipatool
|
||||
path: ipatool
|
||||
if-no-files-found: error
|
||||
test_macos:
|
||||
name: Test (macOS)
|
||||
test:
|
||||
name: Test
|
||||
runs-on: macos-latest
|
||||
needs: [build]
|
||||
strategy:
|
||||
@@ -41,37 +37,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: ipatool-darwin
|
||||
name: ipatool
|
||||
path: build
|
||||
- run: chmod +x ./build/ipatool-darwin
|
||||
- run: ./build/ipatool-darwin ${{ matrix.command }} --help
|
||||
test_linux:
|
||||
name: Test (Linux)
|
||||
runs-on: ubuntu-latest
|
||||
needs: [build]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
command: [auth, download, purchase, search]
|
||||
steps:
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: ipatool-linux
|
||||
path: build
|
||||
- run: chmod +x ./build/ipatool-linux
|
||||
- run: ./build/ipatool-linux ${{ matrix.command }} --help
|
||||
test_windows:
|
||||
name: Test (Windows)
|
||||
runs-on: windows-latest
|
||||
needs: [build]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
command: [auth, download, purchase, search]
|
||||
steps:
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: ipatool-windows
|
||||
path: build
|
||||
- run: chmod +x ./build/ipatool-windows
|
||||
- run: ./build/ipatool-windows ${{ matrix.command }} --help
|
||||
- run: chmod +x ./build/ipatool
|
||||
- run: ./build/ipatool ${{ matrix.command }} --help
|
||||
@@ -0,0 +1,21 @@
|
||||
name: Lint
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: go generate github.com/majd/ipatool/...
|
||||
- uses: golangci/golangci-lint-action@v3
|
||||
with:
|
||||
version: v1.52.2
|
||||
@@ -23,7 +23,7 @@ jobs:
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: ./tools/version.sh
|
||||
- run: go generate github.com/majd/ipatool/...
|
||||
- run: go test -v github.com/majd/ipatool/...
|
||||
build:
|
||||
name: Build
|
||||
@@ -35,46 +35,51 @@ jobs:
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: ./tools/version.sh
|
||||
- run: go build -o ipatool-${{ needs.get_version.outputs.version }}-windows-arm64
|
||||
- run: go build -ldflags="-X github.com/majd/ipatool/v2/cmd.version=$VERSION" -o ipatool-$VERSION-windows-arm64.exe
|
||||
env:
|
||||
GOOS: windows
|
||||
GOARCH: arm64
|
||||
- run: go build -o ipatool-${{ needs.get_version.outputs.version }}-windows-amd64
|
||||
VERSION: ${{ needs.get_version.outputs.version }}
|
||||
- run: go build -ldflags="-X github.com/majd/ipatool/v2/cmd.version=$VERSION" -o ipatool-$VERSION-windows-amd64.exe
|
||||
env:
|
||||
GOOS: windows
|
||||
GOARCH: amd64
|
||||
- run: go build -o ipatool-${{ needs.get_version.outputs.version }}-linux-arm64
|
||||
VERSION: ${{ needs.get_version.outputs.version }}
|
||||
- run: go build -ldflags="-X github.com/majd/ipatool/v2/cmd.version=$VERSION" -o ipatool-$VERSION-linux-arm64
|
||||
env:
|
||||
GOOS: linux
|
||||
GOARCH: arm64
|
||||
- run: go build -o ipatool-${{ needs.get_version.outputs.version }}-linux-amd64
|
||||
VERSION: ${{ needs.get_version.outputs.version }}
|
||||
- run: go build -ldflags="-X github.com/majd/ipatool/v2/cmd.version=$VERSION" -o ipatool-$VERSION-linux-amd64
|
||||
env:
|
||||
GOOS: linux
|
||||
GOARCH: amd64
|
||||
- run: go build -o ipatool-${{ needs.get_version.outputs.version }}-macos-arm64
|
||||
VERSION: ${{ needs.get_version.outputs.version }}
|
||||
- run: go build -ldflags="-X github.com/majd/ipatool/v2/cmd.version=$VERSION" -o ipatool-$VERSION-macos-arm64
|
||||
env:
|
||||
GOOS: darwin
|
||||
GOARCH: arm64
|
||||
VERSION: ${{ needs.get_version.outputs.version }}
|
||||
CGO_CFLAGS: -mmacosx-version-min=10.15
|
||||
CGO_LDFLAGS: -mmacosx-version-min=10.15
|
||||
CGO_ENABLED: 1
|
||||
- run: go build -o ipatool-${{ needs.get_version.outputs.version }}-macos-amd64
|
||||
- run: go build -ldflags="-X github.com/majd/ipatool/v2/cmd.version=$VERSION" -o ipatool-$VERSION-macos-amd64
|
||||
env:
|
||||
GOOS: darwin
|
||||
GOARCH: amd64
|
||||
VERSION: ${{ needs.get_version.outputs.version }}
|
||||
CGO_CFLAGS: -mmacosx-version-min=10.15
|
||||
CGO_LDFLAGS: -mmacosx-version-min=10.15
|
||||
CGO_ENABLED: 1
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-windows-arm64
|
||||
path: ipatool-${{ needs.get_version.outputs.version }}-windows-arm64
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-windows-arm64.exe
|
||||
path: ipatool-${{ needs.get_version.outputs.version }}-windows-arm64.exe
|
||||
if-no-files-found: error
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-windows-amd64
|
||||
path: ipatool-${{ needs.get_version.outputs.version }}-windows-amd64
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-windows-amd64.exe
|
||||
path: ipatool-${{ needs.get_version.outputs.version }}-windows-amd64.exe
|
||||
if-no-files-found: error
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
@@ -96,37 +101,68 @@ jobs:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-macos-amd64
|
||||
path: ipatool-${{ needs.get_version.outputs.version }}-macos-amd64
|
||||
if-no-files-found: error
|
||||
release_windows_linux:
|
||||
name: Release (Windows & Linux)
|
||||
release_windows:
|
||||
name: Release for Windows
|
||||
runs-on: ubuntu-latest
|
||||
needs: [get_version, build]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ linux, windows ]
|
||||
arch: [ arm64, amd64 ]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-${{ matrix.os }}-${{ matrix.arch }}
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-windows-${{ matrix.arch }}.exe
|
||||
path: bin
|
||||
- run: tar -czvf $BIN.tar.gz bin/$BIN
|
||||
- run: tar -czvf $FILE.tar.gz bin/$FILE.exe
|
||||
env:
|
||||
BIN: ipatool-${{ needs.get_version.outputs.version }}-${{ matrix.os }}-${{ matrix.arch }}
|
||||
FILE: ipatool-${{ needs.get_version.outputs.version }}-windows-${{ matrix.arch }}
|
||||
- run: ./tools/sha256sum.sh $TARBALL > $TARBALL.sha256sum
|
||||
env:
|
||||
TARBALL: ipatool-${{ needs.get_version.outputs.version }}-windows-${{ matrix.arch }}.tar.gz
|
||||
- uses: svenstaro/upload-release-action@v2
|
||||
with:
|
||||
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
file: ipatool-${{ needs.get_version.outputs.version }}-${{ matrix.os }}-${{ matrix.arch }}.*
|
||||
file: ipatool-${{ needs.get_version.outputs.version }}-windows-${{ matrix.arch }}.*
|
||||
tag: ${{ github.ref }}
|
||||
overwrite: false
|
||||
file_glob: true
|
||||
release_linux:
|
||||
name: Release for Linux
|
||||
runs-on: ubuntu-latest
|
||||
needs: [get_version, build, release_windows]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
arch: [ arm64, amd64 ]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-linux-${{ matrix.arch }}
|
||||
path: bin
|
||||
- run: tar -czvf $FILE.tar.gz bin/$FILE
|
||||
env:
|
||||
FILE: ipatool-${{ needs.get_version.outputs.version }}-linux-${{ matrix.arch }}
|
||||
- run: ./tools/sha256sum.sh $TARBALL > $TARBALL.sha256sum
|
||||
env:
|
||||
TARBALL: ipatool-${{ needs.get_version.outputs.version }}-linux-${{ matrix.arch }}.tar.gz
|
||||
- uses: svenstaro/upload-release-action@v2
|
||||
with:
|
||||
repo_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
file: ipatool-${{ needs.get_version.outputs.version }}-linux-${{ matrix.arch }}.*
|
||||
tag: ${{ github.ref }}
|
||||
overwrite: false
|
||||
file_glob: true
|
||||
release_macos:
|
||||
name: Release (macOS)
|
||||
name: Release for macOS
|
||||
runs-on: ubuntu-latest
|
||||
needs: [get_version, build, release_windows_linux]
|
||||
needs: [get_version, build, release_windows, release_linux]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
path: ./ipatool
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-macos-arm64
|
||||
@@ -138,13 +174,13 @@ jobs:
|
||||
with:
|
||||
name: ipatool-${{ needs.get_version.outputs.version }}-macos-amd64
|
||||
path: bin
|
||||
- run: tar -czvf $BIN.tar.gz bin/$BIN && rm -rf bin/
|
||||
- run: tar -czvf $FILE.tar.gz bin/$FILE && rm -rf bin/
|
||||
env:
|
||||
BIN: ipatool-${{ needs.get_version.outputs.version }}-macos-amd64
|
||||
FILE: ipatool-${{ needs.get_version.outputs.version }}-macos-amd64
|
||||
- id: sha256
|
||||
run: |
|
||||
SHA256_ARM64=$(./tools/sha256sum.sh ipatool-${{ needs.get_version.outputs.version }}-macos-arm64.tar.gz)
|
||||
SHA256_AMD64=$(./tools/sha256sum.sh ipatool-${{ needs.get_version.outputs.version }}-macos-amd64.tar.gz)
|
||||
SHA256_ARM64=$(./ipatool/tools/sha256sum.sh ipatool-${{ needs.get_version.outputs.version }}-macos-arm64.tar.gz)
|
||||
SHA256_AMD64=$(./ipatool/tools/sha256sum.sh ipatool-${{ needs.get_version.outputs.version }}-macos-amd64.tar.gz)
|
||||
echo $SHA256_ARM64 > ipatool-${{ needs.get_version.outputs.version }}-macos-arm64.tar.gz.sha256sum
|
||||
echo $SHA256_AMD64 > ipatool-${{ needs.get_version.outputs.version }}-macos-amd64.tar.gz.sha256sum
|
||||
echo ::set-output name=sha256_arm64::$SHA256_ARM64
|
||||
@@ -156,24 +192,18 @@ jobs:
|
||||
tag: ${{ github.ref }}
|
||||
overwrite: false
|
||||
file_glob: true
|
||||
outputs:
|
||||
sha256_arm64: ${{ steps.sha256.outputs.sha256_arm64 }}
|
||||
sha256_amd64: ${{ steps.sha256.outputs.sha256_amd64 }}
|
||||
update_cask:
|
||||
name: Update cask
|
||||
runs-on: ubuntu-latest
|
||||
needs: [get_version, release_macos]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
repository: ${{ secrets.HOMEBREW_REPO }}
|
||||
ref: main
|
||||
token: ${{ secrets.GH_TOKEN }}
|
||||
path: homebrew-repo
|
||||
- run: |
|
||||
sed -i "3s/.*/ sha256 \"${{ needs.release_macos.outputs.sha256_arm64 }}\"/" Casks/ipatool.rb
|
||||
cd homebrew-repo
|
||||
sed -i "3s/.*/ sha256 \"$SHA256_ARM64\"/" Casks/ipatool.rb
|
||||
sed -i "4s/.*/ url \"https:\/\/github.com\/majd\/ipatool\/releases\/download\/v${{ needs.get_version.outputs.version }}\/ipatool-${{ needs.get_version.outputs.version }}-macos-arm64.tar.gz\"/" Casks/ipatool.rb
|
||||
sed -i "5s/.*/ binary \"bin\/ipatool-${{ needs.get_version.outputs.version }}-macos-arm64\", target: \"ipatool\"/" Casks/ipatool.rb
|
||||
sed -i "7s/.*/ sha256 \"${{ needs.release_macos.outputs.sha256_amd64 }}\"/" Casks/ipatool.rb
|
||||
sed -i "7s/.*/ sha256 \"$SHA256_AMD64\"/" Casks/ipatool.rb
|
||||
sed -i "8s/.*/ url \"https:\/\/github.com\/majd\/ipatool\/releases\/download\/v${{ needs.get_version.outputs.version }}\/ipatool-${{ needs.get_version.outputs.version }}-macos-amd64.tar.gz\"/" Casks/ipatool.rb
|
||||
sed -i "9s/.*/ binary \"bin\/ipatool-${{ needs.get_version.outputs.version }}-macos-amd64\", target: \"ipatool\"/" Casks/ipatool.rb
|
||||
sed -i "12s/.*/ version \"${{ needs.get_version.outputs.version }}\"/" Casks/ipatool.rb
|
||||
@@ -181,4 +211,7 @@ jobs:
|
||||
git config --local user.email ${{ secrets.GH_EMAIL }}
|
||||
git add Casks/ipatool.rb
|
||||
git commit -m "Update ipatool to v${{ needs.get_version.outputs.version }}"
|
||||
git push "https://${{ secrets.GH_TOKEN }}@github.com/${{ secrets.HOMEBREW_REPO }}.git" --set-upstream "main"
|
||||
git push "https://${{ secrets.GH_TOKEN }}@github.com/${{ secrets.HOMEBREW_REPO }}.git" --set-upstream "main"
|
||||
env:
|
||||
SHA256_ARM64: ${{ steps.sha256.outputs.sha256_arm64 }}
|
||||
SHA256_AMD64: ${{ steps.sha256.outputs.sha256_amd64 }}
|
||||
@@ -8,12 +8,12 @@ on:
|
||||
jobs:
|
||||
run_tests:
|
||||
name: Run tests
|
||||
runs-on: macos-latest
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: '1.19.3'
|
||||
cache: true
|
||||
- run: ./tools/version.sh
|
||||
- run: go generate github.com/majd/ipatool/...
|
||||
- run: go test -v github.com/majd/ipatool/...
|
||||
+1
-1
@@ -3,4 +3,4 @@
|
||||
.LSOverride
|
||||
.vscode/
|
||||
.idea/
|
||||
cmd/version.go
|
||||
**/*_mock.go
|
||||
@@ -0,0 +1,22 @@
|
||||
linters:
|
||||
enable:
|
||||
- errcheck
|
||||
- govet
|
||||
- goimports
|
||||
- gofmt
|
||||
- unused
|
||||
- ginkgolinter
|
||||
- godot
|
||||
- godox
|
||||
- importas
|
||||
- nlreturn
|
||||
- nonamedreturns
|
||||
- prealloc
|
||||
- predeclared
|
||||
- tenv
|
||||
- unconvert
|
||||
- unparam
|
||||
- usestdlibvars
|
||||
- wastedassign
|
||||
- wrapcheck
|
||||
- wsl
|
||||
+18
-1
@@ -1,5 +1,22 @@
|
||||
## Changelog
|
||||
|
||||
### Version [2.0.3](https://github.com/majd/ipatool/releases/tag/v2.0.3)
|
||||
|
||||
- Added support for downloading Apple Arcade games.
|
||||
- Fixed windows builds not having exe extension.
|
||||
|
||||
|
||||
### Version [2.0.2](https://github.com/majd/ipatool/releases/tag/v2.0.2)
|
||||
|
||||
- Fixed Sinf patches when the app contains an Apple Watch app.
|
||||
- Added storefront code for Albania.
|
||||
- Fixed default output path to be current working directory.
|
||||
|
||||
### Version [2.0.1](https://github.com/majd/ipatool/releases/tag/v2.0.1)
|
||||
|
||||
- Linux & Windows releases now include the sha256 hash.
|
||||
- Added storefront code for Mongolia.
|
||||
|
||||
### Version [2.0.0](https://github.com/majd/ipatool/releases/tag/v2.0.0)
|
||||
|
||||
- Added support for Windows.
|
||||
@@ -8,7 +25,7 @@
|
||||
- Implemented new `auth info` command.
|
||||
- Implemented `--verbose` flag that replaces the `--debug-level` flag to enable verbose logging.
|
||||
- Implemented `--format` which allows specifying logs output format to either text or json (default: text).
|
||||
- Implemented flag `--non-interactive` flag to disable running the tool in an interactive session.
|
||||
- Implemented `--non-interactive` flag to disable running the tool in an interactive session.
|
||||
- The relevant command (i.e. purchase) will now automatically determine the country and the device family from the authenticated account. The following flags have been deprecated.
|
||||
- `--country`
|
||||
- `--device-family`
|
||||
|
||||
@@ -129,9 +129,10 @@ The tool can be compiled using the Go toolchain.
|
||||
$ go build -o ipatool
|
||||
```
|
||||
|
||||
Unit tests can be executing with the following command.
|
||||
Unit tests can be executed with the following commands.
|
||||
|
||||
```shell
|
||||
$ go generate github.com/majd/ipatool/...
|
||||
$ go test -v github.com/majd/ipatool/...
|
||||
```
|
||||
|
||||
|
||||
+109
-18
@@ -1,12 +1,20 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
var keychainPassphrase string
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/avast/retry-go"
|
||||
"github.com/majd/ipatool/v2/pkg/appstore"
|
||||
"github.com/majd/ipatool/v2/pkg/util"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func authCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
@@ -26,20 +34,93 @@ func authCmd() *cobra.Command {
|
||||
}
|
||||
|
||||
func loginCmd() *cobra.Command {
|
||||
var email string
|
||||
var password string
|
||||
var authCode string
|
||||
promptForAuthCode := func() (string, error) {
|
||||
authCode, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read string: %w", err)
|
||||
}
|
||||
|
||||
authCode = strings.Trim(authCode, "\n")
|
||||
authCode = strings.Trim(authCode, "\r")
|
||||
|
||||
return authCode, nil
|
||||
}
|
||||
|
||||
var email, password, authCode string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "login",
|
||||
Short: "Login to the App Store",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
appstore, err := newAppStore(cmd, keychainPassphrase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create appstore client")
|
||||
interactive := cmd.Context().Value("interactive").(bool)
|
||||
|
||||
if password == "" && !interactive {
|
||||
return errors.New("password is required when not running in interactive mode; use the \"--password\" flag")
|
||||
}
|
||||
|
||||
return appstore.Login(email, password, authCode)
|
||||
if password == "" && interactive {
|
||||
dependencies.Logger.Log().Msg("enter password:")
|
||||
|
||||
bytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read password: %w", err)
|
||||
}
|
||||
password = string(bytes)
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
|
||||
// nolint:wrapcheck
|
||||
return retry.Do(func() error {
|
||||
if errors.Is(lastErr, appstore.ErrAuthCodeRequired) && interactive {
|
||||
dependencies.Logger.Log().Msg("enter 2FA code:")
|
||||
|
||||
var err error
|
||||
authCode, err = promptForAuthCode()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read auth code: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
dependencies.Logger.Verbose().
|
||||
Str("password", password).
|
||||
Str("email", email).
|
||||
Str("authCode", util.IfEmpty(authCode, "<nil>")).
|
||||
Msg("logging in")
|
||||
|
||||
output, err := dependencies.AppStore.Login(appstore.LoginInput{
|
||||
Email: email,
|
||||
Password: password,
|
||||
AuthCode: authCode,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, appstore.ErrAuthCodeRequired) && !interactive {
|
||||
dependencies.Logger.Log().Msg("2FA code is required; run the command again and supply a code using the `--auth-code` flag")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
dependencies.Logger.Log().
|
||||
Str("name", output.Account.Name).
|
||||
Str("email", output.Account.Email).
|
||||
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.ErrAuthCodeRequired)
|
||||
}),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -52,32 +133,42 @@ func loginCmd() *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
// nolint:wrapcheck
|
||||
func infoCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "Show current account info",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
appstore, err := newAppStore(cmd, keychainPassphrase)
|
||||
output, err := dependencies.AppStore.AccountInfo()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create appstore client")
|
||||
return err
|
||||
}
|
||||
|
||||
return appstore.Info()
|
||||
dependencies.Logger.Log().
|
||||
Str("name", output.Account.Name).
|
||||
Str("email", output.Account.Email).
|
||||
Bool("success", true).
|
||||
Send()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:wrapcheck
|
||||
func revokeCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "revoke",
|
||||
Short: "Revoke your App Store credentials",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
appstore, err := newAppStore(cmd, keychainPassphrase)
|
||||
err := dependencies.AppStore.Revoke()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create appstore client")
|
||||
return err
|
||||
}
|
||||
|
||||
return appstore.Revoke()
|
||||
dependencies.Logger.Log().Bool("success", true).Send()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
+113
-130
@@ -1,37 +1,102 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/juju/persistent-cookiejar"
|
||||
"github.com/majd/ipatool/pkg/appstore"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/exp/slices"
|
||||
"golang.org/x/term"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/99designs/keyring"
|
||||
cookiejar "github.com/juju/persistent-cookiejar"
|
||||
"github.com/majd/ipatool/v2/pkg/appstore"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
"github.com/majd/ipatool/v2/pkg/log"
|
||||
"github.com/majd/ipatool/v2/pkg/util"
|
||||
"github.com/majd/ipatool/v2/pkg/util/machine"
|
||||
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/exp/slices"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func newCookieJar() (*cookiejar.Jar, error) {
|
||||
machine := util.NewMachine(util.MachineArgs{
|
||||
OperatingSystem: util.NewOperatingSystem(),
|
||||
})
|
||||
jar, err := cookiejar.New(&cookiejar.Options{
|
||||
Filename: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName, CookieJarFileName),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create cookie jar")
|
||||
}
|
||||
var dependencies = Dependencies{}
|
||||
var keychainPassphrase string
|
||||
|
||||
return jar, nil
|
||||
type Dependencies struct {
|
||||
Logger log.Logger
|
||||
OS operatingsystem.OperatingSystem
|
||||
Machine machine.Machine
|
||||
CookieJar http.CookieJar
|
||||
Keychain keychain.Keychain
|
||||
AppStore appstore.AppStore
|
||||
KeyringBackendType keyring.BackendType
|
||||
}
|
||||
|
||||
// newLogger returns a new logger instance.
|
||||
func newLogger(format OutputFormat, verbose bool) log.Logger {
|
||||
var writer io.Writer
|
||||
|
||||
switch format {
|
||||
case OutputFormatJSON:
|
||||
writer = zerolog.SyncWriter(os.Stdout)
|
||||
case OutputFormatText:
|
||||
writer = log.NewWriter()
|
||||
}
|
||||
|
||||
return log.NewLogger(log.Args{
|
||||
Verbose: verbose,
|
||||
Writer: writer,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// newCookieJar returns a new cookie jar instance.
|
||||
func newCookieJar(machine machine.Machine) http.CookieJar {
|
||||
return util.Must(cookiejar.New(&cookiejar.Options{
|
||||
Filename: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName, CookieJarFileName),
|
||||
}))
|
||||
}
|
||||
|
||||
// newKeychain returns a new keychain instance.
|
||||
func newKeychain(machine machine.Machine, logger log.Logger, backendType keyring.BackendType, interactive bool) keychain.Keychain {
|
||||
ring := util.Must(keyring.Open(keyring.Config{
|
||||
AllowedBackends: []keyring.BackendType{
|
||||
backendType,
|
||||
},
|
||||
ServiceName: KeychainServiceName,
|
||||
FileDir: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName),
|
||||
FilePasswordFunc: func(s string) (string, error) {
|
||||
if keychainPassphrase == "" && !interactive {
|
||||
return "", errors.New("keychain passphrase is required when not running in interactive mode; use the \"--keychain-passphrase\" flag")
|
||||
}
|
||||
|
||||
if keychainPassphrase != "" {
|
||||
return keychainPassphrase, nil
|
||||
}
|
||||
|
||||
path := strings.Split(s, " unlock ")[1]
|
||||
logger.Log().Msgf("enter passphrase to unlock %s (this is separate from your Apple ID password): ", path)
|
||||
bytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read password: %w", err)
|
||||
}
|
||||
|
||||
password := string(bytes)
|
||||
password = strings.Trim(password, "\n")
|
||||
password = strings.Trim(password, "\r")
|
||||
|
||||
return password, nil
|
||||
},
|
||||
}))
|
||||
|
||||
return keychain.New(keychain.Args{Keyring: ring})
|
||||
}
|
||||
|
||||
// keyringBackendType returns the backend type for the keyring.
|
||||
func keyringBackendType() keyring.BackendType {
|
||||
allowedBackends := []keyring.BackendType{
|
||||
keyring.KeychainBackend,
|
||||
@@ -47,123 +112,41 @@ func keyringBackendType() keyring.BackendType {
|
||||
return keyring.FileBackend
|
||||
}
|
||||
|
||||
func newKeyring(logger log.Logger, passphrase string, interactive bool) (keyring.Keyring, error) {
|
||||
machine := util.NewMachine(util.MachineArgs{
|
||||
OperatingSystem: util.NewOperatingSystem(),
|
||||
// initWithCommand initializes the dependencies of the command.
|
||||
func initWithCommand(cmd *cobra.Command) {
|
||||
verbose := cmd.Flag("verbose").Value.String() == "true"
|
||||
interactive, _ := cmd.Context().Value("interactive").(bool)
|
||||
format := util.Must(OutputFormatFromString(cmd.Flag("format").Value.String()))
|
||||
|
||||
dependencies.Logger = newLogger(format, verbose)
|
||||
dependencies.OS = operatingsystem.New()
|
||||
dependencies.Machine = machine.New(machine.Args{OS: dependencies.OS})
|
||||
dependencies.CookieJar = newCookieJar(dependencies.Machine)
|
||||
dependencies.KeyringBackendType = keyringBackendType()
|
||||
dependencies.Keychain = newKeychain(dependencies.Machine, dependencies.Logger, dependencies.KeyringBackendType, interactive)
|
||||
dependencies.AppStore = appstore.NewAppStore(appstore.Args{
|
||||
CookieJar: dependencies.CookieJar,
|
||||
OperatingSystem: dependencies.OS,
|
||||
Keychain: dependencies.Keychain,
|
||||
Machine: dependencies.Machine,
|
||||
})
|
||||
|
||||
ring, err := keyring.Open(keyring.Config{
|
||||
AllowedBackends: []keyring.BackendType{
|
||||
keyringBackendType(),
|
||||
},
|
||||
ServiceName: KeychainServiceName,
|
||||
FileDir: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName),
|
||||
FilePasswordFunc: func(s string) (string, error) {
|
||||
if passphrase == "" && !interactive {
|
||||
return "", errors.New("keychain passphrase is required when not running in interactive mode; use the \"--keychain-passphrase\" flag")
|
||||
}
|
||||
|
||||
if passphrase != "" {
|
||||
return passphrase, nil
|
||||
}
|
||||
|
||||
path := strings.Split(s, " unlock ")[1]
|
||||
logger.Log().Msgf("enter passphrase to unlock %s (this is separate from your Apple ID password): ", path)
|
||||
bytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to read password")
|
||||
}
|
||||
|
||||
password := string(bytes)
|
||||
password = strings.Trim(password, "\n")
|
||||
password = strings.Trim(password, "\r")
|
||||
|
||||
return password, nil
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to open keyring")
|
||||
}
|
||||
|
||||
return ring, nil
|
||||
util.Must("", createConfigDirectory())
|
||||
}
|
||||
|
||||
func configureConfigDirectory() error {
|
||||
os := util.NewOperatingSystem()
|
||||
machine := util.NewMachine(util.MachineArgs{
|
||||
OperatingSystem: os,
|
||||
})
|
||||
// createConfigDirectory creates the configuration directory for the CLI tool, if needed.
|
||||
func createConfigDirectory() error {
|
||||
configDirectoryPath := filepath.Join(dependencies.Machine.HomeDirectory(), ConfigDirectoryName)
|
||||
_, err := dependencies.OS.Stat(configDirectoryPath)
|
||||
|
||||
configDirectoryPath := filepath.Join(machine.HomeDirectory(), ConfigDirectoryName)
|
||||
_, err := os.Stat(configDirectoryPath)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
err = os.MkdirAll(configDirectoryPath, 0700)
|
||||
if err != nil && dependencies.OS.IsNotExist(err) {
|
||||
err = dependencies.OS.MkdirAll(configDirectoryPath, 0700)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create config directory")
|
||||
return fmt.Errorf("failed to create config directory: %w", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
return errors.Wrap(err, "could not read metadata")
|
||||
return fmt.Errorf("could not read metadata: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseOutputFormat(value string) (OutputFormat, error) {
|
||||
switch value {
|
||||
case "json":
|
||||
return OutputFormatJSON, nil
|
||||
case "text":
|
||||
return OutputFormatText, nil
|
||||
default:
|
||||
return OutputFormatJSON, errors.Errorf("invalid output format: %s", value)
|
||||
}
|
||||
}
|
||||
|
||||
func newLogger(format OutputFormat, verbose bool) log.Logger {
|
||||
var writer io.Writer
|
||||
|
||||
switch format {
|
||||
case OutputFormatJSON:
|
||||
writer = zerolog.SyncWriter(os.Stdout)
|
||||
case OutputFormatText:
|
||||
writer = log.NewWriter()
|
||||
}
|
||||
|
||||
return log.NewLogger(log.LoggerArgs{
|
||||
Verbose: verbose,
|
||||
Writer: writer,
|
||||
})
|
||||
}
|
||||
|
||||
func newAppStore(
|
||||
cmd *cobra.Command,
|
||||
keychainPassphrase string,
|
||||
) (appstore.AppStore, error) {
|
||||
logger := cmd.Context().Value("logger").(log.Logger)
|
||||
interactive := cmd.Context().Value("interactive").(bool)
|
||||
|
||||
cookieJar, err := newCookieJar()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create cookie jar")
|
||||
}
|
||||
|
||||
os := util.NewOperatingSystem()
|
||||
|
||||
keyring, err := newKeyring(logger, keychainPassphrase, interactive)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create keyring")
|
||||
}
|
||||
|
||||
return appstore.NewAppStore(appstore.AppStoreArgs{
|
||||
Logger: logger,
|
||||
CookieJar: cookieJar,
|
||||
Keychain: keychain.NewKeychain(keychain.KeychainArgs{
|
||||
Keyring: keyring,
|
||||
}),
|
||||
Interactive: interactive,
|
||||
Machine: util.NewMachine(util.MachineArgs{
|
||||
OperatingSystem: os,
|
||||
}),
|
||||
OperatingSystem: os,
|
||||
}), nil
|
||||
}
|
||||
@@ -1,14 +1,5 @@
|
||||
package cmd
|
||||
|
||||
import "github.com/thediveo/enumflag/v2"
|
||||
|
||||
type OutputFormat enumflag.Flag
|
||||
|
||||
const (
|
||||
OutputFormatText OutputFormat = iota
|
||||
OutputFormatJSON
|
||||
)
|
||||
|
||||
const (
|
||||
ConfigDirectoryName = ".ipatool"
|
||||
CookieJarFileName = "cookies"
|
||||
|
||||
+94
-10
@@ -1,27 +1,111 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/avast/retry-go"
|
||||
"github.com/majd/ipatool/v2/pkg/appstore"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// nolint:wrapcheck
|
||||
func downloadCmd() *cobra.Command {
|
||||
var keychainPassphrase string
|
||||
var acquireLicense bool
|
||||
var outputPath string
|
||||
var bundleID string
|
||||
var (
|
||||
acquireLicense bool
|
||||
outputPath string
|
||||
bundleID string
|
||||
)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "download",
|
||||
Short: "Download (encrypted) iOS app packages from the App Store",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
appstore, err := newAppStore(cmd, keychainPassphrase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create appstore client")
|
||||
}
|
||||
var lastErr error
|
||||
var acc appstore.Account
|
||||
|
||||
return appstore.Download(bundleID, outputPath, acquireLicense)
|
||||
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
|
||||
}
|
||||
|
||||
if errors.Is(lastErr, appstore.ErrLicenseRequired) {
|
||||
err := dependencies.AppStore.Purchase(appstore.PurchaseInput{Account: acc, App: lookupResult.App})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
progress := progressbar.NewOptions64(1,
|
||||
progressbar.OptionSetDescription("downloading"),
|
||||
progressbar.OptionSetWriter(os.Stdout),
|
||||
progressbar.OptionShowBytes(true),
|
||||
progressbar.OptionSetWidth(20),
|
||||
progressbar.OptionFullWidth(),
|
||||
progressbar.OptionThrottle(65*time.Millisecond),
|
||||
progressbar.OptionShowCount(),
|
||||
progressbar.OptionClearOnFinish(),
|
||||
progressbar.OptionSpinnerType(14),
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetElapsedTime(false),
|
||||
progressbar.OptionSetPredictTime(false),
|
||||
)
|
||||
|
||||
out, err := dependencies.AppStore.Download(appstore.DownloadInput{Account: acc, App: lookupResult.App, OutputPath: outputPath, Progress: progress})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = dependencies.AppStore.ReplicateSinf(appstore.ReplicateSinfInput{Sinfs: out.Sinfs, PackagePath: out.DestinationPath})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dependencies.Logger.Log().
|
||||
Str("output", out.DestinationPath).
|
||||
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
|
||||
|
||||
if errors.Is(err, appstore.ErrPasswordTokenExpired) {
|
||||
return true
|
||||
}
|
||||
|
||||
if errors.Is(err, appstore.ErrLicenseRequired) && acquireLicense {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thediveo/enumflag/v2"
|
||||
)
|
||||
|
||||
type OutputFormat enumflag.Flag
|
||||
|
||||
const (
|
||||
OutputFormatText OutputFormat = iota
|
||||
OutputFormatJSON
|
||||
)
|
||||
|
||||
func OutputFormatFromString(value string) (OutputFormat, error) {
|
||||
switch value {
|
||||
case "json":
|
||||
return OutputFormatJSON, nil
|
||||
case "text":
|
||||
return OutputFormatText, nil
|
||||
default:
|
||||
return OutputFormatJSON, fmt.Errorf("invalid output format '%s'", value)
|
||||
}
|
||||
}
|
||||
+49
-7
@@ -1,25 +1,67 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/avast/retry-go"
|
||||
"github.com/majd/ipatool/v2/pkg/appstore"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// nolint:wrapcheck
|
||||
func purchaseCmd() *cobra.Command {
|
||||
var keychainPassphrase string
|
||||
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 {
|
||||
appstore, err := newAppStore(cmd, keychainPassphrase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create appstore client")
|
||||
}
|
||||
var lastErr error
|
||||
var acc appstore.Account
|
||||
|
||||
return appstore.Purchase(bundleID)
|
||||
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)
|
||||
}),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+32
-27
@@ -1,16 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/appstore"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/thediveo/enumflag/v2"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
|
||||
func rootCmd() *cobra.Command {
|
||||
var verbose bool
|
||||
var nonInteractive bool
|
||||
var format OutputFormat
|
||||
var (
|
||||
verbose bool
|
||||
nonInteractive bool
|
||||
format OutputFormat
|
||||
)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "ipatool",
|
||||
@@ -18,17 +25,10 @@ func rootCmd() *cobra.Command {
|
||||
SilenceErrors: true,
|
||||
SilenceUsage: true,
|
||||
Version: version,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := context.WithValue(context.Background(), "logger", newLogger(format, verbose))
|
||||
ctx = context.WithValue(ctx, "interactive", nonInteractive == false)
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
ctx := context.WithValue(context.Background(), "interactive", !nonInteractive)
|
||||
cmd.SetContext(ctx)
|
||||
|
||||
err := configureConfigDirectory()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to configure config directory")
|
||||
}
|
||||
|
||||
return nil
|
||||
initWithCommand(cmd)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -48,28 +48,33 @@ func rootCmd() *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
// Execute runs the program and returns the approperiate exit status code.
|
||||
func Execute() (exitCode int) {
|
||||
// Execute runs the program and returns the appropriate exit status code.
|
||||
func Execute() int {
|
||||
cmd := rootCmd()
|
||||
err := cmd.Execute()
|
||||
if err != nil {
|
||||
exitCode = 1
|
||||
|
||||
logger := newLogger(OutputFormatText, false)
|
||||
outputFormat, parseErr := parseOutputFormat(cmd.Flag("format").Value.String())
|
||||
if parseErr != nil {
|
||||
logger.Error().Err(parseErr).Send()
|
||||
return
|
||||
if err != nil {
|
||||
if reflect.ValueOf(dependencies).IsZero() {
|
||||
initWithCommand(cmd)
|
||||
}
|
||||
|
||||
logger = newLogger(outputFormat, cmd.Flag("verbose").Value.String() == "true")
|
||||
var appstoreErr *appstore.Error
|
||||
if errors.As(err, &appstoreErr) {
|
||||
dependencies.Logger.Verbose().Stack().
|
||||
Err(err).
|
||||
Interface("metadata", appstoreErr.Metadata).
|
||||
Send()
|
||||
} else {
|
||||
dependencies.Logger.Verbose().Stack().Err(err).Send()
|
||||
}
|
||||
|
||||
logger.Verbose().Stack().Err(err).Send()
|
||||
logger.Error().
|
||||
dependencies.Logger.Error().
|
||||
Err(err).
|
||||
Bool("success", false).
|
||||
Send()
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
return
|
||||
return 0
|
||||
}
|
||||
+19
-5
@@ -2,12 +2,12 @@ package cmd
|
||||
|
||||
import (
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/majd/ipatool/v2/pkg/appstore"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// nolint:wrapcheck
|
||||
func searchCmd() *cobra.Command {
|
||||
var keychainPassphrase string
|
||||
var limit int64
|
||||
|
||||
cmd := &cobra.Command{
|
||||
@@ -15,12 +15,26 @@ func searchCmd() *cobra.Command {
|
||||
Short: "Search for iOS apps available on the App Store",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
appstore, err := newAppStore(cmd, keychainPassphrase)
|
||||
infoResult, err := dependencies.AppStore.AccountInfo()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create appstore client")
|
||||
return err
|
||||
}
|
||||
|
||||
return appstore.Search(args[0], limit)
|
||||
output, err := dependencies.AppStore.Search(appstore.SearchInput{
|
||||
Account: infoResult.Account,
|
||||
Term: args[0],
|
||||
Limit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dependencies.Logger.Log().
|
||||
Int("count", output.Count).
|
||||
Array("apps", appstore.Apps(output.Results)).
|
||||
Send()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 159 KiB After Width: | Height: | Size: 108 KiB |
@@ -1,20 +1,21 @@
|
||||
module github.com/majd/ipatool
|
||||
module github.com/majd/ipatool/v2
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/99designs/keyring v1.2.1
|
||||
github.com/golang/mock v1.6.0
|
||||
github.com/avast/retry-go v3.0.0+incompatible
|
||||
github.com/golang/mock v1.7.0-rc.1
|
||||
github.com/juju/persistent-cookiejar v1.0.0
|
||||
github.com/onsi/ginkgo/v2 v2.5.0
|
||||
github.com/onsi/gomega v1.24.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/rs/zerolog v1.28.0
|
||||
github.com/schollz/progressbar/v3 v3.13.1
|
||||
github.com/spf13/cobra v1.6.1
|
||||
github.com/thediveo/enumflag/v2 v2.0.1
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
|
||||
golang.org/x/net v0.1.0
|
||||
golang.org/x/term v0.2.0
|
||||
golang.org/x/net v0.8.0
|
||||
golang.org/x/term v0.6.0
|
||||
howett.net/plist v1.0.0
|
||||
)
|
||||
|
||||
@@ -29,15 +30,17 @@ require (
|
||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
||||
github.com/juju/go4 v0.0.0-20160222163258-40d72ab9641a // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.16 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/mtibben/percent v0.2.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.3 // indirect
|
||||
github.com/schollz/progressbar/v3 v3.12.1 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/sys v0.3.0 // indirect
|
||||
golang.org/x/text v0.4.0 // indirect
|
||||
golang.org/x/mod v0.8.0 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
golang.org/x/text v0.8.0 // indirect
|
||||
golang.org/x/tools v0.6.0 // indirect
|
||||
gopkg.in/errgo.v1 v1.0.1 // indirect
|
||||
gopkg.in/retry.v1 v1.0.3 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
||||
@@ -2,6 +2,8 @@ github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMb
|
||||
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
|
||||
github.com/99designs/keyring v1.2.1 h1:tYLp1ULvO7i3fI5vE21ReQuj99QFSs7lGm0xWyJo87o=
|
||||
github.com/99designs/keyring v1.2.1/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
|
||||
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
|
||||
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
|
||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
|
||||
@@ -18,8 +20,8 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
|
||||
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=
|
||||
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
|
||||
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
|
||||
github.com/golang/mock v1.7.0-rc.1 h1:YojYx61/OLFsiv6Rw1Z96LpldJIy31o+UHmwAUMJ6/U=
|
||||
github.com/golang/mock v1.7.0-rc.1/go.mod h1:s42URUywIqd+OcERslBJvOjepvNymP31m3q8d/GkuRs=
|
||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
@@ -43,8 +45,9 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
@@ -61,18 +64,16 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.2/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
|
||||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM=
|
||||
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ=
|
||||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
|
||||
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/schollz/progressbar/v3 v3.12.1 h1:JAhtIrLWAn6/p7i82SrpSG3fgAwlAxi+Sy12r4AzBvQ=
|
||||
github.com/schollz/progressbar/v3 v3.12.1/go.mod h1:g7QSuwyGpqCjVQPFZXA31MSxtrhka9Y9LMdF+XT77/Y=
|
||||
github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE=
|
||||
github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
|
||||
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
|
||||
@@ -85,45 +86,45 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/thediveo/enumflag/v2 v2.0.1 h1:2bmWZPD2uSARDsOjXIdLRlNcYBFNF9xX0RNUNF2vKic=
|
||||
github.com/thediveo/enumflag/v2 v2.0.1/go.mod h1:SyxyCNvv0QeRtZ7fjuaUz4FRLC3cWuDiD7QdORU0MGg=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
|
||||
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
|
||||
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
|
||||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
|
||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
|
||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
||||
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/majd/ipatool/cmd"
|
||||
"os"
|
||||
|
||||
"github.com/majd/ipatool/v2/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
+3
-1
@@ -1,6 +1,8 @@
|
||||
package appstore
|
||||
|
||||
import "github.com/rs/zerolog"
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
ID int64 `json:"trackId,omitempty"`
|
||||
|
||||
@@ -3,6 +3,7 @@ package appstore
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/rs/zerolog"
|
||||
@@ -63,4 +64,16 @@ var _ = Describe("App", func() {
|
||||
Expect(out["version"]).To(Equal("1.0"))
|
||||
Expect(out["price"]).To(Equal(float64(0)))
|
||||
})
|
||||
|
||||
It("formats ipa name correctly", func() {
|
||||
app := App{
|
||||
ID: 42,
|
||||
BundleID: "app.bundle-id1",
|
||||
Name: " some app&symb.ols2 !!!",
|
||||
Version: "1.0",
|
||||
Price: 0,
|
||||
}
|
||||
|
||||
Expect(fileName(app)).To(Equal("app.bundle-id1_42_1.0.ipa"))
|
||||
})
|
||||
})
|
||||
+35
-34
@@ -1,62 +1,63 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
"io"
|
||||
"os"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
"github.com/majd/ipatool/v2/pkg/util/machine"
|
||||
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
|
||||
)
|
||||
|
||||
type AppStore interface {
|
||||
Login(email, password, authCode string) error
|
||||
Info() error
|
||||
// Login authenticates with the App Store.
|
||||
Login(input LoginInput) (LoginOutput, error)
|
||||
// AccountInfo returns the information of the authenticated account.
|
||||
AccountInfo() (AccountInfoOutput, error)
|
||||
// Revoke revokes the active credentials.
|
||||
Revoke() error
|
||||
Search(term string, limit int64) error
|
||||
Purchase(bundleID string) error
|
||||
Download(bundleID string, outputPath string, acquireLicense bool) error
|
||||
// Lookup looks apps up based on the specified bundle identifier.
|
||||
Lookup(input LookupInput) (LookupOutput, error)
|
||||
// Search searches the App Store for apps matching the specified term.
|
||||
Search(input SearchInput) (SearchOutput, error)
|
||||
// Purchase acquires a license for the desired app.
|
||||
// Note: only free apps are supported.
|
||||
Purchase(input PurchaseInput) error
|
||||
// Download downloads the IPA package from the App Store to the desired location.
|
||||
Download(input DownloadInput) (DownloadOutput, error)
|
||||
// ReplicateSinf replicates the sinf for the IPA package.
|
||||
ReplicateSinf(input ReplicateSinfInput) error
|
||||
}
|
||||
|
||||
type appstore struct {
|
||||
keychain keychain.Keychain
|
||||
loginClient http.Client[LoginResult]
|
||||
searchClient http.Client[SearchResult]
|
||||
purchaseClient http.Client[PurchaseResult]
|
||||
downloadClient http.Client[DownloadResult]
|
||||
loginClient http.Client[loginResult]
|
||||
searchClient http.Client[searchResult]
|
||||
purchaseClient http.Client[purchaseResult]
|
||||
downloadClient http.Client[downloadResult]
|
||||
httpClient http.Client[interface{}]
|
||||
ioReader io.Reader
|
||||
machine util.Machine
|
||||
os util.OperatingSystem
|
||||
logger log.Logger
|
||||
interactive bool
|
||||
machine machine.Machine
|
||||
os operatingsystem.OperatingSystem
|
||||
}
|
||||
|
||||
type AppStoreArgs struct {
|
||||
type Args struct {
|
||||
Keychain keychain.Keychain
|
||||
CookieJar http.CookieJar
|
||||
Logger log.Logger
|
||||
OperatingSystem util.OperatingSystem
|
||||
Machine util.Machine
|
||||
Interactive bool
|
||||
OperatingSystem operatingsystem.OperatingSystem
|
||||
Machine machine.Machine
|
||||
}
|
||||
|
||||
func NewAppStore(args AppStoreArgs) AppStore {
|
||||
clientArgs := http.ClientArgs{
|
||||
func NewAppStore(args Args) AppStore {
|
||||
clientArgs := http.Args{
|
||||
CookieJar: args.CookieJar,
|
||||
}
|
||||
|
||||
return &appstore{
|
||||
keychain: args.Keychain,
|
||||
loginClient: http.NewClient[LoginResult](clientArgs),
|
||||
searchClient: http.NewClient[SearchResult](clientArgs),
|
||||
purchaseClient: http.NewClient[PurchaseResult](clientArgs),
|
||||
downloadClient: http.NewClient[DownloadResult](clientArgs),
|
||||
loginClient: http.NewClient[loginResult](clientArgs),
|
||||
searchClient: http.NewClient[searchResult](clientArgs),
|
||||
purchaseClient: http.NewClient[purchaseResult](clientArgs),
|
||||
downloadClient: http.NewClient[downloadResult](clientArgs),
|
||||
httpClient: http.NewClient[interface{}](clientArgs),
|
||||
ioReader: os.Stdin,
|
||||
machine: args.Machine,
|
||||
os: args.OperatingSystem,
|
||||
logger: args.Logger,
|
||||
interactive: args.Interactive,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type AccountInfoOutput struct {
|
||||
Account Account
|
||||
}
|
||||
|
||||
func (t *appstore) AccountInfo() (AccountInfoOutput, error) {
|
||||
data, err := t.keychain.Get("account")
|
||||
if err != nil {
|
||||
return AccountInfoOutput{}, fmt.Errorf("failed to get account: %w", err)
|
||||
}
|
||||
|
||||
var acc Account
|
||||
|
||||
err = json.Unmarshal(data, &acc)
|
||||
if err != nil {
|
||||
return AccountInfoOutput{}, fmt.Errorf("failed to unmarshal json: %w", err)
|
||||
}
|
||||
|
||||
return AccountInfoOutput{
|
||||
Account: acc,
|
||||
}, nil
|
||||
}
|
||||
@@ -1,29 +1,27 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (Info)", func() {
|
||||
var _ = Describe("AppStore (AccountInfo)", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
appstore AppStore
|
||||
mockKeychain *keychain.MockKeychain
|
||||
mockLogger *log.MockLogger
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
appstore = NewAppStore(AppStoreArgs{
|
||||
appstore = NewAppStore(Args{
|
||||
Keychain: mockKeychain,
|
||||
Logger: mockLogger,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -31,19 +29,36 @@ var _ = Describe("AppStore (Info)", func() {
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("keychain returns error", func() {
|
||||
var testErr = errors.New("test error")
|
||||
When("keychain returns valid data", func() {
|
||||
const (
|
||||
testEmail = "test-email"
|
||||
testName = "test-name"
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte{}, testErr)
|
||||
Return([]byte(fmt.Sprintf("{\"email\": \"%s\", \"name\": \"%s\"}", testEmail, testName)), nil)
|
||||
})
|
||||
|
||||
It("returns output", func() {
|
||||
out, err := appstore.AccountInfo()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(out.Account.Email).To(Equal(testEmail))
|
||||
Expect(out.Account.Name).To(Equal(testName))
|
||||
})
|
||||
})
|
||||
|
||||
When("keychain returns error", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
err := appstore.Info()
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetAccount.Error())))
|
||||
_, err := appstore.AccountInfo()
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -55,25 +70,8 @@ var _ = Describe("AppStore (Info)", func() {
|
||||
})
|
||||
|
||||
It("fails to unmarshall JSON data", func() {
|
||||
err := appstore.Info()
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrUnmarshal.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("keychain returns valid data", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{}"), nil)
|
||||
})
|
||||
|
||||
It("returns nil", func() {
|
||||
err := appstore.Info()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err := appstore.AccountInfo()
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
+107
-312
@@ -2,223 +2,150 @@ package appstore
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"howett.net/plist"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"howett.net/plist"
|
||||
)
|
||||
|
||||
type DownloadSinfResult struct {
|
||||
ID int64 `plist:"id,omitempty"`
|
||||
Data []byte `plist:"sinf,omitempty"`
|
||||
var (
|
||||
ErrLicenseRequired = errors.New("license is required")
|
||||
)
|
||||
|
||||
type DownloadInput struct {
|
||||
Account Account
|
||||
App App
|
||||
OutputPath string
|
||||
Progress *progressbar.ProgressBar
|
||||
}
|
||||
|
||||
type DownloadItemResult struct {
|
||||
HashMD5 string `plist:"md5,omitempty"`
|
||||
URL string `plist:"URL,omitempty"`
|
||||
Sinfs []DownloadSinfResult `plist:"sinfs,omitempty"`
|
||||
Metadata map[string]interface{} `plist:"metadata,omitempty"`
|
||||
type DownloadOutput struct {
|
||||
DestinationPath string
|
||||
Sinfs []Sinf
|
||||
}
|
||||
|
||||
type DownloadResult struct {
|
||||
FailureType string `plist:"failureType,omitempty"`
|
||||
CustomerMessage string `plist:"customerMessage,omitempty"`
|
||||
Items []DownloadItemResult `plist:"songList,omitempty"`
|
||||
}
|
||||
|
||||
type PackageManifest struct {
|
||||
SinfPaths []string `plist:"SinfPaths,omitempty"`
|
||||
}
|
||||
|
||||
type PackageInfo struct {
|
||||
BundleExecutable string `plist:"CFBundleExecutable,omitempty"`
|
||||
}
|
||||
|
||||
func (a *appstore) Download(bundleID string, outputPath string, acquireLicense bool) error {
|
||||
acc, err := a.account()
|
||||
func (t *appstore) Download(input DownloadInput) (DownloadOutput, error) {
|
||||
destination, err := t.resolveDestinationPath(input.App, input.OutputPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetAccount.Error())
|
||||
return DownloadOutput{}, fmt.Errorf("failed to resolve destination path: %w", err)
|
||||
}
|
||||
|
||||
countryCode, err := a.countryCodeFromStoreFront(acc.StoreFront)
|
||||
macAddr, err := t.machine.MacAddress()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrInvalidCountryCode.Error())
|
||||
}
|
||||
|
||||
app, err := a.lookup(bundleID, countryCode)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrAppLookup.Error())
|
||||
}
|
||||
|
||||
destination, err := a.resolveDestinationPath(app, outputPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrResolveDestinationPath.Error())
|
||||
}
|
||||
|
||||
macAddr, err := a.machine.MacAddress()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetMAC.Error())
|
||||
return DownloadOutput{}, fmt.Errorf("failed to get mac address: %w", err)
|
||||
}
|
||||
|
||||
guid := strings.ReplaceAll(strings.ToUpper(macAddr), ":", "")
|
||||
a.logger.Verbose().Str("mac", macAddr).Str("guid", guid).Send()
|
||||
|
||||
err = a.download(acc, app, destination, guid, acquireLicense, true)
|
||||
req := t.downloadRequest(input.Account, input.App, guid)
|
||||
|
||||
res, err := t.downloadClient.Send(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrDownloadFile.Error())
|
||||
}
|
||||
|
||||
a.logger.Log().Str("output", destination).Bool("success", true).Send()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) download(acc Account, app App, dst, guid string, acquireLicense, attemptToRenewCredentials bool) error {
|
||||
req := a.downloadRequest(acc, app, guid)
|
||||
|
||||
res, err := a.downloadClient.Send(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRequest.Error())
|
||||
return DownloadOutput{}, fmt.Errorf("failed to send http request: %w", err)
|
||||
}
|
||||
|
||||
if res.Data.FailureType == FailureTypePasswordTokenExpired {
|
||||
if attemptToRenewCredentials {
|
||||
a.logger.Verbose().Msg("retrieving new password token")
|
||||
acc, err = a.login(acc.Email, acc.Password, "", guid, 0, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrPasswordTokenExpired.Error())
|
||||
}
|
||||
|
||||
return a.download(acc, app, dst, guid, acquireLicense, false)
|
||||
}
|
||||
|
||||
return ErrPasswordTokenExpired
|
||||
}
|
||||
|
||||
if res.Data.FailureType == FailureTypeLicenseNotFound && acquireLicense {
|
||||
a.logger.Verbose().Msg("attempting to acquire license")
|
||||
err = a.purchase(app.BundleID, guid, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrPurchase.Error())
|
||||
}
|
||||
|
||||
return a.download(acc, app, dst, guid, false, attemptToRenewCredentials)
|
||||
return DownloadOutput{}, ErrPasswordTokenExpired
|
||||
}
|
||||
|
||||
if res.Data.FailureType == FailureTypeLicenseNotFound {
|
||||
return ErrLicenseRequired
|
||||
return DownloadOutput{}, ErrLicenseRequired
|
||||
}
|
||||
|
||||
if res.Data.FailureType != "" && res.Data.CustomerMessage != "" {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return errors.New(res.Data.CustomerMessage)
|
||||
return DownloadOutput{}, NewErrorWithMetadata(fmt.Errorf("received error: %s", res.Data.CustomerMessage), res)
|
||||
}
|
||||
|
||||
if res.Data.FailureType != "" {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return ErrGeneric
|
||||
return DownloadOutput{}, NewErrorWithMetadata(fmt.Errorf("received error: %s", res.Data.FailureType), res)
|
||||
}
|
||||
|
||||
if len(res.Data.Items) == 0 {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return ErrInvalidResponse
|
||||
return DownloadOutput{}, NewErrorWithMetadata(errors.New("invalid response"), res)
|
||||
}
|
||||
|
||||
item := res.Data.Items[0]
|
||||
|
||||
err = a.downloadFile(fmt.Sprintf("%s.tmp", dst), item.URL)
|
||||
err = t.downloadFile(item.URL, fmt.Sprintf("%s.tmp", destination), input.Progress)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrDownloadFile.Error())
|
||||
return DownloadOutput{}, fmt.Errorf("failed to download file: %w", err)
|
||||
}
|
||||
|
||||
err = a.applyPatches(item, acc, fmt.Sprintf("%s.tmp", dst), dst)
|
||||
err = t.applyPatches(item, input.Account, fmt.Sprintf("%s.tmp", destination), destination)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrPatchApp.Error())
|
||||
return DownloadOutput{}, fmt.Errorf("failed to apply patches: %w", err)
|
||||
}
|
||||
|
||||
err = a.os.Remove(fmt.Sprintf("%s.tmp", dst))
|
||||
err = t.os.Remove(fmt.Sprintf("%s.tmp", destination))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRemoveTempFile.Error())
|
||||
return DownloadOutput{}, fmt.Errorf("failed to remove file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return DownloadOutput{
|
||||
DestinationPath: destination,
|
||||
Sinfs: item.Sinfs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *appstore) downloadFile(dst, sourceURL string) (err error) {
|
||||
req, err := a.httpClient.NewRequest("GET", sourceURL, nil)
|
||||
type downloadItemResult struct {
|
||||
HashMD5 string `plist:"md5,omitempty"`
|
||||
URL string `plist:"URL,omitempty"`
|
||||
Sinfs []Sinf `plist:"sinfs,omitempty"`
|
||||
Metadata map[string]interface{} `plist:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type downloadResult struct {
|
||||
FailureType string `plist:"failureType,omitempty"`
|
||||
CustomerMessage string `plist:"customerMessage,omitempty"`
|
||||
Items []downloadItemResult `plist:"songList,omitempty"`
|
||||
}
|
||||
|
||||
func (t *appstore) downloadFile(src, dst string, progress *progressbar.ProgressBar) error {
|
||||
req, err := t.httpClient.NewRequest("GET", src, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCreateRequest.Error())
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
res, err := a.httpClient.Do(req)
|
||||
res, err := t.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRequest.Error())
|
||||
return fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
file, err := t.os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if closeErr := res.Body.Close(); closeErr != err && err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
}()
|
||||
defer file.Close()
|
||||
|
||||
file, err := a.os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrOpenFile.Error())
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if closeErr := file.Close(); closeErr != err && err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
}()
|
||||
|
||||
sizeMB := float64(res.ContentLength) / (1 << 20)
|
||||
a.logger.Verbose().Str("size", fmt.Sprintf("%.2fMB", sizeMB)).Msg("downloading")
|
||||
|
||||
if a.interactive {
|
||||
bar := progressbar.NewOptions64(res.ContentLength,
|
||||
progressbar.OptionSetDescription("downloading"),
|
||||
progressbar.OptionSetWriter(os.Stdout),
|
||||
progressbar.OptionShowBytes(true),
|
||||
progressbar.OptionSetWidth(20),
|
||||
progressbar.OptionFullWidth(),
|
||||
progressbar.OptionThrottle(65*time.Millisecond),
|
||||
progressbar.OptionShowCount(),
|
||||
progressbar.OptionClearOnFinish(),
|
||||
progressbar.OptionSpinnerType(14),
|
||||
progressbar.OptionSetRenderBlankState(true),
|
||||
progressbar.OptionSetElapsedTime(false),
|
||||
progressbar.OptionSetPredictTime(false),
|
||||
)
|
||||
|
||||
_, err = io.Copy(io.MultiWriter(file, bar), res.Body)
|
||||
if progress != nil {
|
||||
progress.ChangeMax64(res.ContentLength)
|
||||
_, err = io.Copy(io.MultiWriter(file, progress), res.Body)
|
||||
} else {
|
||||
_, err = io.Copy(file, res.Body)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrFileWrite.Error())
|
||||
return fmt.Errorf("failed to write file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*appstore) downloadRequest(acc Account, app App, guid string) http.Request {
|
||||
host := fmt.Sprintf("%s-%s", PriavteAppStoreAPIDomainPrefixWithoutAuthCode, PrivateAppStoreAPIDomain)
|
||||
host := fmt.Sprintf("%s-%s", PrivateAppStoreAPIDomainPrefixWithoutAuthCode, PrivateAppStoreAPIDomain)
|
||||
|
||||
return http.Request{
|
||||
URL: fmt.Sprintf("https://%s%s?guid=%s", host, PrivateAppStoreAPIPathDownload, guid),
|
||||
Method: http.MethodPOST,
|
||||
ResponseFormat: http.ResponseFormatXML,
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "Configurator/2.15 (Macintosh; OperatingSystem X 11.0.0; 16G29) AppleWebKit/2603.3.8",
|
||||
"Content-Type": "application/x-apple-plist",
|
||||
"iCloud-DSID": acc.DirectoryServicesID,
|
||||
"X-Dsid": acc.DirectoryServicesID,
|
||||
@@ -233,43 +160,41 @@ func (*appstore) downloadRequest(acc Account, app App, guid string) http.Request
|
||||
}
|
||||
}
|
||||
|
||||
func (a *appstore) resolveDestinationPath(app App, path string) (string, error) {
|
||||
file := fmt.Sprintf("/%s_%d_v%s_%d.ipa", app.BundleID, app.ID, app.Version, util.RandInt(100, 999))
|
||||
func fileName(app App) string {
|
||||
return fmt.Sprintf("%s_%d_%s.ipa",
|
||||
app.BundleID,
|
||||
app.ID,
|
||||
app.Version)
|
||||
}
|
||||
|
||||
func (t *appstore) resolveDestinationPath(app App, path string) (string, error) {
|
||||
file := fileName(app)
|
||||
|
||||
if path == "" {
|
||||
workdir, err := a.currentDirectory()
|
||||
workdir, err := t.os.Getwd()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetCurrentDirectory.Error())
|
||||
return "", fmt.Errorf("failed to get current directory: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s", workdir, file), nil
|
||||
return fmt.Sprintf("%s/%s", workdir, file), nil
|
||||
}
|
||||
|
||||
isDir, err := a.isDirectory(path)
|
||||
isDir, err := t.isDirectory(path)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrCheckDirectory.Error())
|
||||
return "", fmt.Errorf("failed to determine whether path is a directory: %w", err)
|
||||
}
|
||||
|
||||
if isDir {
|
||||
return fmt.Sprintf("%s%s", path, file), nil
|
||||
return fmt.Sprintf("%s/%s", path, file), nil
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (a *appstore) currentDirectory() (string, error) {
|
||||
path, err := a.os.Executable()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetExecutablePath.Error())
|
||||
}
|
||||
|
||||
return filepath.Dir(path), nil
|
||||
}
|
||||
|
||||
func (a *appstore) isDirectory(path string) (bool, error) {
|
||||
info, err := a.os.Stat(path)
|
||||
func (t *appstore) isDirectory(path string) (bool, error) {
|
||||
info, err := t.os.Stat(path)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return false, errors.Wrap(err, ErrGetFileMetadata.Error())
|
||||
return false, fmt.Errorf("failed to read file metadata: %w", err)
|
||||
}
|
||||
|
||||
if info == nil {
|
||||
@@ -279,181 +204,51 @@ func (a *appstore) isDirectory(path string) (bool, error) {
|
||||
return info.IsDir(), nil
|
||||
}
|
||||
|
||||
func (a *appstore) applyPatches(item DownloadItemResult, acc Account, src, dst string) (err error) {
|
||||
dstFile, err := a.os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrOpenFile.Error())
|
||||
}
|
||||
|
||||
func (t *appstore) applyPatches(item downloadItemResult, acc Account, src, dst string) error {
|
||||
srcZip, err := zip.OpenReader(src)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrOpenZipFile.Error())
|
||||
return fmt.Errorf("failed to open zip reader: %w", err)
|
||||
}
|
||||
defer srcZip.Close()
|
||||
|
||||
dstFile, err := t.os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if closeErr := srcZip.Close(); closeErr != err && err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
}()
|
||||
|
||||
dstZip := zip.NewWriter(dstFile)
|
||||
defer func() {
|
||||
if closeErr := dstZip.Close(); closeErr != err && err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
}()
|
||||
defer dstZip.Close()
|
||||
|
||||
manifestData := new(bytes.Buffer)
|
||||
infoData := new(bytes.Buffer)
|
||||
|
||||
appBundle, err := a.replicateZip(srcZip, dstZip, infoData, manifestData)
|
||||
err = t.replicateZip(srcZip, dstZip)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrReplicateZip.Error())
|
||||
return fmt.Errorf("failed to replicate zip: %w", err)
|
||||
}
|
||||
|
||||
err = a.writeMetadata(item.Metadata, acc, dstZip)
|
||||
err = t.writeMetadata(item.Metadata, acc, dstZip)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrWriteMetadataFile.Error())
|
||||
}
|
||||
|
||||
if manifestData.Len() > 0 {
|
||||
err = a.applySinfPatches(item, dstZip, manifestData.Bytes(), appBundle)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrApplyPatches.Error())
|
||||
}
|
||||
} else {
|
||||
err = a.applyLegacySinfPatches(item, dstZip, infoData.Bytes(), appBundle)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrApplyLegacyPatches.Error())
|
||||
}
|
||||
return fmt.Errorf("failed to write metadata: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) writeMetadata(metadata map[string]interface{}, acc Account, zip *zip.Writer) error {
|
||||
func (t *appstore) writeMetadata(metadata map[string]interface{}, acc Account, zip *zip.Writer) error {
|
||||
metadata["apple-id"] = acc.Email
|
||||
metadata["userName"] = acc.Email
|
||||
|
||||
metadataFile, err := zip.Create("iTunesMetadata.plist")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCreateMetadataFile.Error())
|
||||
return fmt.Errorf("failed to create file: %w", err)
|
||||
}
|
||||
|
||||
data, err := plist.Marshal(metadata, plist.BinaryFormat)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrEncodeMetadataFile.Error())
|
||||
return fmt.Errorf("failed to marshal data: %w", err)
|
||||
}
|
||||
|
||||
_, err = metadataFile.Write(data)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrWriteMetadataFile.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) replicateZip(src *zip.ReadCloser, dst *zip.Writer, info *bytes.Buffer, manifest *bytes.Buffer) (appBundle string, err error) {
|
||||
for _, file := range src.File {
|
||||
srcFile, err := file.OpenRaw()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrOpenFile.Error())
|
||||
}
|
||||
|
||||
if strings.HasSuffix(file.Name, ".app/SC_Info/Manifest.plist") {
|
||||
srcFileD, err := file.Open()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrDecompressManifestFile.Error())
|
||||
}
|
||||
|
||||
_, err = io.Copy(manifest, srcFileD)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetManifestFile.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(file.Name, ".app/Info.plist") {
|
||||
srcFileD, err := file.Open()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrDecompressInfoFile.Error())
|
||||
}
|
||||
|
||||
appBundle = filepath.Base(strings.TrimSuffix(file.Name, ".app/Info.plist"))
|
||||
|
||||
_, err = io.Copy(info, srcFileD)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetInfoFile.Error())
|
||||
}
|
||||
}
|
||||
|
||||
header := file.FileHeader
|
||||
dstFile, err := dst.CreateRaw(&header)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrCreateDestinationFile.Error())
|
||||
}
|
||||
|
||||
_, err = io.Copy(dstFile, srcFile)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrFileWrite.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if appBundle == "" {
|
||||
return "", ErrGetBundleName
|
||||
}
|
||||
|
||||
return appBundle, nil
|
||||
}
|
||||
|
||||
func (a *appstore) applySinfPatches(item DownloadItemResult, zip *zip.Writer, manifestData []byte, appBundle string) error {
|
||||
var manifest PackageManifest
|
||||
_, err := plist.Unmarshal(manifestData, &manifest)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrUnmarshal.Error())
|
||||
}
|
||||
|
||||
zipped, err := util.Zip(item.Sinfs, manifest.SinfPaths)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrZipSinfs.Error())
|
||||
}
|
||||
|
||||
for _, pair := range zipped {
|
||||
sp := fmt.Sprintf("Payload/%s.app/%s", appBundle, pair.Second)
|
||||
a.logger.Verbose().Str("path", sp).Msg("writing sinf data")
|
||||
|
||||
file, err := zip.Create(sp)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCreateSinfFile.Error())
|
||||
}
|
||||
|
||||
_, err = file.Write(pair.First.Data)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrWriteSinfData.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) applyLegacySinfPatches(item DownloadItemResult, zip *zip.Writer, infoData []byte, appBundle string) error {
|
||||
a.logger.Verbose().Msg("applying legacy sinf patches")
|
||||
|
||||
var info PackageInfo
|
||||
_, err := plist.Unmarshal(infoData, &info)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrUnmarshal.Error())
|
||||
}
|
||||
|
||||
sp := fmt.Sprintf("Payload/%s.app/SC_Info/%s.sinf", appBundle, info.BundleExecutable)
|
||||
a.logger.Verbose().Str("path", sp).Msg("writing sinf data")
|
||||
|
||||
file, err := zip.Create(sp)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrCreateSinfFile.Error())
|
||||
}
|
||||
|
||||
_, err = file.Write(item.Sinfs[0].Data)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrWriteSinfData.Error())
|
||||
return fmt.Errorf("failed to write data: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,61 +1,54 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
zip "archive/zip"
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"howett.net/plist"
|
||||
"io"
|
||||
gohttp "net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
"github.com/majd/ipatool/v2/pkg/util/machine"
|
||||
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"howett.net/plist"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (Download)", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
mockKeychain *keychain.MockKeychain
|
||||
mockSearchClient *http.MockClient[SearchResult]
|
||||
mockDownloadClient *http.MockClient[DownloadResult]
|
||||
mockPurchaseClient *http.MockClient[PurchaseResult]
|
||||
mockLoginClient *http.MockClient[LoginResult]
|
||||
mockDownloadClient *http.MockClient[downloadResult]
|
||||
mockPurchaseClient *http.MockClient[purchaseResult]
|
||||
mockLoginClient *http.MockClient[loginResult]
|
||||
mockHTTPClient *http.MockClient[interface{}]
|
||||
mockLogger *log.MockLogger
|
||||
mockOS *util.MockOperatingSystem
|
||||
mockMachine *util.MockMachine
|
||||
mockOS *operatingsystem.MockOperatingSystem
|
||||
mockMachine *machine.MockMachine
|
||||
as AppStore
|
||||
testErr = errors.New("testErr")
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
mockSearchClient = http.NewMockClient[SearchResult](ctrl)
|
||||
mockDownloadClient = http.NewMockClient[DownloadResult](ctrl)
|
||||
mockLoginClient = http.NewMockClient[LoginResult](ctrl)
|
||||
mockPurchaseClient = http.NewMockClient[PurchaseResult](ctrl)
|
||||
mockDownloadClient = http.NewMockClient[downloadResult](ctrl)
|
||||
mockLoginClient = http.NewMockClient[loginResult](ctrl)
|
||||
mockPurchaseClient = http.NewMockClient[purchaseResult](ctrl)
|
||||
mockHTTPClient = http.NewMockClient[interface{}](ctrl)
|
||||
mockOS = util.NewMockOperatingSystem(ctrl)
|
||||
mockMachine = util.NewMockMachine(ctrl)
|
||||
mockOS = operatingsystem.NewMockOperatingSystem(ctrl)
|
||||
mockMachine = machine.NewMockMachine(ctrl)
|
||||
as = &appstore{
|
||||
keychain: mockKeychain,
|
||||
loginClient: mockLoginClient,
|
||||
searchClient: mockSearchClient,
|
||||
purchaseClient: mockPurchaseClient,
|
||||
downloadClient: mockDownloadClient,
|
||||
httpClient: mockHTTPClient,
|
||||
machine: mockMachine,
|
||||
os: mockOS,
|
||||
logger: mockLogger,
|
||||
interactive: true,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -63,125 +56,42 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("not logged in", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte{}, testErr)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetAccount.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to determine country code", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"\"}"), nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrInvalidCountryCode.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to find app", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{}, testErr)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrAppLookup.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to resolve output path", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Stat(gomock.Any()).
|
||||
Return(nil, testErr)
|
||||
Return(nil, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "test-out", false)
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to resolve destination path")))
|
||||
_, err := as.Download(DownloadInput{
|
||||
OutputPath: "test-out",
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to read MAC address", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", testErr)
|
||||
Return("", errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetMAC.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("request fails", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
@@ -190,37 +100,19 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{}, testErr)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
Return(http.Result[downloadResult]{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("password token is expired", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
@@ -229,85 +121,23 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
FailureType: FailureTypePasswordTokenExpired,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
When("attempts to renew credentials", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
})
|
||||
|
||||
When("fails to renew credentials", func() {
|
||||
BeforeEach(func() {
|
||||
mockLoginClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{}, testErr)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPasswordTokenExpired.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("successfully renews credentials", func() {
|
||||
BeforeEach(func() {
|
||||
mockLoginClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{},
|
||||
}, nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Set("account", gomock.Any()).
|
||||
Return(nil)
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
FailureType: FailureTypePasswordTokenExpired,
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("attempts to download app", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPasswordTokenExpired.Error())))
|
||||
})
|
||||
})
|
||||
It("returns error", func() {
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("license is missing", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
@@ -316,126 +146,45 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
FailureType: FailureTypeLicenseNotFound,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", false)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrLicenseRequired.Error())))
|
||||
})
|
||||
|
||||
When("attempts to acquire license", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
When("license is acquired", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
StatusCode: 200,
|
||||
Data: PurchaseResult{
|
||||
JingleDocType: "purchaseSuccess",
|
||||
Status: 0,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{}, testErr)
|
||||
})
|
||||
|
||||
It("attempts to download app", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to acquire license", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte{}, testErr)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPurchase.Error())))
|
||||
})
|
||||
})
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("store API returns error", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
})
|
||||
|
||||
When("response contains customer message", func() {
|
||||
BeforeEach(func() {
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
FailureType: "test-failure",
|
||||
CustomerMessage: testErr.Error(),
|
||||
CustomerMessage: errors.New("").Error(),
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns customer message as error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -443,97 +192,60 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
BeforeEach(func() {
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
FailureType: "test-failure",
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns generic error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGeneric.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("store API returns no items", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Items: []DownloadItemResult{},
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
Items: []downloadItemResult{},
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring("received 0 items from the App Store")))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to download file", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Items: []DownloadItemResult{{}},
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
Items: []downloadItemResult{{}},
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
@@ -542,12 +254,12 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
BeforeEach(func() {
|
||||
mockHTTPClient.EXPECT().
|
||||
NewRequest("GET", gomock.Any(), nil).
|
||||
Return(nil, testErr)
|
||||
Return(nil, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrCreateRequest.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -559,12 +271,12 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
mockHTTPClient.EXPECT().
|
||||
Do(gomock.Any()).
|
||||
Return(nil, testErr)
|
||||
Return(nil, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -582,12 +294,12 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
Return(nil, testErr)
|
||||
Return(nil, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to open file")))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -606,15 +318,11 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
Return(nil, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrFileWrite.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -627,37 +335,18 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
testFile, err = os.CreateTemp("", "test_file")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{{}},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
|
||||
mockDownloadClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[DownloadResult]{
|
||||
Data: DownloadResult{
|
||||
Items: []DownloadItemResult{
|
||||
Return(http.Result[downloadResult]{
|
||||
Data: downloadResult{
|
||||
Items: []downloadItemResult{
|
||||
{
|
||||
Metadata: map[string]interface{}{},
|
||||
Sinfs: []DownloadSinfResult{
|
||||
Sinfs: []Sinf{
|
||||
{
|
||||
ID: 0,
|
||||
Data: []byte("test-sinf-data"),
|
||||
@@ -690,15 +379,11 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
It("writes data to file", func() {
|
||||
mockOS.EXPECT().
|
||||
Executable().
|
||||
Getwd().
|
||||
Return("", nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
Return(nil, testErr)
|
||||
|
||||
err := as.Download("", "", true)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrOpenFile.Error())))
|
||||
_, err := as.Download(DownloadInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
|
||||
testData, err := os.ReadFile(testFile.Name())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@@ -706,9 +391,10 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
})
|
||||
|
||||
When("successfully applies patches", func() {
|
||||
var tmpFile *os.File
|
||||
var zipFile *zip.Writer
|
||||
var outputPath string
|
||||
var (
|
||||
tmpFile *os.File
|
||||
outputPath string
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
var err error
|
||||
@@ -719,17 +405,7 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
DoAndReturn(func(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
return os.OpenFile(name, flag, perm)
|
||||
})
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
DoAndReturn(os.OpenFile)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Stat(gomock.Any()).
|
||||
@@ -738,6 +414,21 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
mockOS.EXPECT().
|
||||
Remove(tmpFile.Name()).
|
||||
Return(nil)
|
||||
|
||||
zipFile := zip.NewWriter(tmpFile)
|
||||
w, err := zipFile.Create("Payload/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info, err := plist.Marshal(map[string]interface{}{
|
||||
"CFBundleExecutable": "Test",
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(info)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = zipFile.Close()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
@@ -745,70 +436,12 @@ var _ = Describe("AppStore (Download)", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
When("app uses legacy FairPlay protection", func() {
|
||||
BeforeEach(func() {
|
||||
zipFile = zip.NewWriter(tmpFile)
|
||||
w, err := zipFile.Create("Payload/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info, err := plist.Marshal(map[string]interface{}{
|
||||
"CFBundleExecutable": "Test",
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(info)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = zipFile.Close()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
It("succeeds", func() {
|
||||
err := as.Download("", outputPath, true)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("app uses modern FiarPlay protection", func() {
|
||||
BeforeEach(func() {
|
||||
zipFile = zip.NewWriter(tmpFile)
|
||||
w, err := zipFile.Create("Payload/Test.app/SC_Info/Manifest.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
manifest, err := plist.Marshal(PackageManifest{
|
||||
SinfPaths: []string{
|
||||
"SC_Info/TestApp.sinf",
|
||||
},
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(manifest)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
w, err = zipFile.Create("Payload/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info, err := plist.Marshal(map[string]interface{}{
|
||||
"CFBundleExecutable": "Test",
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(info)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
err = zipFile.Close()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("succeeds", func() {
|
||||
outputPath := strings.TrimSuffix(tmpFile.Name(), ".tmp")
|
||||
err := as.Download("", outputPath, true)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
It("succeeds", func() {
|
||||
out, err := as.Download(DownloadInput{
|
||||
OutputPath: outputPath,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(out.DestinationPath).ToNot(BeEmpty())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (a *appstore) Info() error {
|
||||
acc, err := a.account()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetAccount.Error())
|
||||
}
|
||||
|
||||
a.logger.Log().
|
||||
Str("name", acc.Name).
|
||||
Str("email", acc.Email).
|
||||
Bool("success", true).
|
||||
Send()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) account() (Account, error) {
|
||||
data, err := a.keychain.Get("account")
|
||||
if err != nil {
|
||||
return Account{}, errors.Wrap(err, ErrGetKeychainItem.Error())
|
||||
}
|
||||
|
||||
var acc Account
|
||||
err = json.Unmarshal(data, &acc)
|
||||
if err != nil {
|
||||
return Account{}, errors.Wrap(err, ErrUnmarshal.Error())
|
||||
}
|
||||
|
||||
return acc, err
|
||||
}
|
||||
+55
-108
@@ -1,116 +1,86 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
)
|
||||
|
||||
type LoginAddressResult struct {
|
||||
var (
|
||||
ErrAuthCodeRequired = errors.New("auth code is required")
|
||||
)
|
||||
|
||||
type LoginInput struct {
|
||||
Email string
|
||||
Password string
|
||||
AuthCode string
|
||||
}
|
||||
|
||||
type LoginOutput struct {
|
||||
Account Account
|
||||
}
|
||||
|
||||
func (t *appstore) Login(input LoginInput) (LoginOutput, error) {
|
||||
macAddr, err := t.machine.MacAddress()
|
||||
if err != nil {
|
||||
return LoginOutput{}, fmt.Errorf("failed to get mac address: %w", err)
|
||||
}
|
||||
|
||||
guid := strings.ReplaceAll(strings.ToUpper(macAddr), ":", "")
|
||||
|
||||
acc, err := t.login(input.Email, input.Password, input.AuthCode, guid, 0)
|
||||
if err != nil {
|
||||
return LoginOutput{}, err
|
||||
}
|
||||
|
||||
return LoginOutput{
|
||||
Account: acc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type loginAddressResult struct {
|
||||
FirstName string `plist:"firstName,omitempty"`
|
||||
LastName string `plist:"lastName,omitempty"`
|
||||
}
|
||||
|
||||
type LoginAccountResult struct {
|
||||
type loginAccountResult struct {
|
||||
Email string `plist:"appleId,omitempty"`
|
||||
Address LoginAddressResult `plist:"address,omitempty"`
|
||||
Address loginAddressResult `plist:"address,omitempty"`
|
||||
}
|
||||
|
||||
type LoginResult struct {
|
||||
type loginResult struct {
|
||||
FailureType string `plist:"failureType,omitempty"`
|
||||
CustomerMessage string `plist:"customerMessage,omitempty"`
|
||||
Account LoginAccountResult `plist:"accountInfo,omitempty"`
|
||||
Account loginAccountResult `plist:"accountInfo,omitempty"`
|
||||
DirectoryServicesID string `plist:"dsPersonId,omitempty"`
|
||||
PasswordToken string `plist:"passwordToken,omitempty"`
|
||||
}
|
||||
|
||||
func (a *appstore) Login(email, password, authCode string) error {
|
||||
if password == "" && !a.interactive {
|
||||
return ErrPasswordRequired
|
||||
}
|
||||
func (t *appstore) login(email, password, authCode, guid string, attempt int) (Account, error) {
|
||||
request := t.loginRequest(email, password, authCode, guid)
|
||||
res, err := t.loginClient.Send(request)
|
||||
|
||||
if password == "" && a.interactive {
|
||||
a.logger.Log().Msg("enter password:")
|
||||
|
||||
var err error
|
||||
password, err = a.promptForPassword()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetData.Error())
|
||||
}
|
||||
}
|
||||
|
||||
macAddr, err := a.machine.MacAddress()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetMAC.Error())
|
||||
}
|
||||
|
||||
guid := strings.ReplaceAll(strings.ToUpper(macAddr), ":", "")
|
||||
a.logger.Verbose().Str("mac", macAddr).Str("guid", guid).Send()
|
||||
|
||||
acc, err := a.login(email, password, authCode, guid, 0, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrLogin.Error())
|
||||
}
|
||||
|
||||
a.logger.Log().
|
||||
Str("name", acc.Name).
|
||||
Str("email", acc.Email).
|
||||
Bool("success", true).
|
||||
Send()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) login(email, password, authCode, guid string, attempt int, failOnAuthCodeRequirement bool) (Account, error) {
|
||||
a.logger.Verbose().
|
||||
Int("attempt", attempt).
|
||||
Str("password", password).
|
||||
Str("email", email).
|
||||
Str("authCode", util.IfEmpty(authCode, "<nil>")).
|
||||
Msg("sending login request")
|
||||
|
||||
request := a.loginRequest(email, password, authCode, guid)
|
||||
res, err := a.loginClient.Send(request)
|
||||
if err != nil {
|
||||
return Account{}, errors.Wrap(err, ErrRequest.Error())
|
||||
return Account{}, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
if attempt == 0 && res.Data.FailureType == FailureTypeInvalidCredentials {
|
||||
return a.login(email, password, authCode, guid, 1, failOnAuthCodeRequirement)
|
||||
return t.login(email, password, authCode, guid, 1)
|
||||
}
|
||||
|
||||
if res.Data.FailureType != "" && res.Data.CustomerMessage != "" {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return Account{}, errors.New(res.Data.CustomerMessage)
|
||||
return Account{}, NewErrorWithMetadata(errors.New(res.Data.CustomerMessage), res)
|
||||
}
|
||||
|
||||
if res.Data.FailureType != "" {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return Account{}, ErrGeneric
|
||||
return Account{}, NewErrorWithMetadata(errors.New("something went wrong"), res)
|
||||
}
|
||||
|
||||
if res.Data.FailureType == "" && authCode == "" && res.Data.CustomerMessage == CustomerMessageBadLogin {
|
||||
if failOnAuthCodeRequirement {
|
||||
return Account{}, ErrAuthCodeRequired
|
||||
}
|
||||
|
||||
if a.interactive {
|
||||
a.logger.Log().Msg("enter 2FA code:")
|
||||
authCode, err = a.promptForAuthCode()
|
||||
if err != nil {
|
||||
return Account{}, errors.Wrap(err, ErrGetData.Error())
|
||||
}
|
||||
|
||||
return a.login(email, password, authCode, guid, 0, failOnAuthCodeRequirement)
|
||||
} else {
|
||||
a.logger.Log().Msg("2FA code is required; run the command again and supply a code using the `--auth-code` flag")
|
||||
return Account{}, nil
|
||||
}
|
||||
return Account{}, ErrAuthCodeRequired
|
||||
}
|
||||
|
||||
addr := res.Data.Account.Address
|
||||
@@ -125,18 +95,18 @@ func (a *appstore) login(email, password, authCode, guid string, attempt int, fa
|
||||
|
||||
data, err := json.Marshal(acc)
|
||||
if err != nil {
|
||||
return Account{}, errors.Wrap(err, ErrMarshal.Error())
|
||||
return Account{}, fmt.Errorf("failed to marshal json: %w", err)
|
||||
}
|
||||
|
||||
err = a.keychain.Set("account", data)
|
||||
err = t.keychain.Set("account", data)
|
||||
if err != nil {
|
||||
return Account{}, errors.Wrap(err, ErrSetKeychainItem.Error())
|
||||
return Account{}, fmt.Errorf("failed to save account in keychain: %w", err)
|
||||
}
|
||||
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
func (a *appstore) loginRequest(email, password, authCode, guid string) http.Request {
|
||||
func (t *appstore) loginRequest(email, password, authCode, guid string) http.Request {
|
||||
attempt := "4"
|
||||
if authCode != "" {
|
||||
attempt = "2"
|
||||
@@ -144,10 +114,9 @@ func (a *appstore) loginRequest(email, password, authCode, guid string) http.Req
|
||||
|
||||
return http.Request{
|
||||
Method: http.MethodPOST,
|
||||
URL: a.authDomain(authCode, guid),
|
||||
URL: t.authDomain(authCode, guid),
|
||||
ResponseFormat: http.ResponseFormatXML,
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "Configurator/2.15 (Macintosh; OperatingSystem X 11.0.0; 16G29) AppleWebKit/2603.3.8",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
Payload: &http.XMLPayload{
|
||||
@@ -164,34 +133,12 @@ func (a *appstore) loginRequest(email, password, authCode, guid string) http.Req
|
||||
}
|
||||
}
|
||||
|
||||
func (a *appstore) promptForAuthCode() (string, error) {
|
||||
reader := bufio.NewReader(a.ioReader)
|
||||
authCode, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetData.Error())
|
||||
}
|
||||
|
||||
authCode = strings.Trim(authCode, "\n")
|
||||
authCode = strings.Trim(authCode, "\r")
|
||||
|
||||
return authCode, nil
|
||||
}
|
||||
|
||||
func (*appstore) authDomain(authCode, guid string) string {
|
||||
prefix := PriavteAppStoreAPIDomainPrefixWithoutAuthCode
|
||||
prefix := PrivateAppStoreAPIDomainPrefixWithoutAuthCode
|
||||
if authCode != "" {
|
||||
prefix = PriavteAppStoreAPIDomainPrefixWithAuthCode
|
||||
prefix = PrivateAppStoreAPIDomainPrefixWithAuthCode
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"https://%s-%s%s?guid=%s", prefix, PrivateAppStoreAPIDomain, PrivateAppStoreAPIPathAuthenticate, guid)
|
||||
}
|
||||
|
||||
func (a *appstore) promptForPassword() (string, error) {
|
||||
password, err := a.machine.ReadPassword(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetData.Error())
|
||||
}
|
||||
|
||||
return string(password), nil
|
||||
}
|
||||
@@ -2,124 +2,66 @@ package appstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
"github.com/majd/ipatool/v2/pkg/util/machine"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (Login)", func() {
|
||||
const (
|
||||
testPassword = "test-password"
|
||||
testPassword = "test-password"
|
||||
testEmail = "test-email"
|
||||
testFirstName = "test-first-name"
|
||||
testLastName = "test-last-name"
|
||||
)
|
||||
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
as AppStore
|
||||
mockKeychain *keychain.MockKeychain
|
||||
mockClient *http.MockClient[LoginResult]
|
||||
mockMachine *util.MockMachine
|
||||
mockLogger *log.MockLogger
|
||||
testErr = errors.New("test")
|
||||
mockClient *http.MockClient[loginResult]
|
||||
mockMachine *machine.MockMachine
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockClient = http.NewMockClient[LoginResult](ctrl)
|
||||
mockMachine = util.NewMockMachine(ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
mockClient = http.NewMockClient[loginResult](ctrl)
|
||||
mockMachine = machine.NewMockMachine(ctrl)
|
||||
as = &appstore{
|
||||
keychain: mockKeychain,
|
||||
loginClient: mockClient,
|
||||
ioReader: os.Stdin,
|
||||
machine: mockMachine,
|
||||
logger: mockLogger,
|
||||
interactive: true,
|
||||
}
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
MaxTimes(4)
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("not running in interactive mode and password is not supplied", func() {
|
||||
BeforeEach(func() {
|
||||
as.(*appstore).interactive = false
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Login("", "", "")
|
||||
Expect(err).To(MatchError(ContainSubstring("password is required")))
|
||||
})
|
||||
})
|
||||
|
||||
When("prompts user for password", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
When("user enters password", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", errors.New("success"))
|
||||
|
||||
mockMachine.EXPECT().
|
||||
ReadPassword(syscall.Stdin).
|
||||
Return([]byte(testPassword), nil)
|
||||
})
|
||||
|
||||
It("succeeds", func() {
|
||||
err := as.Login("", "", "")
|
||||
Expect(err).To(MatchError(ContainSubstring("success")))
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to read password from stdin", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
ReadPassword(syscall.Stdin).
|
||||
Return(nil, testErr)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Login("", "", "")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetData.Error())))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to read Machine's MAC address", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", testErr)
|
||||
Return("", errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetMAC.Error())))
|
||||
_, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("sucessfully reads Machine's MAC address", func() {
|
||||
When("successfully reads machine's MAC address", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
@@ -130,23 +72,23 @@ var _ = Describe("AppStore (Login)", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{}, testErr)
|
||||
Return(http.Result[loginResult]{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
_, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("store API returns invalid first response", func() {
|
||||
const testCustomerMessage = "test"
|
||||
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
Return(http.Result[loginResult]{
|
||||
Data: loginResult{
|
||||
FailureType: FailureTypeInvalidCredentials,
|
||||
CustomerMessage: "test",
|
||||
},
|
||||
@@ -155,8 +97,10 @@ var _ = Describe("AppStore (Login)", func() {
|
||||
})
|
||||
|
||||
It("retries one more time", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).To(MatchError(ContainSubstring(testCustomerMessage)))
|
||||
_, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -164,105 +108,43 @@ var _ = Describe("AppStore (Login)", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
Return(http.Result[loginResult]{
|
||||
Data: loginResult{
|
||||
FailureType: "random-error",
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGeneric.Error())))
|
||||
_, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("store API requires 2FA code", func() {
|
||||
When("not running in interactive mode", func() {
|
||||
BeforeEach(func() {
|
||||
as.(*appstore).interactive = false
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
FailureType: "",
|
||||
CustomerMessage: CustomerMessageBadLogin,
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("prints message", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[loginResult]{
|
||||
Data: loginResult{
|
||||
FailureType: "",
|
||||
CustomerMessage: CustomerMessageBadLogin,
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
When("user enters 2FA code", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Set("account", gomock.Any()).
|
||||
Return(nil)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
FailureType: "",
|
||||
CustomerMessage: CustomerMessageBadLogin,
|
||||
},
|
||||
}, nil).
|
||||
Times(2)
|
||||
|
||||
as.(*appstore).ioReader = strings.NewReader("123456\n")
|
||||
})
|
||||
|
||||
It("successfully authenticates", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("prompts user for 2FA code", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
FailureType: "",
|
||||
CustomerMessage: CustomerMessageBadLogin,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
as.(*appstore).ioReader = strings.NewReader("123456")
|
||||
})
|
||||
|
||||
It("fails to read 2FA code from stdin", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetData.Error())))
|
||||
It("returns error", func() {
|
||||
_, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("store API returns valid response", func() {
|
||||
const (
|
||||
testEmail = "test-email"
|
||||
testFirstName = "test-first-name"
|
||||
testLastName = "test-last-name"
|
||||
testPasswordToken = "test-password-token"
|
||||
testDirectoryServicesID = "directory-services-id"
|
||||
)
|
||||
@@ -270,13 +152,13 @@ var _ = Describe("AppStore (Login)", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
Return(http.Result[loginResult]{
|
||||
Data: loginResult{
|
||||
PasswordToken: testPasswordToken,
|
||||
DirectoryServicesID: testDirectoryServicesID,
|
||||
Account: LoginAccountResult{
|
||||
Account: loginAccountResult{
|
||||
Email: testEmail,
|
||||
Address: LoginAddressResult{
|
||||
Address: loginAddressResult{
|
||||
FirstName: testFirstName,
|
||||
LastName: testLastName,
|
||||
},
|
||||
@@ -303,22 +185,19 @@ var _ = Describe("AppStore (Login)", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(got).To(Equal(want))
|
||||
}).
|
||||
Return(testErr)
|
||||
Return(errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrSetKeychainItem.Error())))
|
||||
_, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("sucessfully saves account in keychain", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Set("account", gomock.Any()).
|
||||
Do(func(key string, data []byte) {
|
||||
@@ -339,8 +218,12 @@ var _ = Describe("AppStore (Login)", func() {
|
||||
})
|
||||
|
||||
It("returns nil", func() {
|
||||
err := as.Login("", testPassword, "")
|
||||
out, err := as.Login(LoginInput{
|
||||
Password: testPassword,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(out.Account.Email).To(Equal(testEmail))
|
||||
Expect(out.Account.Name).To(Equal(strings.Join([]string{testFirstName, testLastName}, " ")))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,45 +1,58 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/pkg/errors"
|
||||
gohttp "net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
)
|
||||
|
||||
func (a *appstore) lookup(bundleID, countryCode string) (App, error) {
|
||||
if StoreFronts[countryCode] == "" {
|
||||
return App{}, ErrInvalidCountryCode
|
||||
}
|
||||
type LookupInput struct {
|
||||
Account Account
|
||||
BundleID string
|
||||
}
|
||||
|
||||
request := a.lookupRequest(bundleID, countryCode)
|
||||
type LookupOutput struct {
|
||||
App App
|
||||
}
|
||||
|
||||
res, err := a.searchClient.Send(request)
|
||||
func (t *appstore) Lookup(input LookupInput) (LookupOutput, error) {
|
||||
countryCode, err := countryCodeFromStoreFront(input.Account.StoreFront)
|
||||
if err != nil {
|
||||
return App{}, errors.Wrap(err, ErrRequest.Error())
|
||||
return LookupOutput{}, fmt.Errorf("failed to reoslve the country code: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
a.logger.Verbose().Interface("data", res.Data).Int("status", res.StatusCode).Send()
|
||||
return App{}, ErrRequest
|
||||
request := t.lookupRequest(input.BundleID, countryCode)
|
||||
|
||||
res, err := t.searchClient.Send(request)
|
||||
if err != nil {
|
||||
return LookupOutput{}, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != gohttp.StatusOK {
|
||||
return LookupOutput{}, NewErrorWithMetadata(errors.New("invalid response"), res)
|
||||
}
|
||||
|
||||
if len(res.Data.Results) == 0 {
|
||||
return App{}, ErrAppNotFound
|
||||
return LookupOutput{}, errors.New("app not found")
|
||||
}
|
||||
|
||||
return res.Data.Results[0], nil
|
||||
return LookupOutput{
|
||||
App: res.Data.Results[0],
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *appstore) lookupRequest(bundleID, countryCode string) http.Request {
|
||||
func (t *appstore) lookupRequest(bundleID, countryCode string) http.Request {
|
||||
return http.Request{
|
||||
URL: a.lookupURL(bundleID, countryCode),
|
||||
URL: t.lookupURL(bundleID, countryCode),
|
||||
Method: http.MethodGET,
|
||||
ResponseFormat: http.ResponseFormatJSON,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *appstore) lookupURL(bundleID, countryCode string) string {
|
||||
func (t *appstore) lookupURL(bundleID, countryCode string) string {
|
||||
params := url.Values{}
|
||||
params.Add("entity", "software,iPadSoftware")
|
||||
params.Add("limit", "1")
|
||||
|
||||
@@ -1,31 +1,26 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (Lookup)", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
mockClient *http.MockClient[SearchResult]
|
||||
mockLogger *log.MockLogger
|
||||
as *appstore
|
||||
mockClient *http.MockClient[searchResult]
|
||||
as AppStore
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockClient = http.NewMockClient[SearchResult](ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
mockClient = http.NewMockClient[searchResult](ctrl)
|
||||
as = &appstore{
|
||||
searchClient: mockClient,
|
||||
ioReader: os.Stdin,
|
||||
logger: mockLogger,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -33,56 +28,14 @@ var _ = Describe("AppStore (Lookup)", func() {
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("country code is invalid", func() {
|
||||
It("returns error", func() {
|
||||
_, err := as.lookup("", "XYZ")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrInvalidCountryCode.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("request fails", func() {
|
||||
var testErr = errors.New("test")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{}, testErr)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
_, err := as.lookup("", "US")
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("request returns bad status code", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 400,
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
_, err := as.lookup("", "US")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("request is successful", func() {
|
||||
When("does not find app", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
Return(http.Result[searchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Data: searchResult{
|
||||
Count: 0,
|
||||
Results: []App{},
|
||||
},
|
||||
@@ -90,8 +43,12 @@ var _ = Describe("AppStore (Lookup)", func() {
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
_, err := as.lookup("", "US")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrAppNotFound.Error())))
|
||||
_, err := as.Lookup(LookupInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -107,9 +64,9 @@ var _ = Describe("AppStore (Lookup)", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
Return(http.Result[searchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Data: searchResult{
|
||||
Count: 1,
|
||||
Results: []App{testApp},
|
||||
},
|
||||
@@ -117,10 +74,61 @@ var _ = Describe("AppStore (Lookup)", func() {
|
||||
})
|
||||
|
||||
It("returns app", func() {
|
||||
app, err := as.lookup("", "US")
|
||||
app, err := as.Lookup(LookupInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(app).To(Equal(testApp))
|
||||
Expect(app).To(Equal(LookupOutput{App: testApp}))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("store front is invalid", func() {
|
||||
It("returns error", func() {
|
||||
_, err := as.Lookup(LookupInput{
|
||||
Account: Account{
|
||||
StoreFront: "xyz",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("request fails", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[searchResult]{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
_, err := as.Lookup(LookupInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("request returns bad status code", func() {
|
||||
BeforeEach(func() {
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[searchResult]{
|
||||
StatusCode: 400,
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
_, err := as.Lookup(LookupInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,118 +1,106 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/pkg/errors"
|
||||
gohttp "net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
)
|
||||
|
||||
type PurchaseResult struct {
|
||||
var (
|
||||
ErrPasswordTokenExpired = errors.New("password token is expired")
|
||||
ErrSubscriptionRequired = errors.New("subscription required")
|
||||
ErrTemporarilyUnavailable = errors.New("item is temporarily unavailable")
|
||||
)
|
||||
|
||||
type PurchaseInput struct {
|
||||
Account Account
|
||||
App App
|
||||
}
|
||||
|
||||
func (t *appstore) Purchase(input PurchaseInput) error {
|
||||
macAddr, err := t.machine.MacAddress()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get mac address: %w", err)
|
||||
}
|
||||
|
||||
guid := strings.ReplaceAll(strings.ToUpper(macAddr), ":", "")
|
||||
|
||||
if input.App.Price > 0 {
|
||||
return errors.New("purchasing paid apps is not supported")
|
||||
}
|
||||
|
||||
err = t.purchaseWithParams(input.Account, input.App, guid, PricingParameterAppStore)
|
||||
if err != nil {
|
||||
if err == ErrTemporarilyUnavailable {
|
||||
err = t.purchaseWithParams(input.Account, input.App, guid, PricingParameterAppleArcade)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to purchase item with param '%s': %w", PricingParameterAppleArcade, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to purchase item with param '%s': %w", PricingParameterAppStore, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type purchaseResult struct {
|
||||
FailureType string `plist:"failureType,omitempty"`
|
||||
CustomerMessage string `plist:"customerMessage,omitempty"`
|
||||
JingleDocType string `plist:"jingleDocType,omitempty"`
|
||||
Status int `plist:"status,omitempty"`
|
||||
}
|
||||
|
||||
func (a *appstore) Purchase(bundleID string) error {
|
||||
macAddr, err := a.machine.MacAddress()
|
||||
func (t *appstore) purchaseWithParams(acc Account, app App, guid string, pricingParameters string) error {
|
||||
req := t.purchaseRequest(acc, app, acc.StoreFront, guid, pricingParameters)
|
||||
res, err := t.purchaseClient.Send(req)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetMAC.Error())
|
||||
return fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
guid := strings.ReplaceAll(strings.ToUpper(macAddr), ":", "")
|
||||
a.logger.Verbose().Str("mac", macAddr).Str("guid", guid).Send()
|
||||
|
||||
err = a.purchase(bundleID, guid, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrPurchase.Error())
|
||||
if res.Data.FailureType == FailureTypeTemporarilyUnavailable {
|
||||
return ErrTemporarilyUnavailable
|
||||
}
|
||||
|
||||
a.logger.Log().Bool("success", true).Send()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) purchase(bundleID string, guid string, attemptToRenewCredentials bool) error {
|
||||
acc, err := a.account()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetAccount.Error())
|
||||
}
|
||||
|
||||
countryCode, err := a.countryCodeFromStoreFront(acc.StoreFront)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrInvalidCountryCode.Error())
|
||||
}
|
||||
|
||||
app, err := a.lookup(bundleID, countryCode)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrAppLookup.Error())
|
||||
}
|
||||
|
||||
if app.Price > 0 {
|
||||
return ErrPaidApp
|
||||
}
|
||||
|
||||
req := a.purchaseRequest(acc, app, acc.StoreFront, guid)
|
||||
res, err := a.purchaseClient.Send(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRequest.Error())
|
||||
if res.Data.CustomerMessage == CustomerMessageSubscriptionRequired {
|
||||
return ErrSubscriptionRequired
|
||||
}
|
||||
|
||||
if res.Data.FailureType == FailureTypePasswordTokenExpired {
|
||||
if attemptToRenewCredentials {
|
||||
a.logger.Verbose().Msg("retrieving new password token")
|
||||
acc, err = a.login(acc.Email, acc.Password, "", guid, 0, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrPasswordTokenExpired.Error())
|
||||
}
|
||||
|
||||
return a.purchase(bundleID, guid, false)
|
||||
}
|
||||
|
||||
return ErrPasswordTokenExpired
|
||||
}
|
||||
|
||||
if res.Data.FailureType != "" && res.Data.CustomerMessage != "" {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return errors.New(res.Data.CustomerMessage)
|
||||
return NewErrorWithMetadata(errors.New(res.Data.CustomerMessage), res)
|
||||
}
|
||||
|
||||
if res.Data.FailureType != "" {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return ErrGeneric
|
||||
return NewErrorWithMetadata(errors.New("something went wrong"), res)
|
||||
}
|
||||
|
||||
if res.StatusCode == 500 {
|
||||
return ErrLicenseExists
|
||||
if res.StatusCode == gohttp.StatusInternalServerError {
|
||||
return fmt.Errorf("license already exists")
|
||||
}
|
||||
|
||||
if res.Data.JingleDocType != "purchaseSuccess" || res.Data.Status != 0 {
|
||||
a.logger.Verbose().Interface("response", res).Send()
|
||||
return errors.New(ErrPurchase.Error())
|
||||
return NewErrorWithMetadata(errors.New("failed to purchase app"), res)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*appstore) countryCodeFromStoreFront(storeFront string) (string, error) {
|
||||
for key, val := range StoreFronts {
|
||||
parts := strings.Split(storeFront, "-")
|
||||
|
||||
if len(parts) >= 1 && parts[0] == val {
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New(ErrInvalidStoreFront.Error())
|
||||
}
|
||||
|
||||
func (a *appstore) purchaseRequest(acc Account, app App, storeFront, guid string) http.Request {
|
||||
func (t *appstore) purchaseRequest(acc Account, app App, storeFront, guid string, pricingParameters string) http.Request {
|
||||
return http.Request{
|
||||
URL: fmt.Sprintf("https://%s%s", PrivateAppStoreAPIDomain, PrivateAppStoreAPIPathPurchase),
|
||||
Method: http.MethodPOST,
|
||||
ResponseFormat: http.ResponseFormatXML,
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "Configurator/2.15 (Macintosh; OperatingSystem X 11.0.0; 16G29) AppleWebKit/2603.3.8",
|
||||
"Content-Type": "application/x-apple-plist",
|
||||
"iCloud-DSID": acc.DirectoryServicesID,
|
||||
"X-Dsid": acc.DirectoryServicesID,
|
||||
@@ -130,7 +118,7 @@ func (a *appstore) purchaseRequest(acc Account, app App, storeFront, guid string
|
||||
"origPage": fmt.Sprintf("Software-%d", app.ID),
|
||||
"origPageLocation": "Buy",
|
||||
"price": "0",
|
||||
"pricingParameters": "STDQ",
|
||||
"pricingParameters": pricingParameters,
|
||||
"productType": "C",
|
||||
"salableAdamId": app.ID,
|
||||
},
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/pkg/util"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
"github.com/majd/ipatool/v2/pkg/util/machine"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
@@ -14,29 +15,23 @@ var _ = Describe("AppStore (Purchase)", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
mockKeychain *keychain.MockKeychain
|
||||
mockMachine *util.MockMachine
|
||||
mockLogger *log.MockLogger
|
||||
mockPurchaseClient *http.MockClient[PurchaseResult]
|
||||
mockSearchClient *http.MockClient[SearchResult]
|
||||
mockLoginClient *http.MockClient[LoginResult]
|
||||
mockMachine *machine.MockMachine
|
||||
mockPurchaseClient *http.MockClient[purchaseResult]
|
||||
mockLoginClient *http.MockClient[loginResult]
|
||||
as *appstore
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockPurchaseClient = http.NewMockClient[purchaseResult](ctrl)
|
||||
mockLoginClient = http.NewMockClient[loginResult](ctrl)
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockPurchaseClient = http.NewMockClient[PurchaseResult](ctrl)
|
||||
mockSearchClient = http.NewMockClient[SearchResult](ctrl)
|
||||
mockLoginClient = http.NewMockClient[LoginResult](ctrl)
|
||||
mockMachine = util.NewMockMachine(ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
mockMachine = machine.NewMockMachine(ctrl)
|
||||
as = &appstore{
|
||||
keychain: mockKeychain,
|
||||
purchaseClient: mockPurchaseClient,
|
||||
searchClient: mockSearchClient,
|
||||
loginClient: mockLoginClient,
|
||||
machine: mockMachine,
|
||||
logger: mockLogger,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -48,524 +43,265 @@ var _ = Describe("AppStore (Purchase)", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("", ErrGetMAC)
|
||||
Return("", errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetMAC.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("not logged in", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return(nil, ErrGetKeychainItem)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetKeychainItem.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetAccount.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("country code is invalid", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{}"), nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrInvalidCountryCode.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("app lookup fails", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{}, ErrRequest)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrAppLookup.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
err := as.Purchase(PurchaseInput{})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("app is paid", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
Price: 0.99,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPaidApp.Error())))
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
App: App{
|
||||
Price: 0.99,
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("purchase request fails", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{}, ErrRequest)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
Return(http.Result[purchaseResult]{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("password token is expired", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Data: PurchaseResult{
|
||||
Return(http.Result[purchaseResult]{
|
||||
Data: purchaseResult{
|
||||
FailureType: FailureTypePasswordTokenExpired,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
When("renewing credentials fails", func() {
|
||||
BeforeEach(func() {
|
||||
mockLoginClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{
|
||||
FailureType: "",
|
||||
CustomerMessage: CustomerMessageBadLogin,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPasswordTokenExpired.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("renewing credentials succeeds", func() {
|
||||
BeforeEach(func() {
|
||||
mockLoginClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[LoginResult]{
|
||||
Data: LoginResult{},
|
||||
}, nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Set("account", gomock.Any()).
|
||||
Return(nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Data: PurchaseResult{
|
||||
FailureType: FailureTypePasswordTokenExpired,
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("attempts to purcahse app", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPasswordTokenExpired.Error())))
|
||||
It("returns error", func() {
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("store API returns customer error message", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Data: PurchaseResult{
|
||||
Return(http.Result[purchaseResult]{
|
||||
Data: purchaseResult{
|
||||
FailureType: "failure",
|
||||
CustomerMessage: CustomerMessageBadLogin,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(CustomerMessageBadLogin)))
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("store API returns unknown error", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Data: PurchaseResult{
|
||||
Return(http.Result[purchaseResult]{
|
||||
Data: purchaseResult{
|
||||
FailureType: "failure",
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGeneric.Error())))
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("account already has a license for the app", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Return(http.Result[purchaseResult]{
|
||||
StatusCode: 500,
|
||||
Data: PurchaseResult{},
|
||||
Data: purchaseResult{},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrLicenseExists.Error())))
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("sucessfully purchases the app", func() {
|
||||
When("subscription is required", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Send(pricingParametersMatcher{"STDQ"}).
|
||||
Return(http.Result[purchaseResult]{
|
||||
StatusCode: 200,
|
||||
Data: PurchaseResult{
|
||||
Data: purchaseResult{
|
||||
CustomerMessage: "This item is temporarily unavailable.",
|
||||
FailureType: FailureTypeTemporarilyUnavailable,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(pricingParametersMatcher{"GAME"}).
|
||||
Return(http.Result[purchaseResult]{
|
||||
StatusCode: 200,
|
||||
Data: purchaseResult{
|
||||
CustomerMessage: CustomerMessageSubscriptionRequired,
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("successfully purchases the app", func() {
|
||||
BeforeEach(func() {
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(pricingParametersMatcher{"STDQ"}).
|
||||
Return(http.Result[purchaseResult]{
|
||||
StatusCode: 200,
|
||||
Data: purchaseResult{
|
||||
CustomerMessage: "This item is temporarily unavailable.",
|
||||
FailureType: FailureTypeTemporarilyUnavailable,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(pricingParametersMatcher{"GAME"}).
|
||||
Return(http.Result[purchaseResult]{
|
||||
StatusCode: 200,
|
||||
Data: purchaseResult{
|
||||
JingleDocType: "purchaseSuccess",
|
||||
Status: 0,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
})
|
||||
|
||||
It("returns nil", func() {
|
||||
err := as.Purchase("")
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("purchasing the app fails", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockSearchClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: 0,
|
||||
BundleID: "",
|
||||
Name: "",
|
||||
Version: "",
|
||||
Price: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockMachine.EXPECT().
|
||||
MacAddress().
|
||||
Return("00:00:00:00:00:00", nil)
|
||||
|
||||
mockPurchaseClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[PurchaseResult]{
|
||||
Return(http.Result[purchaseResult]{
|
||||
StatusCode: 200,
|
||||
Data: PurchaseResult{
|
||||
Data: purchaseResult{
|
||||
JingleDocType: "failure",
|
||||
Status: -1,
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil).
|
||||
Times(2)
|
||||
})
|
||||
|
||||
It("returns nil", func() {
|
||||
err := as.Purchase("")
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrPurchase.Error())))
|
||||
err := as.Purchase(PurchaseInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
type pricingParametersMatcher struct {
|
||||
pricingParameters string
|
||||
}
|
||||
|
||||
func (p pricingParametersMatcher) Matches(in interface{}) bool {
|
||||
return in.(http.Request).Payload.(*http.XMLPayload).Content["pricingParameters"] == p.pricingParameters
|
||||
}
|
||||
|
||||
func (p pricingParametersMatcher) String() string {
|
||||
return "payload pricingParameters is " + p.pricingParameters
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/util"
|
||||
"howett.net/plist"
|
||||
)
|
||||
|
||||
type Sinf struct {
|
||||
ID int64 `plist:"id,omitempty"`
|
||||
Data []byte `plist:"sinf,omitempty"`
|
||||
}
|
||||
|
||||
type ReplicateSinfInput struct {
|
||||
Sinfs []Sinf
|
||||
PackagePath string
|
||||
}
|
||||
|
||||
func (t *appstore) ReplicateSinf(input ReplicateSinfInput) error {
|
||||
zipReader, err := zip.OpenReader(input.PackagePath)
|
||||
if err != nil {
|
||||
return errors.New("failed to open zip reader")
|
||||
}
|
||||
defer zipReader.Close()
|
||||
|
||||
tmpPath := fmt.Sprintf("%s.tmp", input.PackagePath)
|
||||
tmpFile, err := t.os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
|
||||
zipWriter := zip.NewWriter(tmpFile)
|
||||
defer zipWriter.Close()
|
||||
|
||||
err = t.replicateZip(zipReader, zipWriter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replicate zip: %w", err)
|
||||
}
|
||||
|
||||
bundleName, err := t.readBundleName(zipReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read bundle name: %w", err)
|
||||
}
|
||||
|
||||
manifest, err := t.readManifestPlist(zipReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read manifest plist: %w", err)
|
||||
}
|
||||
|
||||
info, err := t.readInfoPlist(zipReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read info plist: %w", err)
|
||||
}
|
||||
|
||||
if manifest != nil {
|
||||
err = t.replicateSinfFromManifest(*manifest, zipWriter, input.Sinfs, bundleName)
|
||||
} else {
|
||||
err = t.replicateSinfFromInfo(*info, zipWriter, input.Sinfs, bundleName)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replicate sinf: %w", err)
|
||||
}
|
||||
|
||||
err = t.os.Remove(input.PackagePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove original file: %w", err)
|
||||
}
|
||||
|
||||
err = t.os.Rename(tmpPath, input.PackagePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove original file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type packageManifest struct {
|
||||
SinfPaths []string `plist:"SinfPaths,omitempty"`
|
||||
}
|
||||
|
||||
type packageInfo struct {
|
||||
BundleExecutable string `plist:"CFBundleExecutable,omitempty"`
|
||||
}
|
||||
|
||||
func (*appstore) replicateSinfFromManifest(manifest packageManifest, zip *zip.Writer, sinfs []Sinf, bundleName string) error {
|
||||
zipped, err := util.Zip(sinfs, manifest.SinfPaths)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to zip sinfs: %w", err)
|
||||
}
|
||||
|
||||
for _, pair := range zipped {
|
||||
sp := fmt.Sprintf("Payload/%s.app/%s", bundleName, pair.Second)
|
||||
|
||||
file, err := zip.Create(sp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create file: %w", err)
|
||||
}
|
||||
|
||||
_, err = file.Write(pair.First.Data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write data: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *appstore) replicateSinfFromInfo(info packageInfo, zip *zip.Writer, sinfs []Sinf, bundleName string) error {
|
||||
sp := fmt.Sprintf("Payload/%s.app/SC_Info/%s.sinf", bundleName, info.BundleExecutable)
|
||||
|
||||
file, err := zip.Create(sp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create file: %w", err)
|
||||
}
|
||||
|
||||
_, err = file.Write(sinfs[0].Data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write data: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *appstore) replicateZip(src *zip.ReadCloser, dst *zip.Writer) error {
|
||||
for _, file := range src.File {
|
||||
srcFile, err := file.OpenRaw()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open raw file: %w", err)
|
||||
}
|
||||
|
||||
header := file.FileHeader
|
||||
dstFile, err := dst.CreateRaw(&header)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create raw file: %w", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(dstFile, srcFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to copy file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*appstore) readInfoPlist(reader *zip.ReadCloser) (*packageInfo, error) {
|
||||
for _, file := range reader.File {
|
||||
if strings.Contains(file.Name, ".app/Info.plist") {
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
|
||||
data := new(bytes.Buffer)
|
||||
_, err = io.Copy(data, src)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to copy data: %w", err)
|
||||
}
|
||||
|
||||
var info packageInfo
|
||||
_, err = plist.Unmarshal(data.Bytes(), &info)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal data: %w", err)
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*appstore) readManifestPlist(reader *zip.ReadCloser) (*packageManifest, error) {
|
||||
for _, file := range reader.File {
|
||||
if strings.HasSuffix(file.Name, ".app/SC_Info/Manifest.plist") {
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
|
||||
data := new(bytes.Buffer)
|
||||
_, err = io.Copy(data, src)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to copy data: %w", err)
|
||||
}
|
||||
|
||||
var manifest packageManifest
|
||||
|
||||
_, err = plist.Unmarshal(data.Bytes(), &manifest)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal data: %w", err)
|
||||
}
|
||||
|
||||
return &manifest, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*appstore) readBundleName(reader *zip.ReadCloser) (string, error) {
|
||||
var bundleName string
|
||||
|
||||
for _, file := range reader.File {
|
||||
if strings.Contains(file.Name, ".app/Info.plist") && !strings.Contains(file.Name, "/Watch/") {
|
||||
bundleName = filepath.Base(strings.TrimSuffix(file.Name, ".app/Info.plist"))
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if bundleName == "" {
|
||||
return "", errors.New("could not read bundle name")
|
||||
}
|
||||
|
||||
return bundleName, nil
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
"github.com/majd/ipatool/v2/pkg/util/machine"
|
||||
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"howett.net/plist"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (ReplicateSinf)", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
mockKeychain *keychain.MockKeychain
|
||||
mockDownloadClient *http.MockClient[downloadResult]
|
||||
mockPurchaseClient *http.MockClient[purchaseResult]
|
||||
mockLoginClient *http.MockClient[loginResult]
|
||||
mockHTTPClient *http.MockClient[interface{}]
|
||||
mockOS *operatingsystem.MockOperatingSystem
|
||||
mockMachine *machine.MockMachine
|
||||
as AppStore
|
||||
testFile *os.File
|
||||
testZip *zip.Writer
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockDownloadClient = http.NewMockClient[downloadResult](ctrl)
|
||||
mockLoginClient = http.NewMockClient[loginResult](ctrl)
|
||||
mockPurchaseClient = http.NewMockClient[purchaseResult](ctrl)
|
||||
mockHTTPClient = http.NewMockClient[interface{}](ctrl)
|
||||
mockOS = operatingsystem.NewMockOperatingSystem(ctrl)
|
||||
mockMachine = machine.NewMockMachine(ctrl)
|
||||
as = &appstore{
|
||||
keychain: mockKeychain,
|
||||
loginClient: mockLoginClient,
|
||||
purchaseClient: mockPurchaseClient,
|
||||
downloadClient: mockDownloadClient,
|
||||
httpClient: mockHTTPClient,
|
||||
machine: mockMachine,
|
||||
os: mockOS,
|
||||
}
|
||||
|
||||
var err error
|
||||
testFile, err = os.CreateTemp("", "test_file")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
testZip = zip.NewWriter(testFile)
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
testZip.Close()
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
err := os.Remove(testFile.Name())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("app includes codesign manifest", func() {
|
||||
BeforeEach(func() {
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
DoAndReturn(os.OpenFile)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Remove(testFile.Name()).
|
||||
Return(nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Rename(fmt.Sprintf("%s.tmp", testFile.Name()), testFile.Name()).
|
||||
Return(nil)
|
||||
|
||||
manifest, err := plist.Marshal(packageManifest{
|
||||
SinfPaths: []string{
|
||||
"SC_Info/TestApp.sinf",
|
||||
},
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
w, err := testZip.Create("Payload/Test.app/SC_Info/Manifest.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(manifest)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
w, err = testZip.Create("Payload/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info, err := plist.Marshal(map[string]interface{}{
|
||||
"CFBundleExecutable": "Test",
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(info)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
w, err = testZip.Create("Payload/Test.app/Watch/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
watchInfo, err := plist.Marshal(map[string]interface{}{
|
||||
"WKWatchKitApp": true,
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(watchInfo)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("replicates sinf from manifest plist", func() {
|
||||
err := as.ReplicateSinf(ReplicateSinfInput{
|
||||
PackagePath: testFile.Name(),
|
||||
Sinfs: []Sinf{
|
||||
{
|
||||
ID: 0,
|
||||
Data: []byte(""),
|
||||
},
|
||||
},
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("app does not include codesign manifest", func() {
|
||||
BeforeEach(func() {
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
DoAndReturn(os.OpenFile)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Remove(testFile.Name()).
|
||||
Return(nil)
|
||||
|
||||
mockOS.EXPECT().
|
||||
Rename(fmt.Sprintf("%s.tmp", testFile.Name()), testFile.Name()).
|
||||
Return(nil)
|
||||
|
||||
w, err := testZip.Create("Payload/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
info, err := plist.Marshal(map[string]interface{}{
|
||||
"CFBundleExecutable": "Test",
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(info)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
w, err = testZip.Create("Payload/Test.app/Watch/Test.app/Info.plist")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
watchInfo, err := plist.Marshal(map[string]interface{}{
|
||||
"WKWatchKitApp": true,
|
||||
}, plist.BinaryFormat)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = w.Write(watchInfo)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("replicates sinf", func() {
|
||||
err := as.ReplicateSinf(ReplicateSinfInput{
|
||||
PackagePath: testFile.Name(),
|
||||
Sinfs: []Sinf{
|
||||
{
|
||||
ID: 0,
|
||||
Data: []byte(""),
|
||||
},
|
||||
},
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("fails to open file", func() {
|
||||
BeforeEach(func() {
|
||||
mockOS.EXPECT().
|
||||
OpenFile(gomock.Any(), gomock.Any(), gomock.Any()).
|
||||
Return(nil, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.ReplicateSinf(ReplicateSinfInput{
|
||||
PackagePath: testFile.Name(),
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,15 +1,14 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (a *appstore) Revoke() error {
|
||||
err := a.keychain.Remove("account")
|
||||
func (t *appstore) Revoke() error {
|
||||
err := t.keychain.Remove("account")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRemoveKeychainItem.Error())
|
||||
return fmt.Errorf("failed to remove account from keychain: %w", err)
|
||||
}
|
||||
|
||||
a.logger.Log().Bool("success", true).Send()
|
||||
return nil
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/v2/pkg/keychain"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (Revoke)", func() {
|
||||
@@ -14,16 +14,13 @@ var _ = Describe("AppStore (Revoke)", func() {
|
||||
ctrl *gomock.Controller
|
||||
appstore AppStore
|
||||
mockKeychain *keychain.MockKeychain
|
||||
mockLogger *log.MockLogger
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
appstore = NewAppStore(AppStoreArgs{
|
||||
appstore = NewAppStore(Args{
|
||||
Keychain: mockKeychain,
|
||||
Logger: mockLogger,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -31,28 +28,8 @@ var _ = Describe("AppStore (Revoke)", func() {
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("keychain returns error", func() {
|
||||
var testErr = errors.New("test error")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Remove("account").
|
||||
Return(testErr)
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
err := appstore.Revoke()
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRemoveKeychainItem.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("keychain removes item", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Remove("account").
|
||||
Return(nil)
|
||||
@@ -63,4 +40,17 @@ var _ = Describe("AppStore (Revoke)", func() {
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("keychain returns error", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Remove("account").
|
||||
Return(errors.New(""))
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
err := appstore.Revoke()
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,54 +1,63 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/pkg/errors"
|
||||
gohttp "net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
)
|
||||
|
||||
type SearchResult struct {
|
||||
type SearchInput struct {
|
||||
Account Account
|
||||
Term string
|
||||
Limit int64
|
||||
}
|
||||
|
||||
type SearchOutput struct {
|
||||
Count int
|
||||
Results []App
|
||||
}
|
||||
|
||||
func (t *appstore) Search(input SearchInput) (SearchOutput, error) {
|
||||
countryCode, err := countryCodeFromStoreFront(input.Account.StoreFront)
|
||||
if err != nil {
|
||||
return SearchOutput{}, fmt.Errorf("country code is invalid: %w", err)
|
||||
}
|
||||
|
||||
request := t.searchRequest(input.Term, countryCode, input.Limit)
|
||||
|
||||
res, err := t.searchClient.Send(request)
|
||||
if err != nil {
|
||||
return SearchOutput{}, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != gohttp.StatusOK {
|
||||
return SearchOutput{}, NewErrorWithMetadata(errors.New("request failed"), res)
|
||||
}
|
||||
|
||||
return SearchOutput{
|
||||
Count: res.Data.Count,
|
||||
Results: res.Data.Results,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type searchResult struct {
|
||||
Count int `json:"resultCount,omitempty"`
|
||||
Results []App `json:"results,omitempty"`
|
||||
}
|
||||
|
||||
func (a *appstore) Search(term string, limit int64) error {
|
||||
acc, err := a.account()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrGetAccount.Error())
|
||||
}
|
||||
|
||||
countryCode, err := a.countryCodeFromStoreFront(acc.StoreFront)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrInvalidCountryCode.Error())
|
||||
}
|
||||
|
||||
request := a.searchRequest(term, countryCode, limit)
|
||||
|
||||
res, err := a.searchClient.Send(request)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRequest.Error())
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
a.logger.Verbose().Interface("data", res.Data).Int("status", res.StatusCode).Send()
|
||||
return ErrRequest
|
||||
}
|
||||
|
||||
a.logger.Log().Int("count", res.Data.Count).Array("apps", Apps(res.Data.Results)).Send()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *appstore) searchRequest(term, countryCode string, limit int64) http.Request {
|
||||
func (t *appstore) searchRequest(term, countryCode string, limit int64) http.Request {
|
||||
return http.Request{
|
||||
URL: a.searchURL(term, countryCode, limit),
|
||||
URL: t.searchURL(term, countryCode, limit),
|
||||
Method: http.MethodGET,
|
||||
ResponseFormat: http.ResponseFormatJSON,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *appstore) searchURL(term, countryCode string, limit int64) string {
|
||||
func (t *appstore) searchURL(term, countryCode string, limit int64) string {
|
||||
params := url.Values{}
|
||||
params.Add("entity", "software,iPadSoftware")
|
||||
params.Add("limit", strconv.Itoa(int(limit)))
|
||||
|
||||
@@ -1,35 +1,26 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/pkg/http"
|
||||
"github.com/majd/ipatool/pkg/keychain"
|
||||
"github.com/majd/ipatool/pkg/log"
|
||||
"github.com/majd/ipatool/v2/pkg/http"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ = Describe("AppStore (Search)", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
mockClient *http.MockClient[SearchResult]
|
||||
mockLogger *log.MockLogger
|
||||
mockKeychain *keychain.MockKeychain
|
||||
as AppStore
|
||||
ctrl *gomock.Controller
|
||||
mockClient *http.MockClient[searchResult]
|
||||
as AppStore
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockClient = http.NewMockClient[SearchResult](ctrl)
|
||||
mockLogger = log.NewMockLogger(ctrl)
|
||||
mockKeychain = keychain.NewMockKeychain(ctrl)
|
||||
mockClient = http.NewMockClient[searchResult](ctrl)
|
||||
as = &appstore{
|
||||
searchClient: mockClient,
|
||||
ioReader: os.Stdin,
|
||||
logger: mockLogger,
|
||||
keychain: mockKeychain,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -37,99 +28,98 @@ var _ = Describe("AppStore (Search)", func() {
|
||||
ctrl.Finish()
|
||||
})
|
||||
|
||||
When("user is not logged in", func() {
|
||||
When("request is successful", func() {
|
||||
const (
|
||||
testID = 0
|
||||
testBundleID = "test-bundle-id"
|
||||
testName = "test-name"
|
||||
testVersion = "test-version"
|
||||
testPrice = 0.0
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return(nil, ErrGetKeychainItem)
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[searchResult]{
|
||||
StatusCode: 200,
|
||||
Data: searchResult{
|
||||
Count: 1,
|
||||
Results: []App{
|
||||
{
|
||||
ID: testID,
|
||||
BundleID: testBundleID,
|
||||
Name: testName,
|
||||
Version: testVersion,
|
||||
Price: testPrice,
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Search("", 0)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetAccount.Error())))
|
||||
It("returns output", func() {
|
||||
out, err := as.Search(SearchInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(out.Count).To(Equal(1))
|
||||
Expect(out.Results).To(HaveLen(1))
|
||||
Expect(out.Results[0]).To(Equal(App{
|
||||
ID: testID,
|
||||
BundleID: testBundleID,
|
||||
Name: testName,
|
||||
Version: testVersion,
|
||||
Price: testPrice,
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
When("country code is invalid", func() {
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{}"), nil)
|
||||
})
|
||||
|
||||
When("store front is invalid", func() {
|
||||
It("returns error", func() {
|
||||
err := as.Search("", 0)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrInvalidCountryCode.Error())))
|
||||
_, err := as.Search(SearchInput{
|
||||
Account: Account{
|
||||
StoreFront: "xyz",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("request fails", func() {
|
||||
var testErr = errors.New("test")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{}, testErr)
|
||||
Return(http.Result[searchResult]{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Search("", 0)
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
_, err := as.Search(SearchInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("request returns bad status code", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Verbose().
|
||||
Return(nil)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
Return(http.Result[searchResult]{
|
||||
StatusCode: 400,
|
||||
}, nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
err := as.Search("", 0)
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrRequest.Error())))
|
||||
})
|
||||
})
|
||||
|
||||
When("request is successful", func() {
|
||||
BeforeEach(func() {
|
||||
mockLogger.EXPECT().
|
||||
Log().
|
||||
Return(nil)
|
||||
|
||||
mockClient.EXPECT().
|
||||
Send(gomock.Any()).
|
||||
Return(http.Result[SearchResult]{
|
||||
StatusCode: 200,
|
||||
Data: SearchResult{
|
||||
Count: 0,
|
||||
Results: []App{},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
mockKeychain.EXPECT().
|
||||
Get("account").
|
||||
Return([]byte("{\"storeFront\":\"143441\"}"), nil)
|
||||
})
|
||||
|
||||
It("returns nil", func() {
|
||||
err := as.Search("", 0)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, err := as.Search(SearchInput{
|
||||
Account: Account{
|
||||
StoreFront: "143441",
|
||||
},
|
||||
})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,10 @@
|
||||
package appstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppStore(t *testing.T) {
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
package appstore
|
||||
|
||||
const (
|
||||
FailureTypeInvalidCredentials = "-5000"
|
||||
FailureTypePasswordTokenExpired = "2034"
|
||||
FailureTypeLicenseNotFound = "9610"
|
||||
FailureTypeInvalidCredentials = "-5000"
|
||||
FailureTypePasswordTokenExpired = "2034"
|
||||
FailureTypeLicenseNotFound = "9610"
|
||||
FailureTypeTemporarilyUnavailable = "2059"
|
||||
|
||||
CustomerMessageBadLogin = "MZFinance.BadLogin.Configurator_message"
|
||||
CustomerMessageBadLogin = "MZFinance.BadLogin.Configurator_message"
|
||||
CustomerMessageSubscriptionRequired = "Subscription Required"
|
||||
|
||||
iTunesAPIDomain = "itunes.apple.com"
|
||||
iTunesAPIPathSearch = "/search"
|
||||
iTunesAPIPathLookup = "/lookup"
|
||||
|
||||
PriavteAppStoreAPIDomainPrefixWithoutAuthCode = "p25"
|
||||
PriavteAppStoreAPIDomainPrefixWithAuthCode = "p71"
|
||||
PrivateAppStoreAPIDomainPrefixWithoutAuthCode = "p25"
|
||||
PrivateAppStoreAPIDomainPrefixWithAuthCode = "p71"
|
||||
PrivateAppStoreAPIDomain = "buy." + iTunesAPIDomain
|
||||
PrivateAppStoreAPIPathAuthenticate = "/WebObjects/MZFinance.woa/wa/authenticate"
|
||||
PrivateAppStoreAPIPathPurchase = "/WebObjects/MZBuy.woa/wa/buyProduct"
|
||||
PrivateAppStoreAPIPathDownload = "/WebObjects/MZFinance.woa/wa/volumeStoreDownloadProduct"
|
||||
|
||||
HTTPHeaderStoreFront = "X-Set-Apple-Store-Front"
|
||||
|
||||
PricingParameterAppStore = "STDQ"
|
||||
PricingParameterAppleArcade = "GAME"
|
||||
)
|
||||
@@ -0,0 +1,17 @@
|
||||
package appstore
|
||||
|
||||
type Error struct {
|
||||
Metadata interface{}
|
||||
underlyingError error
|
||||
}
|
||||
|
||||
func (t Error) Error() string {
|
||||
return t.underlyingError.Error()
|
||||
}
|
||||
|
||||
func NewErrorWithMetadata(err error, metadata interface{}) *Error {
|
||||
return &Error{
|
||||
underlyingError: err,
|
||||
Metadata: metadata,
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package appstore
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrAppLookup = errors.New("failed to find app")
|
||||
ErrAppNotFound = errors.New("failed to find app on the App Store")
|
||||
ErrApplyLegacyPatches = errors.New("failed to apply legacy patches")
|
||||
ErrApplyPatches = errors.New("failed to apply patches")
|
||||
ErrAuthCodeRequired = errors.New("auth code is required")
|
||||
ErrCheckDirectory = errors.New("failed to determine if the supplied path is a directory")
|
||||
ErrCreateDestinationFile = errors.New("failed to create destination file")
|
||||
ErrCreateMetadataFile = errors.New("failed to create metadata file")
|
||||
ErrCreateRequest = errors.New("failed to create HTTP request")
|
||||
ErrCreateSinfFile = errors.New("failed to create sinf file")
|
||||
ErrDecompressInfoFile = errors.New("failed to open decompressed info file")
|
||||
ErrDecompressManifestFile = errors.New("failed to open decompressed manifest file")
|
||||
ErrDownloadFile = errors.New("failed to download file")
|
||||
ErrEncodeMetadataFile = errors.New("failed to encode metadata")
|
||||
ErrFileWrite = errors.New("failed to write file")
|
||||
ErrGeneric = errors.New("failed due to an unknown error")
|
||||
ErrGetAccount = errors.New("failed to get account")
|
||||
ErrGetBundleName = errors.New("failed to determine name of app bundle")
|
||||
ErrGetCurrentDirectory = errors.New("failed to get current directory path")
|
||||
ErrGetData = errors.New("failed to get data")
|
||||
ErrGetExecutablePath = errors.New("failed to get executable path")
|
||||
ErrGetFileMetadata = errors.New("failed to get file metadata")
|
||||
ErrGetInfoFile = errors.New("failed to read info file")
|
||||
ErrGetKeychainItem = errors.New("failed to read item from keychain")
|
||||
ErrGetMAC = errors.New("failed to get MAC address")
|
||||
ErrGetManifestFile = errors.New("failed to read manifest file")
|
||||
ErrInvalidCountryCode = errors.New("invalid country code")
|
||||
ErrInvalidResponse = errors.New("received 0 items from the App Store; expected 1 or more")
|
||||
ErrInvalidStoreFront = errors.New("could not infer country code from store front")
|
||||
ErrLicenseExists = errors.New("account already has a license for this app")
|
||||
ErrLicenseRequired = errors.New("license is required")
|
||||
ErrLogin = errors.New("failed to log in")
|
||||
ErrMarshal = errors.New("failed to marshal data")
|
||||
ErrOpenFile = errors.New("failed to open file")
|
||||
ErrOpenZipFile = errors.New("failed to open zip file")
|
||||
ErrPaidApp = errors.New("paid apps cannot be purchased")
|
||||
ErrPasswordRequired = errors.New("password is required when not running in interactive mode; use the \"--password\" flag")
|
||||
ErrPasswordTokenExpired = errors.New("password token is expired")
|
||||
ErrPatchApp = errors.New("failed to patch app package")
|
||||
ErrPurchase = errors.New("failed to purchase app")
|
||||
ErrRemoveKeychainItem = errors.New("failed to remove item from keychain")
|
||||
ErrRemoveTempFile = errors.New("failed to remove temporary file")
|
||||
ErrReplicateZip = errors.New("failed to replicate zip")
|
||||
ErrRequest = errors.New("failed to send request")
|
||||
ErrResolveDestinationPath = errors.New("failed to resolve destination path")
|
||||
ErrSetKeychainItem = errors.New("failed to save item in keychain")
|
||||
ErrUnmarshal = errors.New("failed to unmarshal data")
|
||||
ErrWriteMetadataFile = errors.New("failed to write metadata")
|
||||
ErrWriteSinfData = errors.New("failed to write sinf data")
|
||||
ErrZipSinfs = errors.New("failed to zip sinfs and sinf paths")
|
||||
)
|
||||
@@ -1,9 +1,27 @@
|
||||
package appstore
|
||||
|
||||
var StoreFronts = map[string]string{
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func countryCodeFromStoreFront(storeFront string) (string, error) {
|
||||
for key, val := range storeFronts {
|
||||
parts := strings.Split(storeFront, "-")
|
||||
|
||||
if len(parts) >= 1 && parts[0] == val {
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("country code mapping for store front (%s) was not found", storeFront)
|
||||
}
|
||||
|
||||
var storeFronts = map[string]string{
|
||||
"AE": "143481",
|
||||
"AG": "143540",
|
||||
"AI": "143538",
|
||||
"AL": "143575",
|
||||
"AM": "143524",
|
||||
"AO": "143564",
|
||||
"AR": "143505",
|
||||
@@ -45,6 +63,7 @@ var StoreFronts = map[string]string{
|
||||
"FR": "143442",
|
||||
"GB": "143444",
|
||||
"GD": "143546",
|
||||
"GE": "143615",
|
||||
"GH": "143573",
|
||||
"GR": "143448",
|
||||
"GT": "143504",
|
||||
@@ -79,6 +98,7 @@ var StoreFronts = map[string]string{
|
||||
"MG": "143531",
|
||||
"MK": "143530",
|
||||
"ML": "143532",
|
||||
"MN": "143592",
|
||||
"MO": "143515",
|
||||
"MS": "143547",
|
||||
"MT": "143521",
|
||||
+54
-21
@@ -3,15 +3,15 @@ package http
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"howett.net/plist"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"howett.net/plist"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source=client.go -destination=client_mock.go -package=http
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=client.go -destination=client_mock.go -package=http
|
||||
type Client[R interface{}] interface {
|
||||
Send(request Request) (Result[R], error)
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
@@ -23,34 +23,54 @@ type client[R interface{}] struct {
|
||||
cookieJar CookieJar
|
||||
}
|
||||
|
||||
type ClientArgs struct {
|
||||
type Args struct {
|
||||
CookieJar CookieJar
|
||||
}
|
||||
|
||||
func NewClient[R interface{}](args ClientArgs) Client[R] {
|
||||
type AddHeaderTransport struct {
|
||||
T http.RoundTripper
|
||||
}
|
||||
|
||||
func (t *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if req.Header.Get("User-Agent") == "" {
|
||||
req.Header.Set("User-Agent", DefaultUserAgent)
|
||||
}
|
||||
|
||||
res, err := t.T.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to make round trip: %w", err)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func NewClient[R interface{}](args Args) Client[R] {
|
||||
return &client[R]{
|
||||
internalClient: http.Client{
|
||||
Timeout: time.Second * 15,
|
||||
Jar: args.CookieJar,
|
||||
Timeout: 0,
|
||||
Jar: args.CookieJar,
|
||||
Transport: &AddHeaderTransport{http.DefaultTransport},
|
||||
},
|
||||
cookieJar: args.CookieJar,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *client[R]) Send(req Request) (Result[R], error) {
|
||||
var data []byte
|
||||
var err error
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
)
|
||||
|
||||
if req.Payload != nil {
|
||||
data, err = req.Payload.data()
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrGetPayloadData.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to get payload data: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(req.Method, req.URL, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrCreateRequest.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
for key, val := range req.Headers {
|
||||
@@ -59,42 +79,54 @@ func (c *client[R]) Send(req Request) (Result[R], error) {
|
||||
|
||||
res, err := c.internalClient.Do(request)
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrRequest.Error())
|
||||
return Result[R]{}, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
err = c.cookieJar.Save()
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrSaveCookie.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to save cookies: %w", err)
|
||||
}
|
||||
|
||||
if req.ResponseFormat == ResponseFormatJSON {
|
||||
return c.handleJSONResponse(res)
|
||||
}
|
||||
|
||||
if req.ResponseFormat == ResponseFormatXML {
|
||||
return c.handleXMLResponse(res)
|
||||
}
|
||||
|
||||
return Result[R]{}, errors.Errorf("%s: %s", ErrUnsupportedContentType.Error(), req.ResponseFormat)
|
||||
return Result[R]{}, fmt.Errorf("content type is not supported (%s)", req.ResponseFormat)
|
||||
}
|
||||
|
||||
func (c *client[R]) Do(req *http.Request) (*http.Response, error) {
|
||||
return http.DefaultClient.Do(req)
|
||||
res, err := c.internalClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("received error: %w", err)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (*client[R]) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
|
||||
return http.NewRequest(method, url, body)
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *client[R]) handleJSONResponse(res *http.Response) (Result[R], error) {
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrGetResponseBody.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var data R
|
||||
|
||||
err = json.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrUnmarshalJSON.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to unmarshal json: %w", err)
|
||||
}
|
||||
|
||||
return Result[R]{
|
||||
@@ -106,13 +138,14 @@ func (c *client[R]) handleJSONResponse(res *http.Response) (Result[R], error) {
|
||||
func (c *client[R]) handleXMLResponse(res *http.Response) (Result[R], error) {
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrGetResponseBody.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var data R
|
||||
|
||||
_, err = plist.Unmarshal(body, &data)
|
||||
if err != nil {
|
||||
return Result[R]{}, errors.Wrap(err, ErrUnmarshalXML.Error())
|
||||
return Result[R]{}, fmt.Errorf("failed to unmarshal xml: %w", err)
|
||||
}
|
||||
|
||||
headers := map[string]string{}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: client.go
|
||||
|
||||
// Package http is a generated GoMock package.
|
||||
package http
|
||||
|
||||
import (
|
||||
io "io"
|
||||
http "net/http"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockClient is a mock of Client interface.
|
||||
type MockClient[R interface{}] struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockClientMockRecorder[R]
|
||||
}
|
||||
|
||||
// MockClientMockRecorder is the mock recorder for MockClient.
|
||||
type MockClientMockRecorder[R interface{}] struct {
|
||||
mock *MockClient[R]
|
||||
}
|
||||
|
||||
// NewMockClient creates a new mock instance.
|
||||
func NewMockClient[R interface{}](ctrl *gomock.Controller) *MockClient[R] {
|
||||
mock := &MockClient[R]{ctrl: ctrl}
|
||||
mock.recorder = &MockClientMockRecorder[R]{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockClient[R]) EXPECT() *MockClientMockRecorder[R] {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Do mocks base method.
|
||||
func (m *MockClient[R]) Do(req *http.Request) (*http.Response, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Do", req)
|
||||
ret0, _ := ret[0].(*http.Response)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Do indicates an expected call of Do.
|
||||
func (mr *MockClientMockRecorder[R]) Do(req interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockClient[R])(nil).Do), req)
|
||||
}
|
||||
|
||||
// NewRequest mocks base method.
|
||||
func (m *MockClient[R]) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewRequest", method, url, body)
|
||||
ret0, _ := ret[0].(*http.Request)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// NewRequest indicates an expected call of NewRequest.
|
||||
func (mr *MockClientMockRecorder[R]) NewRequest(method, url, body interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewRequest", reflect.TypeOf((*MockClient[R])(nil).NewRequest), method, url, body)
|
||||
}
|
||||
|
||||
// Send mocks base method.
|
||||
func (m *MockClient[R]) Send(request Request) (Result[R], error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Send", request)
|
||||
ret0, _ := ret[0].(Result[R])
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Send indicates an expected call of Send.
|
||||
func (mr *MockClientMockRecorder[R]) Send(request interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockClient[R])(nil).Send), request)
|
||||
}
|
||||
+56
-66
@@ -1,66 +1,38 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type JSONResult struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
|
||||
type XMLResult struct {
|
||||
Foo string `plist:"foo"`
|
||||
}
|
||||
|
||||
var _ = Describe("Client", Ordered, func() {
|
||||
type jsonResult struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
|
||||
type xmlResult struct {
|
||||
Foo string `plist:"foo"`
|
||||
}
|
||||
|
||||
var (
|
||||
port int
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
ctrl *gomock.Controller
|
||||
srv *httptest.Server
|
||||
mockHandler func(w http.ResponseWriter, r *http.Request)
|
||||
mockCookieJar *MockCookieJar
|
||||
)
|
||||
|
||||
BeforeAll(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
port = rand.Intn(59_999-50_000) + 50_000
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
mockCookieJar = NewMockCookieJar(ctrl)
|
||||
|
||||
http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
|
||||
res := []byte("{\"foo\":\"bar\"}")
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
_, err := w.Write(res)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
http.HandleFunc("/xml", func(w http.ResponseWriter, r *http.Request) {
|
||||
res := []byte("<dict><key>foo</key><string>bar</string></dict>")
|
||||
w.Header().Add("Content-Type", "application/xml")
|
||||
_, err := w.Write(res)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
http.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/random-type")
|
||||
})
|
||||
|
||||
go func() {
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
<-ctx.Done()
|
||||
}()
|
||||
})
|
||||
|
||||
AfterAll(func() {
|
||||
cancel()
|
||||
mockHandler = func(w http.ResponseWriter, r *http.Request) {}
|
||||
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mockHandler(w, r)
|
||||
}))
|
||||
})
|
||||
|
||||
BeforeEach(func() {
|
||||
@@ -71,17 +43,22 @@ var _ = Describe("Client", Ordered, func() {
|
||||
})
|
||||
|
||||
It("returns request", func() {
|
||||
sut := NewClient[XMLResult](ClientArgs{})
|
||||
sut := NewClient[xmlResult](Args{})
|
||||
|
||||
req, err := sut.NewRequest("GET", fmt.Sprintf("http://localhost:%d/json", port), nil)
|
||||
req, err := sut.NewRequest("GET", srv.URL, nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(req).ToNot(BeNil())
|
||||
})
|
||||
|
||||
It("returns response", func() {
|
||||
sut := NewClient[XMLResult](ClientArgs{})
|
||||
mockHandler = func(_w http.ResponseWriter, r *http.Request) {
|
||||
defer GinkgoRecover()
|
||||
Expect(r.Header.Get("User-Agent")).To(Equal(DefaultUserAgent))
|
||||
}
|
||||
|
||||
req, err := sut.NewRequest("GET", fmt.Sprintf("http://localhost:%d/json", port), nil)
|
||||
sut := NewClient[xmlResult](Args{})
|
||||
|
||||
req, err := sut.NewRequest("GET", srv.URL, nil)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(req).ToNot(BeNil())
|
||||
|
||||
@@ -92,25 +69,22 @@ var _ = Describe("Client", Ordered, func() {
|
||||
|
||||
When("payload decodes successfully", func() {
|
||||
When("cookie jar fails to save", func() {
|
||||
var testErr = errors.New("test")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockCookieJar.EXPECT().
|
||||
Save().
|
||||
Return(testErr)
|
||||
Return(errors.New(""))
|
||||
})
|
||||
|
||||
It("returns error", func() {
|
||||
sut := NewClient[JSONResult](ClientArgs{
|
||||
sut := NewClient[jsonResult](Args{
|
||||
CookieJar: mockCookieJar,
|
||||
})
|
||||
_, err := sut.Send(Request{
|
||||
URL: fmt.Sprintf("http://localhost:%d/json", port),
|
||||
URL: srv.URL,
|
||||
Method: MethodGET,
|
||||
})
|
||||
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to save cookies")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -122,11 +96,17 @@ var _ = Describe("Client", Ordered, func() {
|
||||
})
|
||||
|
||||
It("decodes JSON response", func() {
|
||||
sut := NewClient[JSONResult](ClientArgs{
|
||||
mockHandler = func(w http.ResponseWriter, _r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
_, err := w.Write([]byte("{\"foo\":\"bar\"}"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
}
|
||||
|
||||
sut := NewClient[jsonResult](Args{
|
||||
CookieJar: mockCookieJar,
|
||||
})
|
||||
res, err := sut.Send(Request{
|
||||
URL: fmt.Sprintf("http://localhost:%d/json", port),
|
||||
URL: srv.URL,
|
||||
Method: MethodGET,
|
||||
ResponseFormat: ResponseFormatJSON,
|
||||
Headers: map[string]string{
|
||||
@@ -144,11 +124,17 @@ var _ = Describe("Client", Ordered, func() {
|
||||
})
|
||||
|
||||
It("decodes XML response", func() {
|
||||
sut := NewClient[XMLResult](ClientArgs{
|
||||
mockHandler = func(w http.ResponseWriter, _r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/xml")
|
||||
_, err := w.Write([]byte("<dict><key>foo</key><string>bar</string></dict>"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
}
|
||||
|
||||
sut := NewClient[xmlResult](Args{
|
||||
CookieJar: mockCookieJar,
|
||||
})
|
||||
res, err := sut.Send(Request{
|
||||
URL: fmt.Sprintf("http://localhost:%d/xml", port),
|
||||
URL: srv.URL,
|
||||
Method: MethodPOST,
|
||||
ResponseFormat: ResponseFormatXML,
|
||||
})
|
||||
@@ -158,27 +144,31 @@ var _ = Describe("Client", Ordered, func() {
|
||||
})
|
||||
|
||||
It("returns error when content type is not supported", func() {
|
||||
sut := NewClient[XMLResult](ClientArgs{
|
||||
mockHandler = func(w http.ResponseWriter, _r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/xyz")
|
||||
}
|
||||
|
||||
sut := NewClient[xmlResult](Args{
|
||||
CookieJar: mockCookieJar,
|
||||
})
|
||||
_, err := sut.Send(Request{
|
||||
URL: fmt.Sprintf("http://localhost:%d/error", port),
|
||||
URL: srv.URL,
|
||||
Method: MethodPOST,
|
||||
ResponseFormat: "random",
|
||||
})
|
||||
|
||||
Expect(err).To(MatchError(ContainSubstring("unsupported response body content type: random")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("payload fails to decode", func() {
|
||||
It("returns error", func() {
|
||||
sut := NewClient[XMLResult](ClientArgs{
|
||||
sut := NewClient[xmlResult](Args{
|
||||
CookieJar: mockCookieJar,
|
||||
})
|
||||
_, err := sut.Send(Request{
|
||||
URL: fmt.Sprintf("http://localhost:%d/error", port),
|
||||
URL: srv.URL,
|
||||
Method: MethodPOST,
|
||||
ResponseFormat: ResponseFormatXML,
|
||||
Payload: &URLPayload{
|
||||
@@ -188,7 +178,7 @@ var _ = Describe("Client", Ordered, func() {
|
||||
},
|
||||
})
|
||||
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrGetPayloadData.Error())))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -6,3 +6,7 @@ const (
|
||||
ResponseFormatJSON ResponseFormat = "json"
|
||||
ResponseFormatXML ResponseFormat = "xml"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultUserAgent = "Configurator/2.15 (Macintosh; OS X 11.0.0; 16G29) AppleWebKit/2603.3.8"
|
||||
)
|
||||
@@ -2,7 +2,7 @@ package http
|
||||
|
||||
import "net/http"
|
||||
|
||||
//go:generate mockgen -source=cookiejar.go -destination=cookiejar_mock.go -package=http
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=cookiejar.go -destination=cookiejar_mock.go -package=http
|
||||
type CookieJar interface {
|
||||
http.CookieJar
|
||||
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: cookiejar.go
|
||||
|
||||
// Package http is a generated GoMock package.
|
||||
package http
|
||||
|
||||
import (
|
||||
http "net/http"
|
||||
url "net/url"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockCookieJar is a mock of CookieJar interface.
|
||||
type MockCookieJar struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockCookieJarMockRecorder
|
||||
}
|
||||
|
||||
// MockCookieJarMockRecorder is the mock recorder for MockCookieJar.
|
||||
type MockCookieJarMockRecorder struct {
|
||||
mock *MockCookieJar
|
||||
}
|
||||
|
||||
// NewMockCookieJar creates a new mock instance.
|
||||
func NewMockCookieJar(ctrl *gomock.Controller) *MockCookieJar {
|
||||
mock := &MockCookieJar{ctrl: ctrl}
|
||||
mock.recorder = &MockCookieJarMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockCookieJar) EXPECT() *MockCookieJarMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Cookies mocks base method.
|
||||
func (m *MockCookieJar) Cookies(u *url.URL) []*http.Cookie {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Cookies", u)
|
||||
ret0, _ := ret[0].([]*http.Cookie)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Cookies indicates an expected call of Cookies.
|
||||
func (mr *MockCookieJarMockRecorder) Cookies(u interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cookies", reflect.TypeOf((*MockCookieJar)(nil).Cookies), u)
|
||||
}
|
||||
|
||||
// Save mocks base method.
|
||||
func (m *MockCookieJar) Save() error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Save")
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Save indicates an expected call of Save.
|
||||
func (mr *MockCookieJarMockRecorder) Save() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockCookieJar)(nil).Save))
|
||||
}
|
||||
|
||||
// SetCookies mocks base method.
|
||||
func (m *MockCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
||||
m.ctrl.T.Helper()
|
||||
m.ctrl.Call(m, "SetCookies", u, cookies)
|
||||
}
|
||||
|
||||
// SetCookies indicates an expected call of SetCookies.
|
||||
func (mr *MockCookieJarMockRecorder) SetCookies(u, cookies interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetCookies", reflect.TypeOf((*MockCookieJar)(nil).SetCookies), u, cookies)
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package http
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrCreateRequest = errors.New("failed to create HTTP request")
|
||||
ErrEncodePayloadXML = errors.New("failed to encode XML object")
|
||||
ErrGetPayloadData = errors.New("failed to get payload data")
|
||||
ErrGetResponseBody = errors.New("failed to get response body")
|
||||
ErrRequest = errors.New("failed to send request")
|
||||
ErrSaveCookie = errors.New("failed to save cookies")
|
||||
ErrUnmarshalJSON = errors.New("failed to unmarshal JSON data")
|
||||
ErrUnmarshalXML = errors.New("failed to unmarshal XML data")
|
||||
ErrUnsupportedContentType = errors.New("unsupported response body content type")
|
||||
ErrUnsupportedValueType = errors.New("unsupported value type")
|
||||
)
|
||||
@@ -1,9 +1,10 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHTTP(t *testing.T) {
|
||||
|
||||
+5
-4
@@ -2,10 +2,11 @@ package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/pkg/errors"
|
||||
"howett.net/plist"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"howett.net/plist"
|
||||
)
|
||||
|
||||
type Payload interface {
|
||||
@@ -25,7 +26,7 @@ func (p *XMLPayload) data() ([]byte, error) {
|
||||
|
||||
err := plist.NewEncoder(buffer).Encode(p.Content)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, ErrEncodePayloadXML.Error())
|
||||
return nil, fmt.Errorf("failed to encode plist: %w", err)
|
||||
}
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
@@ -41,7 +42,7 @@ func (p *URLPayload) data() ([]byte, error) {
|
||||
case int:
|
||||
params.Add(key, strconv.Itoa(val.(int)))
|
||||
default:
|
||||
return nil, errors.Errorf("%s: %s", ErrUnsupportedValueType.Error(), t)
|
||||
return nil, fmt.Errorf("value type is not supported (%s)", t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ var _ = Describe("Payload", func() {
|
||||
}
|
||||
|
||||
data, err := sut.data()
|
||||
Expect(err).To(MatchError(ContainSubstring(ErrUnsupportedValueType.Error())))
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(data).To(BeNil())
|
||||
})
|
||||
})
|
||||
@@ -57,7 +57,7 @@ var _ = Describe("Payload", func() {
|
||||
}
|
||||
|
||||
data, err := sut.data()
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to encode XML object")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(data).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package keychain
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
|
||||
var (
|
||||
ErrGetKeychainItem = errors.New("failed to get item from keyring")
|
||||
ErrRemoveKeychainItem = errors.New("failed to remove item from keyring")
|
||||
ErrSetKeychainItem = errors.New("failed to set item in keyring")
|
||||
)
|
||||
@@ -1,6 +1,6 @@
|
||||
package keychain
|
||||
|
||||
//go:generate mockgen -source=keychain.go -destination=keychain_mock.go -package keychain
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=keychain.go -destination=keychain_mock.go -package keychain
|
||||
type Keychain interface {
|
||||
Get(key string) ([]byte, error)
|
||||
Set(key string, data []byte) error
|
||||
@@ -11,11 +11,11 @@ type keychain struct {
|
||||
keyring Keyring
|
||||
}
|
||||
|
||||
type KeychainArgs struct {
|
||||
type Args struct {
|
||||
Keyring Keyring
|
||||
}
|
||||
|
||||
func NewKeychain(args KeychainArgs) Keychain {
|
||||
func New(args Args) Keychain {
|
||||
return &keychain{
|
||||
keyring: args.Keyring,
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package keychain
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (k *keychain) Get(key string) ([]byte, error) {
|
||||
item, err := k.keyring.Get(key)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, ErrGetKeychainItem.Error())
|
||||
return nil, fmt.Errorf("failed to get item: %w", err)
|
||||
}
|
||||
|
||||
return item.Data, nil
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package keychain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/golang/mock/gomock"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var _ = Describe("Keychain (Get)", func() {
|
||||
@@ -18,7 +19,7 @@ var _ = Describe("Keychain (Get)", func() {
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeyring = NewMockKeyring(ctrl)
|
||||
keychain = NewKeychain(KeychainArgs{
|
||||
keychain = New(Args{
|
||||
Keyring: mockKeyring,
|
||||
})
|
||||
})
|
||||
@@ -29,18 +30,16 @@ var _ = Describe("Keychain (Get)", func() {
|
||||
|
||||
When("keyring returns error", func() {
|
||||
const testKey = "test-key"
|
||||
var testErr = errors.New("test error")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeyring.EXPECT().
|
||||
Get(testKey).
|
||||
Return(keyring.Item{}, testErr)
|
||||
Return(keyring.Item{}, errors.New(""))
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
data, err := keychain.Get(testKey)
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to get item from keyring")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(data).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: keychain.go
|
||||
|
||||
// Package keychain is a generated GoMock package.
|
||||
package keychain
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockKeychain is a mock of Keychain interface.
|
||||
type MockKeychain struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockKeychainMockRecorder
|
||||
}
|
||||
|
||||
// MockKeychainMockRecorder is the mock recorder for MockKeychain.
|
||||
type MockKeychainMockRecorder struct {
|
||||
mock *MockKeychain
|
||||
}
|
||||
|
||||
// NewMockKeychain creates a new mock instance.
|
||||
func NewMockKeychain(ctrl *gomock.Controller) *MockKeychain {
|
||||
mock := &MockKeychain{ctrl: ctrl}
|
||||
mock.recorder = &MockKeychainMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockKeychain) EXPECT() *MockKeychainMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Get mocks base method.
|
||||
func (m *MockKeychain) Get(key string) ([]byte, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Get", key)
|
||||
ret0, _ := ret[0].([]byte)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Get indicates an expected call of Get.
|
||||
func (mr *MockKeychainMockRecorder) Get(key interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockKeychain)(nil).Get), key)
|
||||
}
|
||||
|
||||
// Remove mocks base method.
|
||||
func (m *MockKeychain) Remove(key string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Remove", key)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Remove indicates an expected call of Remove.
|
||||
func (mr *MockKeychainMockRecorder) Remove(key interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Remove", reflect.TypeOf((*MockKeychain)(nil).Remove), key)
|
||||
}
|
||||
|
||||
// Set mocks base method.
|
||||
func (m *MockKeychain) Set(key string, data []byte) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Set", key, data)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Set indicates an expected call of Set.
|
||||
func (mr *MockKeychainMockRecorder) Set(key, data interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockKeychain)(nil).Set), key, data)
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package keychain
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (k *keychain) Remove(key string) error {
|
||||
err := k.keyring.Remove(key)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrRemoveKeychainItem.Error())
|
||||
return fmt.Errorf("failed to remove item: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package keychain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var _ = Describe("Keychain (Remove)", func() {
|
||||
@@ -17,7 +18,7 @@ var _ = Describe("Keychain (Remove)", func() {
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeyring = NewMockKeyring(ctrl)
|
||||
keychain = NewKeychain(KeychainArgs{
|
||||
keychain = New(Args{
|
||||
Keyring: mockKeyring,
|
||||
})
|
||||
})
|
||||
@@ -28,18 +29,16 @@ var _ = Describe("Keychain (Remove)", func() {
|
||||
|
||||
When("keyring returns error", func() {
|
||||
const testKey = "test-key"
|
||||
var testErr = errors.New("test error")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeyring.EXPECT().
|
||||
Remove(testKey).
|
||||
Return(testErr)
|
||||
Return(errors.New(""))
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
err := keychain.Remove(testKey)
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to remove item from keyring")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package keychain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (k *keychain) Set(key string, data []byte) error {
|
||||
@@ -11,7 +12,7 @@ func (k *keychain) Set(key string, data []byte) error {
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, ErrSetKeychainItem.Error())
|
||||
return fmt.Errorf("failed to set item: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package keychain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/99designs/keyring"
|
||||
"github.com/golang/mock/gomock"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var _ = Describe("Keychain (Set)", func() {
|
||||
@@ -18,7 +19,7 @@ var _ = Describe("Keychain (Set)", func() {
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockKeyring = NewMockKeyring(ctrl)
|
||||
keychain = NewKeychain(KeychainArgs{
|
||||
keychain = New(Args{
|
||||
Keyring: mockKeyring,
|
||||
})
|
||||
})
|
||||
@@ -29,10 +30,7 @@ var _ = Describe("Keychain (Set)", func() {
|
||||
|
||||
When("keyring returns error", func() {
|
||||
const testKey = "test-key"
|
||||
var (
|
||||
testData = []byte("test")
|
||||
testErr = errors.New("test error")
|
||||
)
|
||||
var testData = []byte("test")
|
||||
|
||||
BeforeEach(func() {
|
||||
mockKeyring.EXPECT().
|
||||
@@ -40,13 +38,12 @@ var _ = Describe("Keychain (Set)", func() {
|
||||
Key: testKey,
|
||||
Data: testData,
|
||||
}).
|
||||
Return(testErr)
|
||||
Return(errors.New(""))
|
||||
})
|
||||
|
||||
It("returns wrapped error", func() {
|
||||
err := keychain.Set(testKey, testData)
|
||||
Expect(err).To(MatchError(ContainSubstring(testErr.Error())))
|
||||
Expect(err).To(MatchError(ContainSubstring("failed to set item in keyring")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package keychain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestKeychain(t *testing.T) {
|
||||
|
||||
@@ -2,7 +2,7 @@ package keychain
|
||||
|
||||
import "github.com/99designs/keyring"
|
||||
|
||||
//go:generate mockgen -source=keyring.go -destination=keyring_mock.go -package keychain
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=keyring.go -destination=keyring_mock.go -package keychain
|
||||
type Keyring interface {
|
||||
Get(key string) (keyring.Item, error)
|
||||
Set(item keyring.Item) error
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: keyring.go
|
||||
|
||||
// Package keychain is a generated GoMock package.
|
||||
package keychain
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
keyring "github.com/99designs/keyring"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockKeyring is a mock of Keyring interface.
|
||||
type MockKeyring struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockKeyringMockRecorder
|
||||
}
|
||||
|
||||
// MockKeyringMockRecorder is the mock recorder for MockKeyring.
|
||||
type MockKeyringMockRecorder struct {
|
||||
mock *MockKeyring
|
||||
}
|
||||
|
||||
// NewMockKeyring creates a new mock instance.
|
||||
func NewMockKeyring(ctrl *gomock.Controller) *MockKeyring {
|
||||
mock := &MockKeyring{ctrl: ctrl}
|
||||
mock.recorder = &MockKeyringMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockKeyring) EXPECT() *MockKeyringMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Get mocks base method.
|
||||
func (m *MockKeyring) Get(key string) (keyring.Item, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Get", key)
|
||||
ret0, _ := ret[0].(keyring.Item)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Get indicates an expected call of Get.
|
||||
func (mr *MockKeyringMockRecorder) Get(key interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockKeyring)(nil).Get), key)
|
||||
}
|
||||
|
||||
// Remove mocks base method.
|
||||
func (m *MockKeyring) Remove(key string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Remove", key)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Remove indicates an expected call of Remove.
|
||||
func (mr *MockKeyringMockRecorder) Remove(key interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Remove", reflect.TypeOf((*MockKeyring)(nil).Remove), key)
|
||||
}
|
||||
|
||||
// Set mocks base method.
|
||||
func (m *MockKeyring) Set(item keyring.Item) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Set", item)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Set indicates an expected call of Set.
|
||||
func (mr *MockKeyringMockRecorder) Set(item interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockKeyring)(nil).Set), item)
|
||||
}
|
||||
+2
-1
@@ -1,9 +1,10 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
|
||||
+5
-4
@@ -1,13 +1,14 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog/pkgerrors"
|
||||
"io"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source=logger.go -destination=logger_mock.go -package log
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=logger.go -destination=logger_mock.go -package log
|
||||
type Logger interface {
|
||||
Verbose() *zerolog.Event
|
||||
Log() *zerolog.Event
|
||||
@@ -19,12 +20,12 @@ type logger struct {
|
||||
verbose bool
|
||||
}
|
||||
|
||||
type LoggerArgs struct {
|
||||
type Args struct {
|
||||
Verbose bool
|
||||
Writer io.Writer
|
||||
}
|
||||
|
||||
func NewLogger(args LoggerArgs) Logger {
|
||||
func NewLogger(args Args) Logger {
|
||||
internalLogger := log.Logger
|
||||
level := zerolog.InfoLevel
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: logger.go
|
||||
|
||||
// Package log is a generated GoMock package.
|
||||
package log
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
zerolog "github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// MockLogger is a mock of Logger interface.
|
||||
type MockLogger struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockLoggerMockRecorder
|
||||
}
|
||||
|
||||
// MockLoggerMockRecorder is the mock recorder for MockLogger.
|
||||
type MockLoggerMockRecorder struct {
|
||||
mock *MockLogger
|
||||
}
|
||||
|
||||
// NewMockLogger creates a new mock instance.
|
||||
func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
|
||||
mock := &MockLogger{ctrl: ctrl}
|
||||
mock.recorder = &MockLoggerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockLogger) EXPECT() *MockLoggerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Error mocks base method.
|
||||
func (m *MockLogger) Error() *zerolog.Event {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Error")
|
||||
ret0, _ := ret[0].(*zerolog.Event)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Error indicates an expected call of Error.
|
||||
func (mr *MockLoggerMockRecorder) Error() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockLogger)(nil).Error))
|
||||
}
|
||||
|
||||
// Log mocks base method.
|
||||
func (m *MockLogger) Log() *zerolog.Event {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Log")
|
||||
ret0, _ := ret[0].(*zerolog.Event)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Log indicates an expected call of Log.
|
||||
func (mr *MockLoggerMockRecorder) Log() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Log", reflect.TypeOf((*MockLogger)(nil).Log))
|
||||
}
|
||||
|
||||
// Verbose mocks base method.
|
||||
func (m *MockLogger) Verbose() *zerolog.Event {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Verbose")
|
||||
ret0, _ := ret[0].(*zerolog.Event)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Verbose indicates an expected call of Verbose.
|
||||
func (mr *MockLoggerMockRecorder) Verbose() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Verbose", reflect.TypeOf((*MockLogger)(nil).Verbose))
|
||||
}
|
||||
@@ -22,7 +22,7 @@ var _ = Describe("Logger", func() {
|
||||
|
||||
Context("Verbose logger", func() {
|
||||
BeforeEach(func() {
|
||||
logger = NewLogger(LoggerArgs{
|
||||
logger = NewLogger(Args{
|
||||
Verbose: true,
|
||||
Writer: mockWriter,
|
||||
})
|
||||
@@ -44,7 +44,7 @@ var _ = Describe("Logger", func() {
|
||||
|
||||
Context("Non-verbose logger", func() {
|
||||
BeforeEach(func() {
|
||||
logger = NewLogger(LoggerArgs{
|
||||
logger = NewLogger(Args{
|
||||
Verbose: false,
|
||||
Writer: mockWriter,
|
||||
})
|
||||
|
||||
+24
-7
@@ -1,12 +1,14 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source=writer.go -destination=writer_mock.go -package log
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=writer.go -destination=writer_mock.go -package log
|
||||
type Writer interface {
|
||||
Write(p []byte) (n int, err error)
|
||||
WriteLevel(level zerolog.Level, p []byte) (n int, err error)
|
||||
@@ -24,16 +26,31 @@ func NewWriter() Writer {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *writer) Write(p []byte) (n int, err error) {
|
||||
return l.stdOutWriter.Write(p)
|
||||
func (l *writer) Write(p []byte) (int, error) {
|
||||
n, err := l.stdOutWriter.Write(p)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to write data: %w", err)
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (l *writer) WriteLevel(level zerolog.Level, p []byte) (n int, err error) {
|
||||
func (l *writer) WriteLevel(level zerolog.Level, p []byte) (int, error) {
|
||||
switch level {
|
||||
case zerolog.DebugLevel, zerolog.InfoLevel, zerolog.WarnLevel:
|
||||
return l.stdOutWriter.Write(p)
|
||||
n, err := l.stdOutWriter.Write(p)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to write data: %w", err)
|
||||
}
|
||||
|
||||
return n, nil
|
||||
case zerolog.ErrorLevel:
|
||||
return l.stdErrWriter.Write(p)
|
||||
n, err := l.stdErrWriter.Write(p)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to write data: %w", err)
|
||||
}
|
||||
|
||||
return n, nil
|
||||
default:
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: writer.go
|
||||
|
||||
// Package log is a generated GoMock package.
|
||||
package log
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
zerolog "github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// MockWriter is a mock of Writer interface.
|
||||
type MockWriter struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockWriterMockRecorder
|
||||
}
|
||||
|
||||
// MockWriterMockRecorder is the mock recorder for MockWriter.
|
||||
type MockWriterMockRecorder struct {
|
||||
mock *MockWriter
|
||||
}
|
||||
|
||||
// NewMockWriter creates a new mock instance.
|
||||
func NewMockWriter(ctrl *gomock.Controller) *MockWriter {
|
||||
mock := &MockWriter{ctrl: ctrl}
|
||||
mock.recorder = &MockWriterMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockWriter) EXPECT() *MockWriterMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Write mocks base method.
|
||||
func (m *MockWriter) Write(p []byte) (int, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Write", p)
|
||||
ret0, _ := ret[0].(int)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Write indicates an expected call of Write.
|
||||
func (mr *MockWriterMockRecorder) Write(p interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockWriter)(nil).Write), p)
|
||||
}
|
||||
|
||||
// WriteLevel mocks base method.
|
||||
func (m *MockWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "WriteLevel", level, p)
|
||||
ret0, _ := ret[0].(int)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// WriteLevel indicates an expected call of WriteLevel.
|
||||
func (mr *MockWriterMockRecorder) WriteLevel(level, p interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteLevel", reflect.TypeOf((*MockWriter)(nil).WriteLevel), level, p)
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package util
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
|
||||
var (
|
||||
ErrEmptyNetworkInterfaces = errors.New("could not find network interfaces")
|
||||
ErrGetNetworkInterfaces = errors.New("failed to get network interfaces")
|
||||
ErrInvalidNetworkInterface = errors.New("could not find network interfaces with a valid MAC address")
|
||||
ErrSlicesLengthMismatch = errors.New("slices have different lengths")
|
||||
)
|
||||
@@ -1,62 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/term"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
//go:generate mockgen -source=machine.go -destination=machine_mock.go -package util
|
||||
type Machine interface {
|
||||
MacAddress() (string, error)
|
||||
HomeDirectory() string
|
||||
ReadPassword(fd int) ([]byte, error)
|
||||
}
|
||||
|
||||
type machine struct {
|
||||
os OperatingSystem
|
||||
}
|
||||
|
||||
type MachineArgs struct {
|
||||
OperatingSystem OperatingSystem
|
||||
}
|
||||
|
||||
func NewMachine(args MachineArgs) Machine {
|
||||
return &machine{
|
||||
os: args.OperatingSystem,
|
||||
}
|
||||
}
|
||||
|
||||
func (*machine) MacAddress() (string, error) {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, ErrGetNetworkInterfaces.Error())
|
||||
}
|
||||
|
||||
if len(ifaces) == 0 {
|
||||
return "", errors.New(ErrEmptyNetworkInterfaces.Error())
|
||||
}
|
||||
|
||||
for _, iface := range ifaces {
|
||||
addr := iface.HardwareAddr.String()
|
||||
if addr != "" {
|
||||
return addr, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New(ErrInvalidNetworkInterface.Error())
|
||||
}
|
||||
|
||||
func (m *machine) HomeDirectory() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return filepath.Join(m.os.Getenv("HOMEDRIVE"), m.os.Getenv("HOMEPATH"))
|
||||
}
|
||||
|
||||
return m.os.Getenv("HOME")
|
||||
}
|
||||
|
||||
func (*machine) ReadPassword(fd int) ([]byte, error) {
|
||||
return term.ReadPassword(fd)
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=machine.go -destination=machine_mock.go -package machine
|
||||
type Machine interface {
|
||||
MacAddress() (string, error)
|
||||
HomeDirectory() string
|
||||
ReadPassword(fd int) ([]byte, error)
|
||||
}
|
||||
|
||||
type machine struct {
|
||||
os operatingsystem.OperatingSystem
|
||||
}
|
||||
|
||||
type Args struct {
|
||||
OS operatingsystem.OperatingSystem
|
||||
}
|
||||
|
||||
func New(args Args) Machine {
|
||||
return &machine{
|
||||
os: args.OS,
|
||||
}
|
||||
}
|
||||
|
||||
func (*machine) MacAddress() (string, error) {
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get network interfaces: %w", err)
|
||||
}
|
||||
|
||||
if len(interfaces) == 0 {
|
||||
return "", fmt.Errorf("could not find network interfaces: %w", err)
|
||||
}
|
||||
|
||||
for _, netInterface := range interfaces {
|
||||
addr := netInterface.HardwareAddr.String()
|
||||
if addr != "" {
|
||||
return addr, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("could not find network interfaces with a valid mac address: %w", err)
|
||||
}
|
||||
|
||||
func (m *machine) HomeDirectory() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return filepath.Join(m.os.Getenv("HOMEDRIVE"), m.os.Getenv("HOMEPATH"))
|
||||
}
|
||||
|
||||
return m.os.Getenv("HOME")
|
||||
}
|
||||
|
||||
func (*machine) ReadPassword(fd int) ([]byte, error) {
|
||||
data, err := term.ReadPassword(fd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read password: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
@@ -1,24 +1,32 @@
|
||||
package util
|
||||
package machine
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/majd/ipatool/v2/pkg/util/operatingsystem"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func TestMachine(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Machine Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("Machine", func() {
|
||||
var (
|
||||
ctrl *gomock.Controller
|
||||
machine Machine
|
||||
mockOS *MockOperatingSystem
|
||||
mockOS *operatingsystem.MockOperatingSystem
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
ctrl = gomock.NewController(GinkgoT())
|
||||
mockOS = NewMockOperatingSystem(ctrl)
|
||||
machine = NewMachine(MachineArgs{
|
||||
OperatingSystem: mockOS,
|
||||
mockOS = operatingsystem.NewMockOperatingSystem(ctrl)
|
||||
machine = New(Args{
|
||||
OS: mockOS,
|
||||
})
|
||||
})
|
||||
|
||||
@@ -46,7 +54,7 @@ var _ = Describe("Machine", func() {
|
||||
When("reading password from stdout", func() {
|
||||
It("returns error", func() {
|
||||
_, err := machine.ReadPassword(syscall.Stdout)
|
||||
Expect(err).To(MatchError(ContainSubstring("inappropriate ioctl for device")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,78 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: machine.go
|
||||
|
||||
// Package util is a generated GoMock package.
|
||||
package util
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockMachine is a mock of Machine interface.
|
||||
type MockMachine struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockMachineMockRecorder
|
||||
}
|
||||
|
||||
// MockMachineMockRecorder is the mock recorder for MockMachine.
|
||||
type MockMachineMockRecorder struct {
|
||||
mock *MockMachine
|
||||
}
|
||||
|
||||
// NewMockMachine creates a new mock instance.
|
||||
func NewMockMachine(ctrl *gomock.Controller) *MockMachine {
|
||||
mock := &MockMachine{ctrl: ctrl}
|
||||
mock.recorder = &MockMachineMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockMachine) EXPECT() *MockMachineMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// HomeDirectory mocks base method.
|
||||
func (m *MockMachine) HomeDirectory() string {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "HomeDirectory")
|
||||
ret0, _ := ret[0].(string)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// HomeDirectory indicates an expected call of HomeDirectory.
|
||||
func (mr *MockMachineMockRecorder) HomeDirectory() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HomeDirectory", reflect.TypeOf((*MockMachine)(nil).HomeDirectory))
|
||||
}
|
||||
|
||||
// MacAddress mocks base method.
|
||||
func (m *MockMachine) MacAddress() (string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "MacAddress")
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// MacAddress indicates an expected call of MacAddress.
|
||||
func (mr *MockMachineMockRecorder) MacAddress() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MacAddress", reflect.TypeOf((*MockMachine)(nil).MacAddress))
|
||||
}
|
||||
|
||||
// ReadPassword mocks base method.
|
||||
func (m *MockMachine) ReadPassword(fd int) ([]byte, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "ReadPassword", fd)
|
||||
ret0, _ := ret[0].([]byte)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// ReadPassword indicates an expected call of ReadPassword.
|
||||
func (mr *MockMachineMockRecorder) ReadPassword(fd interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadPassword", reflect.TypeOf((*MockMachine)(nil).ReadPassword), fd)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package util
|
||||
|
||||
// Must is a helper that wraps a call to a function returning (T, error) and panics if the error is non-nil.
|
||||
func Must[T any](val T, err error) T {
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Must", func() {
|
||||
It("returns current value", func() {
|
||||
res := Must("value", nil)
|
||||
Expect(res).To(Equal("value"))
|
||||
})
|
||||
|
||||
It("panics", func() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
Expect(r).To(Equal("test"))
|
||||
}()
|
||||
|
||||
_ = Must("value", errors.New("test"))
|
||||
})
|
||||
})
|
||||
@@ -1,48 +0,0 @@
|
||||
package util
|
||||
|
||||
import "os"
|
||||
|
||||
//go:generate mockgen -source=operating_system.go -destination=operating_system_mock.go -package util
|
||||
type OperatingSystem interface {
|
||||
Getenv(key string) string
|
||||
Stat(name string) (os.FileInfo, error)
|
||||
Executable() (string, error)
|
||||
OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
|
||||
Remove(name string) error
|
||||
IsNotExist(err error) bool
|
||||
MkdirAll(path string, perm os.FileMode) error
|
||||
}
|
||||
|
||||
type operatingSystem struct{}
|
||||
|
||||
func NewOperatingSystem() OperatingSystem {
|
||||
return &operatingSystem{}
|
||||
}
|
||||
|
||||
func (*operatingSystem) Getenv(key string) string {
|
||||
return os.Getenv(key)
|
||||
}
|
||||
|
||||
func (*operatingSystem) Stat(name string) (os.FileInfo, error) {
|
||||
return os.Stat(name)
|
||||
}
|
||||
|
||||
func (*operatingSystem) Executable() (string, error) {
|
||||
return os.Executable()
|
||||
}
|
||||
|
||||
func (*operatingSystem) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
return os.OpenFile(name, flag, perm)
|
||||
}
|
||||
|
||||
func (*operatingSystem) Remove(name string) error {
|
||||
return os.Remove(name)
|
||||
}
|
||||
|
||||
func (*operatingSystem) IsNotExist(err error) bool {
|
||||
return os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func (*operatingSystem) MkdirAll(path string, perm os.FileMode) error {
|
||||
return os.MkdirAll(path, perm)
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: operating_system.go
|
||||
|
||||
// Package util is a generated GoMock package.
|
||||
package util
|
||||
|
||||
import (
|
||||
os "os"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockOperatingSystem is a mock of OperatingSystem interface.
|
||||
type MockOperatingSystem struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockOperatingSystemMockRecorder
|
||||
}
|
||||
|
||||
// MockOperatingSystemMockRecorder is the mock recorder for MockOperatingSystem.
|
||||
type MockOperatingSystemMockRecorder struct {
|
||||
mock *MockOperatingSystem
|
||||
}
|
||||
|
||||
// NewMockOperatingSystem creates a new mock instance.
|
||||
func NewMockOperatingSystem(ctrl *gomock.Controller) *MockOperatingSystem {
|
||||
mock := &MockOperatingSystem{ctrl: ctrl}
|
||||
mock.recorder = &MockOperatingSystemMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockOperatingSystem) EXPECT() *MockOperatingSystemMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Executable mocks base method.
|
||||
func (m *MockOperatingSystem) Executable() (string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Executable")
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Executable indicates an expected call of Executable.
|
||||
func (mr *MockOperatingSystemMockRecorder) Executable() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Executable", reflect.TypeOf((*MockOperatingSystem)(nil).Executable))
|
||||
}
|
||||
|
||||
// Getenv mocks base method.
|
||||
func (m *MockOperatingSystem) Getenv(key string) string {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Getenv", key)
|
||||
ret0, _ := ret[0].(string)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Getenv indicates an expected call of Getenv.
|
||||
func (mr *MockOperatingSystemMockRecorder) Getenv(key interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Getenv", reflect.TypeOf((*MockOperatingSystem)(nil).Getenv), key)
|
||||
}
|
||||
|
||||
// IsNotExist mocks base method.
|
||||
func (m *MockOperatingSystem) IsNotExist(err error) bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "IsNotExist", err)
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// IsNotExist indicates an expected call of IsNotExist.
|
||||
func (mr *MockOperatingSystemMockRecorder) IsNotExist(err interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsNotExist", reflect.TypeOf((*MockOperatingSystem)(nil).IsNotExist), err)
|
||||
}
|
||||
|
||||
// MkdirAll mocks base method.
|
||||
func (m *MockOperatingSystem) MkdirAll(path string, perm os.FileMode) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "MkdirAll", path, perm)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// MkdirAll indicates an expected call of MkdirAll.
|
||||
func (mr *MockOperatingSystemMockRecorder) MkdirAll(path, perm interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MkdirAll", reflect.TypeOf((*MockOperatingSystem)(nil).MkdirAll), path, perm)
|
||||
}
|
||||
|
||||
// OpenFile mocks base method.
|
||||
func (m *MockOperatingSystem) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "OpenFile", name, flag, perm)
|
||||
ret0, _ := ret[0].(*os.File)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// OpenFile indicates an expected call of OpenFile.
|
||||
func (mr *MockOperatingSystemMockRecorder) OpenFile(name, flag, perm interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenFile", reflect.TypeOf((*MockOperatingSystem)(nil).OpenFile), name, flag, perm)
|
||||
}
|
||||
|
||||
// Remove mocks base method.
|
||||
func (m *MockOperatingSystem) Remove(name string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Remove", name)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Remove indicates an expected call of Remove.
|
||||
func (mr *MockOperatingSystemMockRecorder) Remove(name interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Remove", reflect.TypeOf((*MockOperatingSystem)(nil).Remove), name)
|
||||
}
|
||||
|
||||
// Stat mocks base method.
|
||||
func (m *MockOperatingSystem) Stat(name string) (os.FileInfo, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Stat", name)
|
||||
ret0, _ := ret[0].(os.FileInfo)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Stat indicates an expected call of Stat.
|
||||
func (mr *MockOperatingSystemMockRecorder) Stat(name interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stat", reflect.TypeOf((*MockOperatingSystem)(nil).Stat), name)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package operatingsystem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
//go:generate go run github.com/golang/mock/mockgen -source=operatingsystem.go -destination=operatingsystem_mock.go -package operatingsystem
|
||||
type OperatingSystem interface {
|
||||
Getenv(key string) string
|
||||
Stat(name string) (os.FileInfo, error)
|
||||
Getwd() (string, error)
|
||||
OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
|
||||
Remove(name string) error
|
||||
IsNotExist(err error) bool
|
||||
MkdirAll(path string, perm os.FileMode) error
|
||||
Rename(oldPath, newPath string) error
|
||||
}
|
||||
|
||||
type operatingSystem struct{}
|
||||
|
||||
func New() OperatingSystem {
|
||||
return &operatingSystem{}
|
||||
}
|
||||
|
||||
func (operatingSystem) Getenv(key string) string {
|
||||
return os.Getenv(key)
|
||||
}
|
||||
|
||||
func (operatingSystem) Stat(name string) (os.FileInfo, error) {
|
||||
info, err := os.Stat(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to describe file '%s': %w", name, err)
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (operatingSystem) Getwd() (string, error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get current directory: %w", err)
|
||||
}
|
||||
|
||||
return wd, nil
|
||||
}
|
||||
|
||||
func (operatingSystem) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
file, err := os.OpenFile(name, flag, perm)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file '%s': %w", name, err)
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (operatingSystem) Remove(name string) error {
|
||||
err := os.Remove(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove file '%s': %w", name, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (operatingSystem) IsNotExist(err error) bool {
|
||||
return os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func (operatingSystem) MkdirAll(path string, perm os.FileMode) error {
|
||||
err := os.MkdirAll(path, perm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create directory '%s': %w", path, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (operatingSystem) Rename(oldPath, newPath string) error {
|
||||
err := os.Rename(oldPath, newPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to rename '%s' to '%s': %w", oldPath, newPath, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+44
-23
@@ -1,41 +1,50 @@
|
||||
package util
|
||||
package operatingsystem
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"io/fs"
|
||||
goos "os"
|
||||
"path"
|
||||
)
|
||||
|
||||
var _ = Describe("Operating System", func() {
|
||||
var (
|
||||
os OperatingSystem
|
||||
)
|
||||
func TestOS(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "OperatingSystem Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("OperatingSystem", func() {
|
||||
var sut OperatingSystem
|
||||
|
||||
BeforeEach(func() {
|
||||
os = NewOperatingSystem()
|
||||
sut = New()
|
||||
})
|
||||
|
||||
When("env var is set", func() {
|
||||
BeforeEach(func() {
|
||||
err := goos.Setenv("TEST", "true")
|
||||
err := os.Setenv("TEST", "true")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
It("returns env var", func() {
|
||||
res := os.Getenv("TEST")
|
||||
res := sut.Getenv("TEST")
|
||||
Expect(res).To(Equal("true"))
|
||||
})
|
||||
})
|
||||
|
||||
When("file exists", func() {
|
||||
var file *goos.File
|
||||
var file *os.File
|
||||
|
||||
BeforeEach(func() {
|
||||
var err error
|
||||
|
||||
file, err = goos.CreateTemp("", "test_file")
|
||||
file, err = os.CreateTemp("", "test_file")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
@@ -45,29 +54,41 @@ var _ = Describe("Operating System", func() {
|
||||
})
|
||||
|
||||
It("returns file info", func() {
|
||||
res, err := os.Stat(file.Name())
|
||||
res, err := sut.Stat(file.Name())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(res.Name()).To(Equal(path.Base(file.Name())))
|
||||
})
|
||||
|
||||
It("opens file", func() {
|
||||
res, err := os.OpenFile(file.Name(), goos.O_WRONLY, 0644)
|
||||
res, err := sut.OpenFile(file.Name(), os.O_WRONLY, 0644)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(res.Name()).To(Equal(file.Name()))
|
||||
})
|
||||
|
||||
It("removes file", func() {
|
||||
err := os.Remove(file.Name())
|
||||
err := sut.Remove(file.Name())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
_, err = os.Stat(file.Name())
|
||||
Expect(goos.IsNotExist(err)).To(BeTrue())
|
||||
_, err = sut.Stat(file.Name())
|
||||
Expect(os.IsNotExist(errors.Unwrap(err))).To(BeTrue())
|
||||
})
|
||||
|
||||
It("renames file", func() {
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
newPath := fmt.Sprintf("%s/%d", os.TempDir(), r.Intn(100))
|
||||
|
||||
err := sut.Rename(file.Name(), newPath)
|
||||
defer func() {
|
||||
_ = sut.Remove(newPath)
|
||||
}()
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("executing", func() {
|
||||
It("returns executable path", func() {
|
||||
res, err := os.Executable()
|
||||
When("running", func() {
|
||||
It("returns current working directory", func() {
|
||||
res, err := sut.Getwd()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(res).ToNot(BeNil())
|
||||
})
|
||||
@@ -75,14 +96,14 @@ var _ = Describe("Operating System", func() {
|
||||
|
||||
When("error is 'ErrNotExist'", func() {
|
||||
It("returns true", func() {
|
||||
res := os.IsNotExist(fs.ErrNotExist)
|
||||
res := sut.IsNotExist(fs.ErrNotExist)
|
||||
Expect(res).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
||||
When("directory does not exist", func() {
|
||||
It("creates directory", func() {
|
||||
err := os.MkdirAll(goos.TempDir(), 0664)
|
||||
err := sut.MkdirAll(os.TempDir(), 0664)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
})
|
||||
@@ -1,9 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func RandInt(min, max int) int {
|
||||
return rand.Intn(max-min) + min
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Rand", func() {
|
||||
It("returns random integer in range", func() {
|
||||
res := RandInt(1, 3)
|
||||
Expect(res).To(BeNumerically(">=", 1))
|
||||
Expect(res).To(BeNumerically("<=", 3))
|
||||
})
|
||||
})
|
||||
+1
-1
@@ -6,4 +6,4 @@ func IfEmpty(value, fallback string) string {
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUtil(t *testing.T) {
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package util
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
import "errors"
|
||||
|
||||
type Pair[T, U any] struct {
|
||||
First T
|
||||
@@ -9,7 +9,7 @@ type Pair[T, U any] struct {
|
||||
|
||||
func Zip[T, U any](ts []T, us []U) ([]Pair[T, U], error) {
|
||||
if len(ts) != len(us) {
|
||||
return nil, errors.New(ErrSlicesLengthMismatch.Error())
|
||||
return nil, errors.New("slices have different lengths")
|
||||
}
|
||||
|
||||
pairs := make([]Pair[T, U], len(ts))
|
||||
|
||||
@@ -9,7 +9,7 @@ var _ = Describe("Zip", func() {
|
||||
When("slices have different lengths", func() {
|
||||
It("returns error", func() {
|
||||
_, err := Zip([]string{}, []string{"test"})
|
||||
Expect(err).To(MatchError(ContainSubstring("slices have different lengths")))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#!/bin/sh -e
|
||||
|
||||
VERSION=$(git describe --abbrev=0 --tags | cut -c 2-)
|
||||
BASEDIR=$(dirname "$0")
|
||||
|
||||
cat <<EOF >"$BASEDIR/../cmd/version.go"
|
||||
package cmd
|
||||
|
||||
// Automatically generated
|
||||
const version = "$VERSION"
|
||||
EOF
|
||||
|
||||
echo "Version ${VERSION}"
|
||||
Reference in new issue
Block a user