From 5aecfd76943c98d6c1bd196dad7341f806f21ddf Mon Sep 17 00:00:00 2001 From: Jessica Pittman Date: Wed, 21 Sep 2016 13:37:21 -0500 Subject: [PATCH] Cloud_config support added. (#12) * Added cloud_config support. Removed develop branch of buildpack from manifest file. * Added more stuff to make cloud_config work. It should check for a cloud_config file now. Got a real file to actually test with. So this should work. * Syntax changes. --- Sources/Deploy/Configuration.swift | 66 ++++++++++++++++++++++++++++++ Sources/Deploy/main.swift | 5 +-- manifest.yml | 1 - 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 Sources/Deploy/Configuration.swift diff --git a/Sources/Deploy/Configuration.swift b/Sources/Deploy/Configuration.swift new file mode 100644 index 0000000..dcfa677 --- /dev/null +++ b/Sources/Deploy/Configuration.swift @@ -0,0 +1,66 @@ +/** +* Copyright IBM Corporation 2016 +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import Foundation +import CouchDB +import SwiftyJSON +import LoggerAPI +import CloudFoundryEnv + +public struct Configuration { + + private static var configurationFile = "cloud_config.json" + + public static func getConfiguration() -> Service? { + var appEnv: AppEnv + do { + let path = Configuration.getAbsolutePath(relativePath: "/\(configurationFile)", useFallback: false) + if path != nil { + let data = try Data(contentsOf: URL(fileURLWithPath: path!)) + let configJson = JSON(data: data) + appEnv = try CloudFoundryEnv.getAppEnv(options: configJson) + Log.info("Using configuration values from '\(configurationFile)'.") + } else { + Log.warning("Could not find '\(configurationFile)'.") + appEnv = try CloudFoundryEnv.getAppEnv() + } + return appEnv.getService(spec: "TodoList-CouchDB") + } catch { + Log.warning("An error occurred while trying to read configurations.") + return nil + } + } + + private static func getAbsolutePath(relativePath: String, useFallback: Bool) -> String? { + let initialPath = #file + let components = initialPath.characters.split(separator: "/").map(String.init) + let notLastThree = components[0..