diff --git a/main.go b/main.go index be1e3dd7..9c05b7c8 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ var ( configFlag = false debuggingFlag = false + composeFiles []string ) func main() { @@ -33,6 +34,7 @@ func main() { flaggy.Bool(&configFlag, "c", "config", "Print the current default config") flaggy.Bool(&debuggingFlag, "d", "debug", "a boolean") + flaggy.StringSlice(&composeFiles, "f", "file", "Specify alternate compose files") flaggy.SetVersion(fmt.Sprintf("commit=%s, build date=%s, build source=%s, version=%s, os=%s, arch=%s\n", commit, date, buildSource, version, runtime.GOOS, runtime.GOARCH)) flaggy.Parse() @@ -44,7 +46,7 @@ func main() { os.Exit(0) } - appConfig, err := config.NewAppConfig("lazydocker", version, commit, date, buildSource, debuggingFlag) + appConfig, err := config.NewAppConfig("lazydocker", version, commit, date, buildSource, debuggingFlag, composeFiles) if err != nil { log.Fatal(err.Error()) } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index cfaa7194..18243b9a 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -16,6 +16,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "time" yaml "github.com/jesseduffield/yaml" @@ -364,7 +365,7 @@ type AppConfig struct { } // NewAppConfig makes a new app config -func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) { +func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool, composeFiles []string) (*AppConfig, error) { configDir, err := findOrCreateConfigDir(name) if err != nil { return nil, err @@ -375,6 +376,11 @@ func NewAppConfig(name, version, commit, date string, buildSource string, debugg return nil, err } + // Pass compose files as individual -f flags to docker-compose + if len(composeFiles) > 0 { + userConfig.CommandTemplates.DockerCompose += " -f " + strings.Join(composeFiles, " -f ") + } + appConfig := &AppConfig{ Name: name, Version: version, diff --git a/pkg/config/app_config_test.go b/pkg/config/app_config_test.go new file mode 100644 index 00000000..bae43e3a --- /dev/null +++ b/pkg/config/app_config_test.go @@ -0,0 +1,50 @@ +package config + +import ( + "testing" +) + +func TestDockerComposeCommandNoFiles(t *testing.T) { + composeFiles := []string{} + conf, err := NewAppConfig("name", "version", "commit", "date", "buildSource", false, composeFiles) + + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + actual := conf.UserConfig.CommandTemplates.DockerCompose + expected := "docker-compose" + if actual != expected { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +func TestDockerComposeCommandSingleFile(t *testing.T) { + composeFiles := []string{"one.yml"} + conf, err := NewAppConfig("name", "version", "commit", "date", "buildSource", false, composeFiles) + + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + actual := conf.UserConfig.CommandTemplates.DockerCompose + expected := "docker-compose -f one.yml" + if actual != expected { + t.Fatalf("Expected %s but got %s", expected, actual) + } +} + +func TestDockerComposeCommandMultipleFiles(t *testing.T) { + composeFiles := []string{"one.yml", "two.yml", "three.yml"} + conf, err := NewAppConfig("name", "version", "commit", "date", "buildSource", false, composeFiles) + + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + actual := conf.UserConfig.CommandTemplates.DockerCompose + expected := "docker-compose -f one.yml -f two.yml -f three.yml" + if actual != expected { + t.Fatalf("Expected %s but got %s", expected, actual) + } +}