Prechádzať zdrojové kódy

--env-file: simple line-delimited

match dock functionality, and not try to achieve shell-sourcing compatibility

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
Vincent Batts 11 rokov pred
rodič
commit
ff4ac7441b
2 zmenil súbory, kde vykonal 20 pridanie a 20 odobranie
  1. 14 12
      docs/sources/reference/commandline/cli.rst
  2. 6 8
      opts/envfile.go

+ 14 - 12
docs/sources/reference/commandline/cli.rst

@@ -1296,7 +1296,8 @@ through (i.e. $MYVAR1 from the host is set to $MYVAR1 in the container). All
 three flags, ``-e``, ``--env``  and ``--env-file`` can be repeated.
 three flags, ``-e``, ``--env``  and ``--env-file`` can be repeated.
 
 
 Regardless of the order of these three flags, the ``--env-file`` are processed
 Regardless of the order of these three flags, the ``--env-file`` are processed
-first, and then ``-e``/``--env`` flags. So that they can override VAR as needed.
+first, and then ``-e``/``--env`` flags. This way, the ``-e`` or ``--env`` will
+override variables as needed.
 
 
 .. code-block:: bash
 .. code-block:: bash
 
 
@@ -1306,29 +1307,30 @@ first, and then ``-e``/``--env`` flags. So that they can override VAR as needed.
     TEST_FOO=This is a test
     TEST_FOO=This is a test
 
 
 The ``--env-file`` flag takes a filename as an argument and expects each line
 The ``--env-file`` flag takes a filename as an argument and expects each line
-to be in the VAR=VAL format.  The VAL is Unquoted, so if you need a multi-line
-value, then use `\n` escape characters inside of a double quoted VAL. Single
-quotes are literal. An example of a file passed with ``--env-file``
+to be in the VAR=VAL format, mimicking the argument passed to ``--env``.
+Comment lines need only be prefixed with ``#``
+
+An example of a file passed with ``--env-file``
 
 
 .. code-block:: bash
 .. code-block:: bash
 
 
     $ cat ./env.list
     $ cat ./env.list
     TEST_FOO=BAR
     TEST_FOO=BAR
+
+    # this is a comment
     TEST_APP_DEST_HOST=10.10.0.127
     TEST_APP_DEST_HOST=10.10.0.127
     TEST_APP_DEST_PORT=8888
     TEST_APP_DEST_PORT=8888
-    TEST_SOME_MULTILINE_VAR="this is first line\nthis is second line"
-    TEST_SOME_LITERAL_VAR='this\nwill\nall\nbe\none\nline'
-    $ sudo docker run --env-file ./env.list busybox env
+
+    # pass through this variable from the caller
+    TEST_PASSTHROUGH
+    $ sudo TEST_PASSTHROUGH=howdy docker run --env-file ./env.list busybox env
     HOME=/
     HOME=/
     PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
     PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
-    HOSTNAME=215d54a814bc
+    HOSTNAME=5198e0745561
     TEST_FOO=BAR
     TEST_FOO=BAR
     TEST_APP_DEST_HOST=10.10.0.127
     TEST_APP_DEST_HOST=10.10.0.127
     TEST_APP_DEST_PORT=8888
     TEST_APP_DEST_PORT=8888
-    TEST_SOME_MULTILINE_VAR=this is first line
-    this is second line
-    TEST_SOME_LITERAL_VAR='this\nwill\nall\nbe\none\nline'
-    container=lxc
+    TEST_PASSTHROUGH=howdy
 
 
 
 
 .. code-block:: bash
 .. code-block:: bash

+ 6 - 8
pkg/opts/envfile.go → opts/envfile.go

@@ -4,7 +4,6 @@ import (
 	"bufio"
 	"bufio"
 	"fmt"
 	"fmt"
 	"os"
 	"os"
-	"strconv"
 	"strings"
 	"strings"
 )
 )
 
 
@@ -23,14 +22,13 @@ func ParseEnvFile(filename string) ([]string, error) {
 	for scanner.Scan() {
 	for scanner.Scan() {
 		line := scanner.Text()
 		line := scanner.Text()
 		// line is not empty, and not starting with '#'
 		// line is not empty, and not starting with '#'
-		if len(line) > 0 && !strings.HasPrefix(line, "#") && strings.Contains(line, "=") {
-			data := strings.SplitN(line, "=", 2)
-			key := data[0]
-			val := data[1]
-			if str, err := strconv.Unquote(data[1]); err == nil {
-				val = str
+		if len(line) > 0 && !strings.HasPrefix(line, "#") {
+			if strings.Contains(line, "=") {
+				data := strings.SplitN(line, "=", 2)
+				lines = append(lines, fmt.Sprintf("%s=%s", data[0], data[1]))
+			} else {
+				lines = append(lines, fmt.Sprintf("%s=%s", line, os.Getenv(line)))
 			}
 			}
-			lines = append(lines, fmt.Sprintf("%s=%s", key, val))
 		}
 		}
 	}
 	}
 	return lines, nil
 	return lines, nil