0001 // 0002 // Env.swift 0003 // Swiftline 0004 // 0005 // Created by Omar Abdelhafith on 24/11/2015. 0006 // Copyright © 2015 Omar Abdelhafith. All rights reserved. 0007 // 0008 0009 import Darwin 0010 0011 0012 public class Env { 0013 0014 /// Return the list of all the enviromenment keys passed to the script 0015 public static var keys: [String] { 0016 let keyValues = run("env").stdout.componentsSeparatedByString("\n") 0017 let keys = keyValues.map { $0.componentsSeparatedByString("=").first! }.filter { !$0.isEmpty } 0018 return keys 0019 } 0020 0021 /// Return the list of all the enviromenment values passed to the script 0022 public static var values
Env.swift:23 return self.keys.map { self.get($0)! }Env.swift:57 self.keysEnv.swift:71 return self.keys.contains(key)Env.swift:92 zip(self.keys, self.values).forEach(callback): [String] { 0023 return self.keys.map { self.get($0)! } 0024 } 0025 0026 /** 0027 Return the enviromenment for the provided key 0028 0029 - parameter key: The enviromenment variable key 0030 0031 - returns: The enviromenment variable value 0032 */ 0033 public static func get
Env.swift:83 return self.values.contains(value)Env.swift:92 zip(self.keys, self.values).forEach(callback)(key: String) -> String? { 0034 let value = getenv(key) 0035 return String.fromCString(value) 0036 } 0037 0038 /** 0039 Set a new value for the enviromenment variable 0040 0041 - parameter key: The enviromenment variable key 0042 - parameter value: The enviromenment variable value 0043 */ 0044 public static func set
Env.swift:23 return self.keys.map { self.get($0)! }(key: String, _ value: String?) { 0045 if let newValue = value { 0046 setenv(key, newValue, 1) 0047 } else { 0048 unsetenv(key) 0049 } 0050 } 0051 0052 0053 /** 0054 Clear all the enviromenment variables 0055 */ 0056 public static func clear() { 0057 self.keys 0058 .map { String($0) } 0059 .filter { $0 != nil } 0060 .forEach{ self.set($0!, nil) } 0061 } 0062 0063 /** 0064 Check if the enviromenment variable key exists 0065 0066 - parameter key: The enviromenment variable key 0067 0068 - returns: true if exists false otherwise 0069 */ 0070 public static func hasKey(key: String) -> Bool { 0071 return self.keys.contains(key) 0072 } 0073 0074 0075 /** 0076 Check if the enviromenment variable value exists 0077 0078 - parameter key: The enviromenment variable value 0079 0080 - returns: true if exists false otherwise 0081 */ 0082 public static func hasValue(value: String) -> Bool { 0083 return self.values.contains(value) 0084 } 0085 0086 /** 0087 Iterate through the list of enviromenment variables 0088 0089 - parameter callback: callback to call on each key/value pair 0090 */ 0091 public static func eachPair(callback: (key: String, value: String) -> ()) { 0092 zip(self.keys, self.values).forEach(callback) 0093 } 0094 0095 } 0096
Env.swift:60 .forEach{ self.set($0!, nil) }