Skip to content

Commit 8c9030f

Browse files
committed
read compose.yaml file too
1 parent 58d164d commit 8c9030f

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

src/cmd/cli/color.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type ColorMode string
6+
7+
const (
8+
// ColorNone disables color output.
9+
ColorNone ColorMode = "none"
10+
// ColorAuto enables color output only if the output is connected to a terminal.
11+
ColorAuto ColorMode = "auto"
12+
// ColorAlways enables color output.
13+
ColorAlways ColorMode = "always"
14+
)
15+
16+
var allColorModes = []ColorMode{
17+
ColorNone,
18+
ColorAuto,
19+
ColorAlways,
20+
}
21+
22+
func (c ColorMode) String() string {
23+
return string(c)
24+
}
25+
26+
func (c *ColorMode) Set(value string) error {
27+
for _, colorMode := range allColorModes {
28+
if colorMode.String() == value {
29+
*c = colorMode
30+
return nil
31+
}
32+
}
33+
return fmt.Errorf("color mode not one of %v", allColorModes)
34+
}
35+
36+
func (c ColorMode) Type() string {
37+
return "color-mode"
38+
}

src/cmd/cli/main.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ var rootCmd = &cobra.Command{
6363
}
6464
}
6565

66-
color, _ := cmd.Flags().GetString("color")
67-
switch color {
68-
case "auto":
66+
color := cmd.Flag("color").Value.(*ColorMode)
67+
switch *color {
68+
case ColorAuto:
6969
cli.DoColor = cli.CanColor
70-
case "always":
70+
case ColorAlways:
7171
cli.DoColor = true
72-
case "never":
72+
case ColorNone:
7373
cli.DoColor = false
7474
default:
75-
return fmt.Errorf("invalid color option: %s", color)
75+
panic("invalid color mode")
7676
}
7777

7878
if _, _, err := net.SplitHostPort(server); err != nil {
@@ -575,7 +575,8 @@ var logoutCmd = &cobra.Command{
575575
}
576576

577577
func main() {
578-
rootCmd.PersistentFlags().String("color", "auto", `Colorize output; "auto", "always" or "never"`)
578+
colorMode := ColorAuto
579+
rootCmd.PersistentFlags().Var(&colorMode, "color", `Colorize output; "auto", "always" or "never"`)
579580
rootCmd.PersistentFlags().StringVarP(&server, "cluster", "s", defFabric, "Cluster to connect to")
580581
rootCmd.PersistentFlags().BoolVarP(&cli.DoVerbose, "verbose", "v", false, "Verbose logging")
581582
rootCmd.PersistentFlags().BoolVar(&cli.DoDryRun, "dry-run", false, "Dry run (don't actually change anything)")
@@ -623,7 +624,7 @@ func main() {
623624
rootCmd.AddCommand(secretsCmd)
624625

625626
// Compose Command
626-
composeCmd.PersistentFlags().StringP("file", "f", "docker-compose.yml", "Compose file path")
627+
composeCmd.PersistentFlags().StringP("file", "f", "*compose.y*ml", `Compose file path`)
627628
composeCmd.MarkPersistentFlagFilename("file", "yml", "yaml")
628629
// composeCmd.Flags().Bool("compatibility", false, "Run compose in backward compatibility mode"); TODO: Implement compose option
629630
// composeCmd.Flags().String("env-file", "", "Specify an alternate environment file."); TODO: Implement compose option
@@ -684,7 +685,7 @@ func main() {
684685
var derr *cli.ComposeError
685686
if errors.As(err, &derr) {
686687
compose := "compose"
687-
composeFile := composeCmd.PersistentFlags().Lookup("file")
688+
composeFile := composeCmd.Flag("file")
688689
if composeFile.Changed {
689690
compose += " -f " + composeFile.Value.String()
690691
}

src/pkg/cli/compose.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ func convertPlatform(platform string) pb.Platform {
9595
}
9696

9797
func loadDockerCompose(filePath, projectName string) (*types.Project, error) {
98+
// The default path for a Compose file is compose.yaml (preferred) or compose.yml that is placed in the working directory.
99+
// Compose also supports docker-compose.yaml and docker-compose.yml for backwards compatibility.
100+
if files, _ := filepath.Glob(filePath); len(files) > 1 {
101+
return nil, fmt.Errorf("multiple Compose files found: %q; use -f to specify which one to use", files)
102+
} else if len(files) == 1 {
103+
filePath = files[0]
104+
}
98105
Debug(" - Loading compose file", filePath, "for project", projectName)
99106
// Compose-go uses the logrus logger, so we need to configure it to be more like our own logger
100107
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true, DisableColors: !DoColor, DisableLevelTruncation: true})

0 commit comments

Comments
 (0)