github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/cli/client/opts.go (about) 1 // Copyright (C) 2015 NTT Innovation Institute, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package client 17 18 import ( 19 "fmt" 20 "os" 21 "strconv" 22 "time" 23 24 l "github.com/cloudwan/gohan/log" 25 ) 26 27 var ( 28 gohanEndpointURLKey = "GOHAN_ENDPOINT_URL" 29 gohanServiceNameKey = "GOHAN_SERVICE_NAME" 30 gohanRegionKey = "GOHAN_REGION" 31 gohanSchemaURLKey = "GOHAN_SCHEMA_URL" 32 keystoneDomainNameKey = "OS_DOMAIN_NAME" 33 keystoneDomainIDKey = "OS_DOMAIN_ID" 34 keystoneTokenIDKey = "OS_TOKEN_ID" 35 cacheSchemasKey = "GOHAN_CACHE_SCHEMAS" 36 cacheTimeoutKey = "GOHAN_CACHE_TIMEOUT" 37 cachePathKey = "GOHAN_CACHE_PATH" 38 envVariableNotSetError = "Environment variable %v needs to be set" 39 envVariablesNotSetError = "Environment variable %v or %v needs to be set" 40 incorrectOutputFormat = "Incorrect output format. Available formats: %v" 41 incorrectVerbosityLevel = "Incorrect verbosity level. Available level range %d %d" 42 incorrectValueForArgument = "Incorrect value for '%s' environment variable, should be %s" 43 44 defaultCachedSchemasPath = "/tmp/.cached-gohan-schemas" 45 46 outputFormatKey = "output-format" 47 outputFormatEnvKey = "GOHAN_OUTPUT_FORMAT" 48 outputFormatTable = "table" 49 outputFormatJSON = "json" 50 outputFormats = []string{outputFormatTable, outputFormatJSON} 51 52 logLevelKey = "verbosity" 53 logLevelEnvKey = "GOHAN_VERBOSITY" 54 logLevels = []l.Level{ 55 l.WARNING, 56 l.NOTICE, 57 l.INFO, 58 l.DEBUG, 59 } 60 defaultLogLevel = l.WARNING 61 62 commonParams = map[string]struct{}{ 63 outputFormatKey: struct{}{}, 64 logLevelKey: struct{}{}, 65 } 66 ) 67 68 // GohanClientCLIOpts options for GohanClientCLI 69 type GohanClientCLIOpts struct { 70 authTokenID string 71 72 cacheSchemas bool 73 cacheTimeout time.Duration 74 cachePath string 75 76 gohanEndpointURL string 77 gohanServiceName string 78 gohanRegion string 79 gohanSchemaURL string 80 81 outputFormat string 82 logLevel l.Level 83 } 84 85 // NewOptsFromEnv creates new Opts for GohanClientCLI using env variables 86 func NewOptsFromEnv() (*GohanClientCLIOpts, error) { 87 opts := GohanClientCLIOpts{ 88 outputFormat: outputFormatTable, 89 cacheSchemas: true, 90 cacheTimeout: 5 * time.Minute, 91 cachePath: defaultCachedSchemasPath, 92 logLevel: defaultLogLevel, 93 } 94 95 opts.gohanEndpointURL = os.Getenv(gohanEndpointURLKey) 96 97 if opts.gohanEndpointURL == "" { 98 gohanServiceName := os.Getenv(gohanServiceNameKey) 99 if gohanServiceName == "" { 100 return nil, fmt.Errorf(envVariableNotSetError, gohanServiceNameKey) 101 } 102 opts.gohanServiceName = gohanServiceName 103 104 gohanRegion := os.Getenv(gohanRegionKey) 105 if gohanRegion == "" { 106 return nil, fmt.Errorf(envVariableNotSetError, gohanRegionKey) 107 } 108 opts.gohanRegion = gohanRegion 109 } 110 111 rawCacheSchemas := os.Getenv(cacheSchemasKey) 112 if rawCacheSchemas != "" { 113 cacheSchemas, err := strconv.ParseBool(rawCacheSchemas) 114 if err != nil { 115 return nil, fmt.Errorf(incorrectValueForArgument, cacheSchemasKey, "bool") 116 } 117 opts.cacheSchemas = cacheSchemas 118 } 119 120 rawCacheTimeout := os.Getenv(cacheTimeoutKey) 121 if rawCacheTimeout != "" { 122 cacheTimeout, err := time.ParseDuration(rawCacheTimeout) 123 if err != nil { 124 return nil, fmt.Errorf(incorrectValueForArgument, cacheTimeoutKey, "e.g. 1h20m5s") 125 } 126 opts.cacheTimeout = cacheTimeout 127 } 128 129 cachePath := os.Getenv(cachePathKey) 130 if cachePath != "" { 131 opts.cachePath = cachePath 132 } 133 134 authTokenID := os.Getenv(keystoneTokenIDKey) 135 if authTokenID != "" { 136 opts.authTokenID = authTokenID 137 } 138 139 gohanSchemaURL := os.Getenv(gohanSchemaURLKey) 140 if gohanSchemaURL == "" { 141 return nil, fmt.Errorf(envVariableNotSetError, gohanSchemaURLKey) 142 } 143 opts.gohanSchemaURL = gohanSchemaURL 144 145 outputFormatOpt := os.Getenv(outputFormatEnvKey) 146 if outputFormatOpt != "" { 147 outputFormat, err := findOutputFormat(outputFormatOpt) 148 if err != nil { 149 return nil, err 150 } 151 opts.outputFormat = outputFormat 152 } 153 154 verbosity := os.Getenv(logLevelEnvKey) 155 if verbosity != "" { 156 logLevel, err := parseLogLevel(verbosity) 157 if err != nil { 158 return nil, err 159 } 160 opts.logLevel = logLevel 161 } 162 163 return &opts, nil 164 } 165 166 func findOutputFormat(formatOpt interface{}) (string, error) { 167 for _, format := range outputFormats { 168 if format == formatOpt { 169 return format, nil 170 } 171 } 172 return "", fmt.Errorf(incorrectOutputFormat, outputFormats) 173 } 174 175 func parseLogLevel(verbosityOpt interface{}) (l.Level, error) { 176 for i, logLevel := range logLevels { 177 if fmt.Sprint(i) == verbosityOpt { 178 return logLevel, nil 179 } 180 } 181 return l.WARNING, fmt.Errorf(incorrectVerbosityLevel, 0, len(logLevels)-1) 182 }