Skip to content

Instantly share code, notes, and snippets.

@hfossli
Last active January 28, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hfossli/31f47c86630deb8aa22a01c346dae739 to your computer and use it in GitHub Desktop.
Save hfossli/31f47c86630deb8aa22a01c346dae739 to your computer and use it in GitHub Desktop.
Switch constants easily with this swift CLI
#!/usr/bin/swift
import Foundation
struct Config {
enum Environment: String {
case dev
case prod
}
var environment: Environment = .prod
var pinning: Bool = true
var server: String {
switch environment {
case .dev:
return "https://dev.acme.com/"
case .prod:
return "https://acme.com/"
}
}
}
struct ANSIColors {
static let clear = "\u{001B}[0m"
static let red = "\u{001B}[38;5;160m"
static let green = "\u{001B}[0;32m"
}
func usage(error: String) -> Never {
let scriptLocation = CommandLine.arguments.first ?? "script.swift"
print(ANSIColors.red, "👉 ", error, ANSIColors.clear, separator: "")
print(ANSIColors.red, "Script failed ", scriptLocation, ANSIColors.clear, separator: "")
let defaultConfig = Config()
print("""
Usage: \(scriptLocation)
--environment Desired environment. Defaults to "\(defaultConfig.environment)".
--[no]-pinning Use https public key pinning. Defaults to \(defaultConfig.pinning).
""")
exit(1)
}
func failed(error: Error) {
print(ANSIColors.red, "Script failed due to error:", ANSIColors.clear, "\n", error)
exit(1)
}
func parseCLIArguments() -> Config {
var config = Config()
var arguments = CommandLine.arguments
arguments.removeFirst()
while arguments.isEmpty == false {
let argument = arguments.removeFirst()
switch argument {
case "--pinning":
config.pinning = true
case "--no-pinning":
config.pinning = false
case "--environment":
guard !arguments.isEmpty, let value = Config.Environment(rawValue: arguments.removeFirst()) else {
usage(error: "Bad value passed to \(argument) option")
}
config.environment = value
default:
usage(error: "Unknown argument \"\(argument)\"")
}
}
return config
}
func apply(config: Config) {
do {
try """
APP_HTTPS_PUBLIC_KEY_PINNING=\(config.pinning ? 1 : 0)
""".write(to: URL(fileURLWithPath: "App.xconfig"), atomically: false, encoding: .utf8)
try """
NSString *const kServer = @"\(config.server)";
""".write(to: URL(fileURLWithPath: "Constants.m"), atomically: false, encoding: .utf8)
try """
struct Config {
let server = "\(config.server)"
}
""".write(to: URL(fileURLWithPath: "Constants.swift"), atomically: false, encoding: .utf8)
} catch {
failed(error: error)
}
}
let config = parseCLIArguments()
apply(config: config)
@hfossli
Copy link
Author

hfossli commented Jan 21, 2019

Example usage

./switch.swift --environment "dev" --pinning

The script will then write 3 files

  • App.xconfig
  • Constants.m
  • Constants.swift

The contents of these files is just an example

@hfossli
Copy link
Author

hfossli commented Jan 28, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment