 }
 
 let command = CommandLine.arguments[1]
+var spotify = Spotify()
 
 switch (command) {
 case "info":
-  dump(Spotify.currentlyPlaying().json)
+  dump(spotify.currentlyPlaying().json)
 case "save":
-  let result = Spotify.saveToLibrary()
+  let result = spotify.saveToLibrary()
   if result.json.count > 0 {
     dump(result.json)
   } else {

    }
  }

  private static func apiRequest(_ route: String, method: HTTPMethod? = nil, body: String? = nil) -> JSONResponse {
    return apiRequest(route, method: method, body: body?.data(using: .utf8))
  }

  private static func apiRequest(_ route: String, method: HTTPMethod? = nil) -> JSONResponse {
    let thisVarHelpsAvoidAmbiguity: Data? = nil
    return apiRequest(route, method: method, body: thisVarHelpsAvoidAmbiguity)
  }

  private static func refreshToken() -> String {
    let credentials = Credentials.load()
    let auth = Utils.b64("\(credentials.clientId):\(credentials.clientSecret)")
    let (json, response, _) = Utils.synchronousRequest(
      "https://accounts.spotify.com/api/token",

  var accessToken: String
  var clientId: String
  var clientSecret: String
}


struct DB: Codable {
  var credentials: Credentials
  var playlistIds: [String: String]

  static let file = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".sptf.json")

  static func load() -> DB {
    let fileData = try! Data(contentsOf: DB.file)
    return try! JSONDecoder().decode(DB.self, from: fileData)
  }

  static func updateCredentials(_ newCredentials: Credentials) -> DB {
    var existingDb = DB.load()
    existingDb.credentials = newCredentials

    let json = try! JSONEncoder().encode(existingDb)
    let output = String(data: json, encoding: .ascii)!

    try! output.write(to: DB.file, atomically: true, encoding: .ascii)

    return existingDb
  }
}

