2019-08-01 14:01:40 -04:00
/ *
Copyright 2017 The Kubernetes Authors .
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 .
* /
package env
import (
"fmt"
"io"
"strings"
)
func ExampleIsEnvironmentArgument_true ( ) {
test := "returns=true"
fmt . Println ( IsEnvironmentArgument ( test ) )
// Output: true
}
func ExampleIsEnvironmentArgument_false ( ) {
test := "returnsfalse"
fmt . Println ( IsEnvironmentArgument ( test ) )
// Output: false
}
func ExampleSplitEnvironmentFromResources ( ) {
args := [ ] string { ` resource ` , "ENV\\=ARG" , ` ONE\=MORE ` , ` DASH- ` }
fmt . Println ( SplitEnvironmentFromResources ( args ) )
// Output: [resource] [ENV\=ARG ONE\=MORE DASH-] true
}
2021-06-14 17:16:54 -04:00
func ExampleParseEnv_good_with_stdin ( ) {
2019-08-01 14:01:40 -04:00
r := strings . NewReader ( "FROM=READER" )
2021-02-06 22:27:44 -05:00
ss := [ ] string { "ENV=VARIABLE" , "ENV.TEST=VARIABLE" , "AND=ANOTHER" , "REMOVE-" , "-" }
2019-08-01 14:01:40 -04:00
fmt . Println ( ParseEnv ( ss , r ) )
// Output:
2021-06-14 17:16:54 -04:00
// [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] true <nil>
}
func ExampleParseEnv_good_with_stdin_and_error ( ) {
r := strings . NewReader ( "FROM=READER" )
ss := [ ] string { "-" , "This not in the key=value format." }
fmt . Println ( ParseEnv ( ss , r ) )
// Output:
// [] [] 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]*')
}
func ExampleParseEnv_good_without_stdin ( ) {
ss := [ ] string { "ENV=VARIABLE" , "ENV.TEST=VARIABLE" , "AND=ANOTHER" , "REMOVE-" }
fmt . Println ( ParseEnv ( ss , nil ) )
// Output:
// [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil}] [REMOVE] false <nil>
2019-08-01 14:01:40 -04:00
}
2021-02-06 22:27:44 -05:00
func ExampleParseEnv_bad_first ( ) {
2019-08-01 14:01:40 -04:00
var r io . Reader
bad := [ ] string { "This not in the key=value format." }
fmt . Println ( ParseEnv ( bad , r ) )
// Output:
2021-06-14 17:16:54 -04:00
// [] [] 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]*')
2021-02-06 22:27:44 -05:00
}
func ExampleParseEnv_bad_second ( ) {
var r io . Reader
bad := [ ] string { ".=VARIABLE" }
fmt . Println ( ParseEnv ( bad , r ) )
// Output:
2021-06-14 17:16:54 -04:00
// [] [] false "." is not a valid key name: must not be '.'
2021-02-06 22:27:44 -05:00
}
func ExampleParseEnv_bad_third ( ) {
var r io . Reader
bad := [ ] string { "..=VARIABLE" }
fmt . Println ( ParseEnv ( bad , r ) )
// Output:
2021-06-14 17:16:54 -04:00
// [] [] false ".." is not a valid key name: must not be '..'
2021-02-06 22:27:44 -05:00
}
func ExampleParseEnv_bad_fourth ( ) {
var r io . Reader
bad := [ ] string { "..ENV=VARIABLE" }
fmt . Println ( ParseEnv ( bad , r ) )
// Output:
2021-06-14 17:16:54 -04:00
// [] [] false "..ENV" is not a valid key name: must not start with '..'
2019-08-01 14:01:40 -04:00
}