2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-16 09:42:07 +02:00
|
|
|
package progress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-08 16:11:32 +01:00
|
|
|
"io"
|
2020-06-16 09:42:07 +02:00
|
|
|
"sync"
|
|
|
|
|
2023-05-14 12:11:49 +02:00
|
|
|
"github.com/cloudflare/cfssl/log"
|
2023-01-27 16:43:48 +01:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
|
2020-06-17 22:19:08 +02:00
|
|
|
"github.com/containerd/console"
|
|
|
|
"github.com/moby/term"
|
2020-06-18 12:10:36 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-06-16 09:42:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Writer can write multiple progress events
|
|
|
|
type Writer interface {
|
|
|
|
Start(context.Context) error
|
|
|
|
Stop()
|
|
|
|
Event(Event)
|
2022-01-07 19:01:59 +01:00
|
|
|
Events([]Event)
|
2021-03-25 22:21:36 +01:00
|
|
|
TailMsgf(string, ...interface{})
|
2020-06-16 09:42:07 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 17:24:50 +02:00
|
|
|
type writerKey struct{}
|
|
|
|
|
|
|
|
// WithContextWriter adds the writer to the context
|
|
|
|
func WithContextWriter(ctx context.Context, writer Writer) context.Context {
|
|
|
|
return context.WithValue(ctx, writerKey{}, writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContextWriter returns the writer from the context
|
|
|
|
func ContextWriter(ctx context.Context) Writer {
|
2020-09-24 09:39:46 +02:00
|
|
|
s, ok := ctx.Value(writerKey{}).(Writer)
|
|
|
|
if !ok {
|
|
|
|
return &noopWriter{}
|
|
|
|
}
|
2020-06-17 17:24:50 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:29:39 +02:00
|
|
|
type progressFunc func(context.Context) error
|
2020-06-18 12:10:36 +02:00
|
|
|
|
2021-06-01 14:29:39 +02:00
|
|
|
type progressFuncWithStatus func(context.Context) (string, error)
|
|
|
|
|
|
|
|
// Run will run a writer and the progress function in parallel
|
2023-03-08 16:11:32 +01:00
|
|
|
func Run(ctx context.Context, pf progressFunc, out io.Writer) error {
|
2021-06-01 14:29:39 +02:00
|
|
|
_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
|
|
|
|
return "", pf(ctx)
|
2023-05-02 20:15:35 +02:00
|
|
|
}, out, "Running")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunWithTitle(ctx context.Context, pf progressFunc, out io.Writer, progressTitle string) error {
|
|
|
|
_, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
|
|
|
|
return "", pf(ctx)
|
|
|
|
}, out, progressTitle)
|
2021-06-01 14:29:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithStatus will run a writer and the progress function in parallel and return a status
|
2023-05-02 20:15:35 +02:00
|
|
|
func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out io.Writer, progressTitle string) (string, error) {
|
2020-06-18 12:10:36 +02:00
|
|
|
eg, _ := errgroup.WithContext(ctx)
|
2023-05-02 20:15:35 +02:00
|
|
|
w, err := NewWriter(ctx, out, progressTitle)
|
2020-09-14 10:01:59 +02:00
|
|
|
var result string
|
2020-06-18 12:10:36 +02:00
|
|
|
if err != nil {
|
2020-09-14 10:01:59 +02:00
|
|
|
return "", err
|
2020-06-18 12:10:36 +02:00
|
|
|
}
|
|
|
|
eg.Go(func() error {
|
|
|
|
return w.Start(context.Background())
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx = WithContextWriter(ctx, w)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer w.Stop()
|
2020-09-14 10:01:59 +02:00
|
|
|
s, err := pf(ctx)
|
|
|
|
if err == nil {
|
|
|
|
result = s
|
|
|
|
}
|
|
|
|
return err
|
2020-06-18 12:10:36 +02:00
|
|
|
})
|
|
|
|
|
2020-09-14 10:01:59 +02:00
|
|
|
err = eg.Wait()
|
|
|
|
return result, err
|
2020-06-18 12:10:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 12:15:59 +01:00
|
|
|
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
|
|
|
|
|
2020-06-16 09:42:07 +02:00
|
|
|
// NewWriter returns a new multi-progress writer
|
2023-05-02 20:15:35 +02:00
|
|
|
func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer, error) {
|
2020-06-17 22:19:08 +02:00
|
|
|
_, isTerminal := term.GetFdInfo(out)
|
2023-01-27 16:43:48 +01:00
|
|
|
dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
|
|
|
|
if !ok {
|
|
|
|
dryRun = false
|
|
|
|
}
|
2023-05-14 12:11:49 +02:00
|
|
|
f, isConsole := out.(console.File) // see https://github.com/docker/compose/issues/10560
|
|
|
|
if Mode == ModeAuto && isTerminal && isConsole {
|
|
|
|
return newTTYWriter(f, dryRun, progressTitle)
|
2022-03-08 12:15:59 +01:00
|
|
|
}
|
|
|
|
if Mode == ModeTTY {
|
2023-05-14 12:11:49 +02:00
|
|
|
if !isConsole {
|
|
|
|
log.Warning("Terminal is not a POSIX console")
|
|
|
|
} else {
|
|
|
|
return newTTYWriter(f, dryRun, progressTitle)
|
|
|
|
}
|
2020-06-16 09:42:07 +02:00
|
|
|
}
|
2020-06-17 22:19:08 +02:00
|
|
|
return &plainWriter{
|
2023-01-27 16:43:48 +01:00
|
|
|
out: out,
|
|
|
|
done: make(chan bool),
|
|
|
|
dryRun: dryRun,
|
2020-06-17 22:19:08 +02:00
|
|
|
}, nil
|
2020-06-16 09:42:07 +02:00
|
|
|
}
|
2022-03-08 12:15:59 +01:00
|
|
|
|
2023-05-02 20:15:35 +02:00
|
|
|
func newTTYWriter(out console.File, dryRun bool, progressTitle string) (Writer, error) {
|
2022-03-08 12:15:59 +01:00
|
|
|
con, err := console.ConsoleFromFile(out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ttyWriter{
|
2023-05-02 20:15:35 +02:00
|
|
|
out: con,
|
|
|
|
eventIDs: []string{},
|
|
|
|
events: map[string]Event{},
|
|
|
|
repeated: false,
|
|
|
|
done: make(chan bool),
|
|
|
|
mtx: &sync.Mutex{},
|
|
|
|
dryRun: dryRun,
|
|
|
|
progressTitle: progressTitle,
|
2022-03-08 12:15:59 +01:00
|
|
|
}, nil
|
|
|
|
}
|