Skip to content

Commit

Permalink
feature: cli supports --env-file
Browse files Browse the repository at this point in the history
Signed-off-by: Lang Chi <21860405@zju.edu.cn>
  • Loading branch information
lang710 committed May 27, 2019
1 parent 7235f82 commit c5a6e8b
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/common_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {
flagSet.BoolVar(&c.enableLxcfs, "enableLxcfs", false, "Enable lxcfs for the container, only effective when enable-lxcfs switched on in Pouchd")
flagSet.StringVar(&c.entrypoint, "entrypoint", "", "Overwrite the default ENTRYPOINT of the image")
flagSet.StringArrayVarP(&c.env, "env", "e", nil, "Set environment variables for container('--env A=' means setting env A to empty, '--env B' means removing env B from container env inherited from image)")
flagSet.StringArrayVar(&c.env, "env-file", nil, "Read in a file of environment variables")
flagSet.StringVar(&c.hostname, "hostname", "", "Set container's hostname")
flagSet.BoolVar(&c.disableNetworkFiles, "disable-network-files", false, "Disable the generation of network files(/etc/hostname, /etc/hosts and /etc/resolv.conf) for container. If true, no network files will be generated. Default false")

Expand Down
1 change: 1 addition & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type container struct {
volumesFrom []string
runtime string
env []string
envfile []string
entrypoint string
workdir string
user string
Expand Down
6 changes: 6 additions & 0 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (cc *CreateCommand) runCreate(args []string) error {
}
config.ContainerConfig.OpenStdin = cc.openstdin

// collect all the environment variables for the container
config.Env, err = readKVStrings(cc.envfile, cc.env)
if err != nil {
return nil
}

config.Image = args[0]
if len(args) > 1 {
config.Cmd = args[1:]
Expand Down
73 changes: 73 additions & 0 deletions cli/envfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bufio"
"fmt"
"os"
"strings"
)

// reads a file of line terminated key=value pairs, and overrides any keys
// present in the file with additional pairs specified in the override parameter
func readKVStrings(files []string, override []string) ([]string, error) {
envVariables := []string{}
for _, ef := range files {
parsedVars, err := ParseEnvFile(ef)
if err != nil {
return nil, err
}
envVariables = append(envVariables, parsedVars...)
}
// parse the '-e' and '--env' after, to allow override
envVariables = append(envVariables, override...)

return envVariables, nil
}

// ParseEnvFile reads a file with environment variables enumerated by lines
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() {
// trim the line from all leading whitespace first
line := strings.TrimLeft(scanner.Text(), whiteSpaces)
// line is not empty, and not starting with '#'
if len(line) > 0 && !strings.HasPrefix(line, "#") {
data := strings.SplitN(line, "=", 2)

// 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)}
}

if len(data) > 1 {

// pass the value through, no trimming
lines = append(lines, fmt.Sprintf("%s=%s", variable, data[1]))
} else {
// if only a pass-through variable is given, clean it up.
lines = append(lines, fmt.Sprintf("%s=%s", strings.TrimSpace(line), os.Getenv(line)))
}
}
}
return lines, scanner.Err()
}

var whiteSpaces = " \t"

// ErrBadEnvVariable typed error for bad environment variable
type ErrBadEnvVariable struct {
msg string
}

// Error implements error interface.
func (e ErrBadEnvVariable) Error() string {
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
}
56 changes: 56 additions & 0 deletions cli/envfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"io/ioutil"
"os"
"reflect"
"testing"
)

func tmpFileWithContent(content string, t *testing.T) string {
tmpFile, err := ioutil.TempFile("", "envfile-test")
if err != nil {
t.Fatal(err)
}
defer tmpFile.Close()

tmpFile.WriteString(content)
return tmpFile.Name()
}

// Test ParseEnvFile for a file with a few well formatted lines
func TestParseEnvFileGoodFile(t *testing.T) {
content := `foo=bar
baz=quux
# comment
_foobar=foobaz
with.dots=working
and_underscore=working too
`
// Adding a newline + a line with pure whitespace.
// This is being done like this instead of the block above
// because it's common for editors to trim trailing whitespace
// from lines, which becomes annoying since that's the
// exact thing we need to test.
content += "\n \t "
tmpFile := tmpFileWithContent(content, t)
defer os.Remove(tmpFile)

lines, err := ParseEnvFile(tmpFile)
if err != nil {
t.Fatal(err)
}

expectedLines := []string{
"foo=bar",
"baz=quux",
"_foobar=foobaz",
"with.dots=working",
"and_underscore=working too",
}

if !reflect.DeepEqual(lines, expectedLines) {
t.Fatal("lines not equal to expected_lines")
}
}
6 changes: 6 additions & 0 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func (rc *RunCommand) runRun(args []string) error {
return fmt.Errorf("failed to run container: %v", err)
}

// collect all the environment variables for the container
config.Env, err = readKVStrings(rc.envfile, rc.env)
if err != nil {
return nil
}

config.Image = args[0]
if len(args) > 1 {
config.Cmd = args[1:]
Expand Down

0 comments on commit c5a6e8b

Please sign in to comment.