<commit_before>//
//  MacHost+SSH.swift
//  MacKey
//
//  Created by <NAME> on 5/14/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import Result

extension MacHost {
    func executeCmd(cmd: String) -> Result<String, NSError> {
        if cmd == "unlock" {
            let command = getDetailCommand(cmd)
            var result = SshUtils.executeSshCmdWithPassword(command, host: host, username: user, password: password)
            switch result {
            case .Success:
                if result.value! == "" {
                    result = .Success("Mac is unlocked")
                }
            default:
                break
            }
            return result
        }

        return .Failure(NSError(domain:"MacKey", code: 121, userInfo: [NSLocalizedDescriptionKey : "This command is not supported."]))
    }
    
    func getDetailCommand(cmd: String) -> String {
        switch cmd {
        case "unlock":
            return "caffeinate  -u -t 1;" +  // Wake up the screen.
                "d=$(/usr/bin/python -c 'import Quartz; print Quartz.CGSessionCopyCurrentDictionary()');" +
                "echo $d | grep -q 'OnConsoleKey = 0' && { echo 'Needs to unlock manually'; echo $d; return; };" +
                "echo $d | grep -q 'ScreenIsLocked = 1' || { echo 'Mac is already unlocked'; return; }; " +
                "osascript -e 'tell application \"System Events\"' -e 'keystroke \"\(self.password)\"' -e 'delay 0.5' -e 'keystroke return' -e 'end tell'"
        case "sleep":
            return "osascript -e 'tell application \"Finder\" to sleep'"
        default:<commit_msg>Changed 'Mac is unlocked' to 'Unlock request was sent' since unlock request may fail.<commit_after>//
//  MacHost+SSH.swift
//  MacKey
//
//  Created by <NAME> on 5/14/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import Result

extension MacHost {
    func executeCmd(cmd: String) -> Result<String, NSError> {
        if cmd == "unlock" {
            let command = getDetailCommand(cmd)
            var result = SshUtils.executeSshCmdWithPassword(command, host: host, username: user, password: password)
            switch result {
            case .Success:
                if result.value! == "" {
                    result = .Success("Unlock request was sent")
                }
            default:
                break
            }
            return result
        }

        return .Failure(NSError(domain:"MacKey", code: 121, userInfo: [NSLocalizedDescriptionKey : "This command is not supported."]))
    }
    
    func getDetailCommand(cmd: String) -> String {
        switch cmd {
        case "unlock":
            return "caffeinate  -u -t 1;" +  // Wake up the screen.
                "d=$(/usr/bin/python -c 'import Quartz; print Quartz.CGSessionCopyCurrentDictionary()');" +
                "echo $d | grep -q 'OnConsoleKey = 0' && { echo 'Needs to unlock manually'; echo $d; return; };" +
                "echo $d | grep -q 'ScreenIsLocked = 1' || { echo 'Mac is already unlocked'; return; }; " +
                "osascript -e 'tell application \"System Events\"' -e 'keystroke \"\(self.password)\"' -e 'delay 0.5' -e 'keystroke return' -e 'end tell'"
        case "sleep":
            return "osascript -e 'tell application \"Finder\" to sleep'"
        default:
{'fraction_non_alphanumeric': 0.1293309438470729, 'fraction_numerical': 0.01015531660692951, 'mean_word_length': 1.937719298245614, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4799420', 'n_tokens_mistral': 1003, 'n_tokens_neox': 932, 'n_words': 301}