Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements #26: Add info command #55

Merged
merged 2 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mas-cli.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
30EA893640B02CCF679F9C57 /* Option.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD7FE171F643805F7BC38A7 /* Option.swift */; };
693A98991CBFFA760004D3B4 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A98981CBFFA760004D3B4 /* Search.swift */; };
693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */; };
900A1E811DBAC8CB0069B1A8 /* Info.swift in Sources */ = {isa = PBXBuildFile; fileRef = 900A1E801DBAC8CB0069B1A8 /* Info.swift */; };
92AE0FD7BE06D64692E6C1E6 /* OrderedSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9871C2273F4D762A1F19B07 /* OrderedSet.swift */; };
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9D96DDBBCCCC5944160ABE /* ArgumentParser.swift */; };
ADE553C828AF4EAFF39ED3E1 /* ArgumentProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4237E5AA1A289D03D2A2FB8 /* ArgumentProtocol.swift */; };
Expand Down Expand Up @@ -59,6 +60,7 @@
693A98981CBFFA760004D3B4 /* Search.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = "<group>"; };
693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURLSession+Synchronous.swift"; sourceTree = "<group>"; };
8FDC2B8063EC231E28353D23 /* HelpCommand.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = HelpCommand.swift; path = Seeds/Commandant/Sources/Commandant/HelpCommand.swift; sourceTree = "<group>"; };
900A1E801DBAC8CB0069B1A8 /* Info.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Info.swift; sourceTree = "<group>"; };
9257C5FABA335E5F060CB7F7 /* Result.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Seeds/Result/Result/Result.swift; sourceTree = "<group>"; };
AF1B6BEDF32AF3F8A575FB1F /* Switch.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Switch.swift; path = Seeds/Commandant/Sources/Commandant/Switch.swift; sourceTree = "<group>"; };
B4237E5AA1A289D03D2A2FB8 /* ArgumentProtocol.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ArgumentProtocol.swift; path = Seeds/Commandant/Sources/Commandant/ArgumentProtocol.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -206,6 +208,7 @@
EDE296521C700F4300554778 /* SignOut.swift */,
EDD3B3621C34709400B56B88 /* Upgrade.swift */,
EDB6CE8B1BAEC3D400648B4D /* Version.swift */,
900A1E801DBAC8CB0069B1A8 /* Info.swift */,
);
name = Commands;
path = "mas-cli/Commands";
Expand Down Expand Up @@ -351,6 +354,7 @@
ED0F23871B87537200AE40CD /* Account.swift in Sources */,
F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */,
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */,
900A1E811DBAC8CB0069B1A8 /* Info.swift in Sources */,
ADE553C828AF4EAFF39ED3E1 /* ArgumentProtocol.swift in Sources */,
0C47E694564FCB59996690DD /* Command.swift in Sources */,
ED0F238B1B87569C00AE40CD /* Downloader.swift in Sources */,
Expand Down
105 changes: 105 additions & 0 deletions mas-cli/Commands/Info.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// Info.swift
// mas-cli
//
// Created by Denis Lebedev on 21/10/2016.
// Copyright © 2016 Andrew Naylor. All rights reserved.
//

import Foundation

struct InfoCommand: CommandProtocol {
let verb = "info"
let function = "Display app information from the Mac App Store"

func run(_ options: InfoOptions) -> Result<(), MASError> {
guard let infoURLString = infoURLString(options.appId),
let searchJson = URLSession.requestSynchronousJSONWithURLString(infoURLString) as? [String: Any] else {
return .failure(.searchFailed)
}

guard let resultCount = searchJson[ResultKeys.ResultCount] as? Int, resultCount > 0,
let results = searchJson[ResultKeys.Results] as? [[String: Any]],
let result = results.first else {
print("No results found")
return .failure(.noSearchResultsFound)
}

print(AppInfoFormatter.format(appInfo: result))

return .success(())
}

private func infoURLString(_ appId: String) -> String? {
if let urlEncodedAppId = appId.URLEncodedString {
return "https://itunes.apple.com/lookup?id=\(urlEncodedAppId)"
}
return nil
}
}

struct InfoOptions: OptionsProtocol {
let appId: String

static func create(_ appId: String) -> InfoOptions {
return InfoOptions(appId: appId)
}

static func evaluate(_ m: CommandMode) -> Result<InfoOptions, CommandantError<MASError>> {
return create
<*> m <| Argument(usage: "the app id to show info")
}
}

private struct AppInfoFormatter {
rgoldberg marked this conversation as resolved.
Show resolved Hide resolved

private enum Keys {
static let Name = "trackCensoredName"
static let Version = "version"
static let Price = "formattedPrice"
static let Seller = "sellerName"
static let VersionReleaseDate = "currentVersionReleaseDate"
static let MinimumOS = "minimumOsVersion"
static let FileSize = "fileSizeBytes"
static let AppStoreUrl = "trackViewUrl"
}

static func format(appInfo: [String: Any]) -> String {
let headline = [
"\(appInfo.stringOrEmpty(key: Keys.Name))",
"\(appInfo.stringOrEmpty(key: Keys.Version))",
"[\(appInfo.stringOrEmpty(key: Keys.Price))]",
].joined(separator: " ")

return [
headline,
"By: \(appInfo.stringOrEmpty(key: Keys.Seller))",
"Released: \(humaReadableDate(appInfo.stringOrEmpty(key: Keys.VersionReleaseDate)))",
"Minimum OS: \(appInfo.stringOrEmpty(key: Keys.MinimumOS))",
"Size: \(humanReadableSize(appInfo.stringOrEmpty(key: Keys.FileSize)))",
"From: \(appInfo.stringOrEmpty(key: Keys.AppStoreUrl))",
].joined(separator: "\n")
}

private static func humanReadableSize(_ size: String) -> String {
let bytesSize = Int64(size) ?? 0
return ByteCountFormatter.string(fromByteCount: bytesSize, countStyle: .file)
}

private static func humaReadableDate(_ serverDate: String) -> String {
let serverDateFormatter = DateFormatter()
serverDateFormatter.locale = Locale(identifier: "en_US_POSIX")
serverDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"

let humanDateFormatter = DateFormatter()
humanDateFormatter.timeStyle = .none
humanDateFormatter.dateStyle = .medium
return serverDateFormatter.date(from: serverDate).flatMap(humanDateFormatter.string(from:)) ?? ""
}
}

extension Dictionary {
fileprivate func stringOrEmpty(key: Key) -> String {
return self[key] as? String ?? ""
}
}
2 changes: 1 addition & 1 deletion mas-cli/Commands/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct SearchCommand: CommandProtocol {
}

func searchURLString(_ appName: String) -> String? {
if let urlEncodedAppName = appName.URLEncodedString() {
if let urlEncodedAppName = appName.URLEncodedString {
return "https://itunes.apple.com/search?entity=macSoftware&term=\(urlEncodedAppName)&attribute=allTrackTerm"
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions mas-cli/NSURLSession+Synchronous.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public extension URLSession {
public extension String {

/// Return an URL encoded string
func URLEncodedString() -> String? {
let customAllowedSet = CharacterSet.urlQueryAllowed
return addingPercentEncoding(withAllowedCharacters: customAllowedSet)
var URLEncodedString: String? {
return addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
}
}

1 change: 1 addition & 0 deletions mas-cli/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct StderrOutputStream: TextOutputStream {
let registry = CommandRegistry<MASError>()
let helpCommand = HelpCommand(registry: registry)
registry.register(AccountCommand())
registry.register(InfoCommand())
registry.register(InstallCommand())
registry.register(ListCommand())
registry.register(OutdatedCommand())
Expand Down