| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You 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. |
| */ |
| |
| package config |
| |
| import ( |
| "bufio" |
| "fmt" |
| "os" |
| "strings" |
| |
| "github.com/openwhisk/openwhisk-client-go/whisk" |
| "github.com/openwhisk/openwhisk-wskdeploy/utils" |
| ) |
| |
| var ClientConfig *whisk.Config |
| |
| // Load configuration will load properties from a file |
| func LoadConfiguration(propPath string) ([]string, error) { |
| props, err := ReadProps(propPath) |
| utils.Check(err) |
| Namespace := props["NAMESPACE"] |
| Apihost := props["APIHOST"] |
| Authtoken := props["AUTH"] |
| return []string{Namespace, Apihost, Authtoken}, nil |
| } |
| |
| func ReadProps(path string) (map[string]string, error) { |
| |
| props := map[string]string{} |
| |
| file, err := os.Open(path) |
| if err != nil { |
| // If file does not exist, just return props |
| fmt.Printf("Unable to read whisk properties file '%s' (file open error: %s); falling back to default properties\n", path, err) |
| return props, nil |
| } |
| defer file.Close() |
| |
| lines := []string{} |
| scanner := bufio.NewScanner(file) |
| for scanner.Scan() { |
| lines = append(lines, scanner.Text()) |
| } |
| |
| props = map[string]string{} |
| for _, line := range lines { |
| kv := strings.Split(line, "=") |
| if len(kv) != 2 { |
| // Invalid format; skip |
| continue |
| } |
| props[kv[0]] = kv[1] |
| } |
| |
| return props, nil |
| |
| } |