Merge pull request #9247 from ndeloof/plain

use plain text progress when ansi=never is set
This commit is contained in:
Guillaume Lours 2022-03-08 16:55:55 +01:00 committed by GitHub
commit ce944520ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 18 deletions

View File

@ -29,16 +29,17 @@ import (
"github.com/compose-spec/compose-go/types"
dockercli "github.com/docker/cli/cli"
"github.com/docker/cli/cli-plugins/manager"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/utils"
"github.com/morikuni/aec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
"github.com/docker/compose/v2/pkg/progress"
"github.com/docker/compose/v2/pkg/utils"
)
// Command defines a compose CLI command as a func with args
@ -271,6 +272,12 @@ func RootCommand(backend api.Service) *cobra.Command {
logrus.SetLevel(logrus.TraceLevel)
}
formatter.SetANSIMode(ansi)
switch ansi {
case "never":
progress.Mode = progress.ModePlain
case "tty":
progress.Mode = progress.ModeTTY
}
if opts.WorkDir != "" {
if opts.ProjectDir != "" {
return errors.New(`cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead`)

View File

@ -90,28 +90,45 @@ func RunWithStatus(ctx context.Context, pf progressFuncWithStatus) (string, erro
return result, err
}
const (
// ModeAuto detect console capabilities
ModeAuto = "auto"
// ModeTTY use terminal capability for advanced rendering
ModeTTY = "tty"
// ModePlain dump raw events to output
ModePlain = "plain"
)
// Mode define how progress should be rendered, either as ModePlain or ModeTTY
var Mode = ModeAuto
// NewWriter returns a new multi-progress writer
func NewWriter(out console.File) (Writer, error) {
_, isTerminal := term.GetFdInfo(out)
if isTerminal {
con, err := console.ConsoleFromFile(out)
if err != nil {
return nil, err
}
return &ttyWriter{
out: con,
eventIDs: []string{},
events: map[string]Event{},
repeated: false,
done: make(chan bool),
mtx: &sync.Mutex{},
}, nil
if Mode == ModeAuto && isTerminal {
return newTTYWriter(out)
}
if Mode == ModeTTY {
return newTTYWriter(out)
}
return &plainWriter{
out: out,
done: make(chan bool),
}, nil
}
func newTTYWriter(out console.File) (Writer, error) {
con, err := console.ConsoleFromFile(out)
if err != nil {
return nil, err
}
return &ttyWriter{
out: con,
eventIDs: []string{},
events: map[string]Event{},
repeated: false,
done: make(chan bool),
mtx: &sync.Mutex{},
}, nil
}