Add command "lucky"

This commit is contained in:
Pablo Varela 2017-05-11 16:29:18 -04:00
parent 281dcf084c
commit 705e7b1f65
4 changed files with 96 additions and 0 deletions

View file

@ -47,6 +47,12 @@ Use `mas outdated` to list all applications with pending updates.
$ mas outdated
497799835 Xcode (7.0)
446107677 Screens VNC - Access Your Computer From Anywhere (3.6.7)
If you want to install the first result that the `search` command would pompt you:
$ mas lucky twitter
==> Downloading Twitter
==> Installed Twitter
> `mas` is only able to install/update applications that are listed in the Mac App Store itself. Use [`softwareupdate(8)`](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man8/softwareupdate.8.html) utility for downloading system updates (like iTunes, Xcode Command Line Tools, etc)

View file

@ -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 */; };
8078FAA81EC4F2FB004B5B3F /* Lucky.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8078FAA71EC4F2FB004B5B3F /* Lucky.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 */; };
@ -59,6 +60,7 @@
55E3BFBE58DFCE19A53A23D7 /* LinuxSupport.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = LinuxSupport.swift; path = Seeds/Commandant/Sources/Commandant/LinuxSupport.swift; sourceTree = "<group>"; };
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>"; };
8078FAA71EC4F2FB004B5B3F /* Lucky.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lucky.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>"; };
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>"; };
@ -201,6 +203,7 @@
ED0F23821B87533A00AE40CD /* List.swift */,
ED0F23841B87536A00AE40CD /* Outdated.swift */,
EDCBF9521D89AC6F000039C6 /* Reset.swift */,
8078FAA71EC4F2FB004B5B3F /* Lucky.swift */,
693A98981CBFFA760004D3B4 /* Search.swift */,
EDC90B641C70045E0019E396 /* SignIn.swift */,
EDE296521C700F4300554778 /* SignOut.swift */,
@ -362,6 +365,7 @@
25209791ED0F49CF5BAF7348 /* LinuxSupport.swift in Sources */,
ED0F23831B87533A00AE40CD /* List.swift in Sources */,
ED031A7C1B5127C00097692E /* main.swift in Sources */,
8078FAA81EC4F2FB004B5B3F /* Lucky.swift in Sources */,
693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */,
30EA893640B02CCF679F9C57 /* Option.swift in Sources */,
ED0F23851B87536A00AE40CD /* Outdated.swift in Sources */,

View file

@ -0,0 +1,85 @@
//
// Lucky.swift
// mas-cli
//
// Created by Pablo Varela on 05/11/17.
// Copyright © 2016 Andrew Naylor. All rights reserved.
//
struct LuckyCommand: CommandProtocol {
typealias Options = LuckyOptions
let verb = "lucky"
let function = "Install the first result from the Mac App Store"
func run(_ options: Options) -> Result<(), MASError> {
guard let searchURLString = searchURLString(options.appName),
let searchJson = URLSession.requestSynchronousJSONWithURLString(searchURLString) 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]] else {
print("No results found")
return .failure(.noSearchResultsFound)
}
let appId = results[0][ResultKeys.TrackId] as! UInt64
return install(appId, options: options)
}
fileprivate func install(_ appId: UInt64, options: Options) -> Result<(), MASError> {
// Try to download applications with given identifiers and collect results
let downloadResults = [appId].flatMap { (appId) -> MASError? in
if let product = installedApp(appId) , !options.forceInstall {
printWarning("\(product.appName) is already installed")
return nil
}
return download(appId)
}
switch downloadResults.count {
case 0:
return .success()
case 1:
return .failure(downloadResults[0])
default:
return .failure(.downloadFailed(error: nil))
}
}
fileprivate func installedApp(_ appId: UInt64) -> CKSoftwareProduct? {
let appId = NSNumber(value: appId)
let softwareMap = CKSoftwareMap.shared()
return softwareMap.allProducts()?.first { $0.itemIdentifier == appId }
}
func searchURLString(_ appName: String) -> String? {
if let urlEncodedAppName = appName.URLEncodedString() {
return "https://itunes.apple.com/search?entity=macSoftware&term=\(urlEncodedAppName)&attribute=allTrackTerm"
}
return nil
}
}
struct LuckyOptions: OptionsProtocol {
let appName: String
let forceInstall: Bool
static func create(_ appName: String) -> (_ forceInstall: Bool) -> LuckyOptions {
return { forceInstall in
return LuckyOptions(appName: appName, forceInstall: forceInstall)
}
}
static func evaluate(_ m: CommandMode) -> Result<LuckyOptions, CommandantError<MASError>> {
return create
<*> m <| Argument(usage: "the app name to install")
<*> m <| Switch(flag: nil, key: "force", usage: "force reinstall")
}
}

View file

@ -19,6 +19,7 @@ let helpCommand = HelpCommand(registry: registry)
registry.register(AccountCommand())
registry.register(InstallCommand())
registry.register(ListCommand())
registry.register(LuckyCommand())
registry.register(OutdatedCommand())
registry.register(ResetCommand())
registry.register(SearchCommand())