mirror of
https://github.com/mas-cli/mas
synced 2024-11-22 03:23:08 +00:00
Implements #26: Add info command
This commit is contained in:
parent
778aa74d99
commit
aefaeb5e05
5 changed files with 113 additions and 4 deletions
|
@ -16,6 +16,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 */; };
|
||||
AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9D96DDBBCCCC5944160ABE /* ArgumentParser.swift */; };
|
||||
ADE553C828AF4EAFF39ED3E1 /* ArgumentProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4237E5AA1A289D03D2A2FB8 /* ArgumentProtocol.swift */; };
|
||||
EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */; };
|
||||
|
@ -60,6 +61,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>"; };
|
||||
|
@ -206,6 +208,7 @@
|
|||
EDE296521C700F4300554778 /* SignOut.swift */,
|
||||
EDD3B3621C34709400B56B88 /* Upgrade.swift */,
|
||||
EDB6CE8B1BAEC3D400648B4D /* Version.swift */,
|
||||
900A1E801DBAC8CB0069B1A8 /* Info.swift */,
|
||||
);
|
||||
name = Commands;
|
||||
path = "mas-cli/Commands";
|
||||
|
@ -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 */,
|
||||
|
|
105
mas-cli/Commands/Info.swift
Normal file
105
mas-cli/Commands/Info.swift
Normal file
|
@ -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 {
|
||||
|
||||
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 ?? ""
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue