Skip to content

Commit

Permalink
support --compose-file - as stdin
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Mariani <marco.mariani@alterway.fr>
  • Loading branch information
mmariani committed Jul 24, 2017
1 parent b175e3a commit 72662e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
36 changes: 27 additions & 9 deletions cli/command/stack/deploy_composefile.go
Expand Up @@ -2,6 +2,7 @@ package stack

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -22,7 +23,7 @@ import (
)

func deployCompose(ctx context.Context, dockerCli command.Cli, opts deployOptions) error {
configDetails, err := getConfigDetails(opts.composefile)
configDetails, err := getConfigDetails(opts.composefile, dockerCli.In())
if err != nil {
return err
}
Expand Down Expand Up @@ -118,16 +119,24 @@ func propertyWarnings(properties map[string]string) string {
return strings.Join(msgs, "\n\n")
}

func getConfigDetails(composefile string) (composetypes.ConfigDetails, error) {
func getConfigDetails(composefile string, stdin io.Reader) (composetypes.ConfigDetails, error) {
var details composetypes.ConfigDetails

absPath, err := filepath.Abs(composefile)
if err != nil {
return details, err
if composefile == "-" {
workingDir, err := os.Getwd()
if err != nil {
return details, err
}
details.WorkingDir = workingDir
} else {
absPath, err := filepath.Abs(composefile)
if err != nil {
return details, err
}
details.WorkingDir = filepath.Dir(absPath)
}
details.WorkingDir = filepath.Dir(absPath)

configFile, err := getConfigFile(composefile)
configFile, err := getConfigFile(composefile, stdin)
if err != nil {
return details, err
}
Expand All @@ -150,15 +159,24 @@ func buildEnvironment(env []string) (map[string]string, error) {
return result, nil
}

func getConfigFile(filename string) (*composetypes.ConfigFile, error) {
bytes, err := ioutil.ReadFile(filename)
func getConfigFile(filename string, stdin io.Reader) (*composetypes.ConfigFile, error) {
var bytes []byte
var err error

if filename == "-" {
bytes, err = ioutil.ReadAll(stdin)
} else {
bytes, err = ioutil.ReadFile(filename)
}
if err != nil {
return nil, err
}

config, err := loader.ParseYAML(bytes)
if err != nil {
return nil, err
}

return &composetypes.ConfigFile{
Filename: filename,
Config: config,
Expand Down
19 changes: 18 additions & 1 deletion cli/command/stack/deploy_composefile_test.go
Expand Up @@ -3,6 +3,7 @@ package stack
import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/docker/cli/cli/internal/test/network"
Expand All @@ -25,13 +26,29 @@ services:
file := tempfile.NewTempFile(t, "test-get-config-details", content)
defer file.Remove()

details, err := getConfigDetails(file.Name())
details, err := getConfigDetails(file.Name(), nil)
require.NoError(t, err)
assert.Equal(t, filepath.Dir(file.Name()), details.WorkingDir)
assert.Len(t, details.ConfigFiles, 1)
assert.Len(t, details.Environment, len(os.Environ()))
}

func TestGetConfigDetailsStdin(t *testing.T) {
content := `
version: "3.0"
services:
foo:
image: alpine:3.5
`
details, err := getConfigDetails("-", strings.NewReader(content))
require.NoError(t, err)
cwd, err := os.Getwd()
require.NoError(t, err)
assert.Equal(t, cwd, details.WorkingDir)
assert.Len(t, details.ConfigFiles, 1)
assert.Len(t, details.Environment, len(os.Environ()))
}

type notFound struct {
error
}
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/commandline/stack_deploy.md
Expand Up @@ -57,6 +57,8 @@ Creating service vossibility_ghollector
Creating service vossibility_lookupd
```

The Compose file can also be provided as standard input with `--compose-file -`.

Only a single Compose file is accepted. If your configuration is split between
multiple Compose files, e.g. a base configuration and environment-specific overrides,
you can combine these by passing them to `docker-compose config` with the `-f` option
Expand Down

0 comments on commit 72662e6

Please sign in to comment.