2014-03-11 16:22:58 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ParseEnvFile reads a file with environment variables enumerated by lines
|
2015-09-28 14:26:20 -04:00
|
|
|
//
|
|
|
|
// ``Environment variable names used by the utilities in the Shell and
|
|
|
|
// Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase
|
|
|
|
// letters, digits, and the '_' (underscore) from the characters defined in
|
|
|
|
// Portable Character Set and do not begin with a digit. *But*, other
|
|
|
|
// characters may be permitted by an implementation; applications shall
|
|
|
|
// tolerate the presence of such names.''
|
|
|
|
// -- http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
|
|
|
|
//
|
|
|
|
// As of #16585, it's up to application inside docker to validate or not
|
|
|
|
// environment variables, that's why we just strip leading whitespace and
|
|
|
|
// nothing more.
|
2014-03-11 16:22:58 -04:00
|
|
|
func ParseEnvFile(filename string) ([]string, error) {
|
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
|
|
|
lines := []string{}
|
|
|
|
scanner := bufio.NewScanner(fh)
|
|
|
|
for scanner.Scan() {
|
2015-08-21 16:28:01 -04:00
|
|
|
// trim the line from all leading whitespace first
|
|
|
|
line := strings.TrimLeft(scanner.Text(), whiteSpaces)
|
2014-03-11 16:22:58 -04:00
|
|
|
// line is not empty, and not starting with '#'
|
|
|
|
if len(line) > 0 && !strings.HasPrefix(line, "#") {
|
2015-07-12 04:33:30 -04:00
|
|
|
data := strings.SplitN(line, "=", 2)
|
|
|
|
|
2015-09-28 14:26:20 -04:00
|
|
|
// trim the front of a variable, but nothing else
|
|
|
|
variable := strings.TrimLeft(data[0], whiteSpaces)
|
|
|
|
if strings.ContainsAny(variable, whiteSpaces) {
|
|
|
|
return []string{}, ErrBadEnvVariable{fmt.Sprintf("variable '%s' has white spaces", variable)}
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
2015-09-28 14:26:20 -04:00
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
if len(data) > 1 {
|
2014-03-17 17:11:27 -04:00
|
|
|
|
|
|
|
// pass the value through, no trimming
|
|
|
|
lines = append(lines, fmt.Sprintf("%s=%s", variable, data[1]))
|
2014-03-11 16:22:58 -04:00
|
|
|
} else {
|
2014-03-17 17:11:27 -04:00
|
|
|
// if only a pass-through variable is given, clean it up.
|
|
|
|
lines = append(lines, fmt.Sprintf("%s=%s", strings.TrimSpace(line), os.Getenv(line)))
|
2014-03-11 16:22:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 10:54:20 -04:00
|
|
|
return lines, scanner.Err()
|
2014-03-11 16:22:58 -04:00
|
|
|
}
|
2014-03-17 17:11:27 -04:00
|
|
|
|
|
|
|
var whiteSpaces = " \t"
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
// ErrBadEnvVariable typed error for bad environment variable
|
2014-03-17 17:11:27 -04:00
|
|
|
type ErrBadEnvVariable struct {
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrBadEnvVariable) Error() string {
|
|
|
|
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
|
|
|
|
}
|