1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package env 18 19 import ( 20 "fmt" 21 "io" 22 "strings" 23 ) 24 25 func ExampleIsEnvironmentArgument_true() { 26 test := "returns=true" 27 fmt.Println(IsEnvironmentArgument(test)) 28 // Output: true 29 } 30 31 func ExampleIsEnvironmentArgument_false() { 32 test := "returnsfalse" 33 fmt.Println(IsEnvironmentArgument(test)) 34 // Output: false 35 } 36 37 func ExampleSplitEnvironmentFromResources() { 38 args := []string{`resource`, "ENV\\=ARG", `ONE\=MORE`, `DASH-`} 39 fmt.Println(SplitEnvironmentFromResources(args)) 40 // Output: [resource] [ENV\=ARG ONE\=MORE DASH-] true 41 } 42 43 func ExampleParseEnv_good_with_stdin() { 44 r := strings.NewReader("FROM=READER") 45 ss := []string{"ENV=VARIABLE", "ENV.TEST=VARIABLE", "AND=ANOTHER", "REMOVE-", "-"} 46 fmt.Println(ParseEnv(ss, r)) 47 // Output: 48 // [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] true <nil> 49 } 50 51 func ExampleParseEnv_good_with_stdin_and_error() { 52 r := strings.NewReader("FROM=READER") 53 ss := []string{"-", "This not in the key=value format."} 54 fmt.Println(ParseEnv(ss, r)) 55 // Output: 56 // [] [] true "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*') 57 } 58 59 func ExampleParseEnv_good_without_stdin() { 60 ss := []string{"ENV=VARIABLE", "ENV.TEST=VARIABLE", "AND=ANOTHER", "REMOVE-"} 61 fmt.Println(ParseEnv(ss, nil)) 62 // Output: 63 // [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil}] [REMOVE] false <nil> 64 } 65 66 func ExampleParseEnv_bad_first() { 67 var r io.Reader 68 bad := []string{"This not in the key=value format."} 69 fmt.Println(ParseEnv(bad, r)) 70 // Output: 71 // [] [] false "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*') 72 } 73 74 func ExampleParseEnv_bad_second() { 75 var r io.Reader 76 bad := []string{".=VARIABLE"} 77 fmt.Println(ParseEnv(bad, r)) 78 // Output: 79 // [] [] false "." is not a valid key name: must not be '.' 80 } 81 82 func ExampleParseEnv_bad_third() { 83 var r io.Reader 84 bad := []string{"..=VARIABLE"} 85 fmt.Println(ParseEnv(bad, r)) 86 // Output: 87 // [] [] false ".." is not a valid key name: must not be '..' 88 } 89 90 func ExampleParseEnv_bad_fourth() { 91 var r io.Reader 92 bad := []string{"..ENV=VARIABLE"} 93 fmt.Println(ParseEnv(bad, r)) 94 // Output: 95 // [] [] false "..ENV" is not a valid key name: must not start with '..' 96 } 97