-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateRemoteDesktops.swift
executable file
·95 lines (83 loc) · 2.83 KB
/
createRemoteDesktops.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env swift
// Ouput .rdp files to ~/rdp directory
// 1. Execute this file.
// 2. Open Microsoft Remote Desktop
// 3. File > Import.. Select all .rdp files in rdp/
// Usage: ./createRemoteDesktops.swift
import Foundation
extension String {
func appendPath(path: String) -> String {
let nsString = self as NSString
return nsString.stringByAppendingPathComponent(path)
}
}
let fileManager = NSFileManager.defaultManager()
let currentDirectoryPath = fileManager.currentDirectoryPath
let formatPath: String = {
return currentDirectoryPath.appendPath("format.txt")
}()
let datasourcePath: String = {
return currentDirectoryPath.appendPath("datasource.csv")
}()
let subDirectory = "rdp"
func createDirectoryIfNeed(directoryPath: String) {
guard !fileManager.fileExistsAtPath(directoryPath) else {
return
}
try! fileManager.createDirectoryAtPath(directoryPath, withIntermediateDirectories: false, attributes: nil)
}
struct Content {
var name: String?
var ipAddress: String?
var userName: String?
var password: String?
var redirectionPath: String?
}
func read(path filePath: String) -> String {
return try! String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding)
}
func save(fileName: String, data: String) {
let directoryPath = currentDirectoryPath.appendPath(subDirectory)
createDirectoryIfNeed(directoryPath)
let filePath = directoryPath.appendPath(fileName)
try! data.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding)
}
func createFormat(content: Content) -> String {
let data = read(path: formatPath)
var components = [String]()
data.enumerateLines{ (line, stop) in
components.append(line)
}
return components.joinWithSeparator("\n")
.stringByReplacingOccurrencesOfString("${IPADDRESS}", withString: content.ipAddress!)
.stringByReplacingOccurrencesOfString("${USERNAME}", withString: content.userName!)
}
func getContents() -> [Content] {
let data = read(path: datasourcePath)
var contents = [Content]()
var first = false
data.enumerateLines { (line, stop) -> () in
guard first else {
first = true
return
}
let components = line.componentsSeparatedByString(",")
var content = Content()
content.name = components[0]
content.ipAddress = components[1]
content.userName = components[2]
content.password = components[3]
// content.redirectionPath = components[4]
contents.append(content)
}
return contents
}
let contents = getContents()
for content in contents {
let format = createFormat(content)
let fileName: String = {
return content.name! + "-" + content.ipAddress! + ".rdp"
}()
save(fileName, data: format)
print("Saved! \(subDirectory)/\(fileName)")
}