2016-10-26 04:19:32 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-10-26 04:19:32 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-05-08 13:58:37 -04:00
|
|
|
"sort"
|
2016-12-06 21:57:22 -05:00
|
|
|
"strconv"
|
2016-10-26 04:19:32 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
2017-09-07 12:44:52 -04:00
|
|
|
"github.com/docker/cli/service/logs"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2023-05-10 08:43:18 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2016-12-06 21:57:22 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type logsOptions struct {
|
|
|
|
noResolve bool
|
2016-12-06 21:57:22 -05:00
|
|
|
noTrunc bool
|
2017-03-08 19:28:21 -05:00
|
|
|
noTaskIDs bool
|
2016-10-26 04:19:32 -04:00
|
|
|
follow bool
|
|
|
|
since string
|
|
|
|
timestamps bool
|
|
|
|
tail string
|
2017-05-08 13:58:37 -04:00
|
|
|
details bool
|
|
|
|
raw bool
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
target string
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func newLogsCommand(dockerCli command.Cli) *cobra.Command {
|
2016-10-26 04:19:32 -04:00
|
|
|
var opts logsOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2017-04-06 14:55:54 -04:00
|
|
|
Use: "logs [OPTIONS] SERVICE|TASK",
|
|
|
|
Short: "Fetch the logs of a service or task",
|
2016-10-26 04:19:32 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-03-21 14:35:55 -04:00
|
|
|
opts.target = args[0]
|
2023-09-09 18:27:44 -04:00
|
|
|
return runLogs(cmd.Context(), dockerCli, &opts)
|
2016-10-26 04:19:32 -04:00
|
|
|
},
|
2017-10-25 12:59:32 -04:00
|
|
|
Annotations: map[string]string{"version": "1.29"},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
|
|
},
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-04-06 14:55:54 -04:00
|
|
|
// options specific to service logs
|
2017-03-21 14:35:55 -04:00
|
|
|
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names in output")
|
2016-12-06 21:57:22 -05:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
|
2017-05-08 13:58:37 -04:00
|
|
|
flags.BoolVar(&opts.raw, "raw", false, "Do not neatly format logs")
|
|
|
|
flags.SetAnnotation("raw", "version", []string{"1.30"})
|
2017-03-21 14:35:55 -04:00
|
|
|
flags.BoolVar(&opts.noTaskIDs, "no-task-ids", false, "Do not include task IDs in output")
|
2017-04-06 14:55:54 -04:00
|
|
|
// options identical to container logs
|
2016-10-26 04:19:32 -04:00
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.StringVar(&opts.since, "since", "", `Show logs since timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)`)
|
2016-10-26 04:19:32 -04:00
|
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
|
2017-05-08 13:58:37 -04:00
|
|
|
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
|
|
|
|
flags.SetAnnotation("details", "version", []string{"1.30"})
|
2020-08-06 10:35:27 -04:00
|
|
|
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs")
|
2016-10-26 04:19:32 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runLogs(ctx context.Context, dockerCli command.Cli, opts *logsOptions) error {
|
2023-10-13 14:34:32 -04:00
|
|
|
apiClient := dockerCli.Client()
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
var (
|
|
|
|
maxLength = 1
|
|
|
|
responseBody io.ReadCloser
|
2017-03-20 13:07:04 -04:00
|
|
|
tty bool
|
2017-05-08 13:58:37 -04:00
|
|
|
// logfunc is used to delay the call to logs so that we can do some
|
|
|
|
// processing before we actually get the logs
|
2023-10-13 14:34:32 -04:00
|
|
|
logfunc func(context.Context, string, container.LogsOptions) (io.ReadCloser, error)
|
2017-03-21 14:35:55 -04:00
|
|
|
)
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2023-10-13 14:34:32 -04:00
|
|
|
service, _, err := apiClient.ServiceInspectWithRaw(ctx, opts.target, types.ServiceInspectOptions{})
|
2016-10-26 04:19:32 -04:00
|
|
|
if err != nil {
|
2017-03-21 14:35:55 -04:00
|
|
|
// if it's any error other than service not found, it's Real
|
2023-05-10 08:43:18 -04:00
|
|
|
if !errdefs.IsNotFound(err) {
|
2017-03-21 14:35:55 -04:00
|
|
|
return err
|
|
|
|
}
|
2023-10-13 14:34:32 -04:00
|
|
|
task, _, err := apiClient.TaskInspectWithRaw(ctx, opts.target)
|
2017-04-26 23:31:01 -04:00
|
|
|
if err != nil {
|
2023-05-10 08:43:18 -04:00
|
|
|
if errdefs.IsNotFound(err) {
|
2017-04-26 23:31:01 -04:00
|
|
|
// if the task isn't found, rewrite the error to be clear
|
|
|
|
// that we looked for services AND tasks and found none
|
2017-05-08 13:58:37 -04:00
|
|
|
err = fmt.Errorf("no such task or service: %v", opts.target)
|
2017-04-26 23:31:01 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
tty = task.Spec.ContainerSpec.TTY
|
2017-03-21 14:35:55 -04:00
|
|
|
maxLength = getMaxLength(task.Slot)
|
2017-05-08 13:58:37 -04:00
|
|
|
|
|
|
|
// use the TaskLogs api function
|
2023-10-13 14:34:32 -04:00
|
|
|
logfunc = apiClient.TaskLogs
|
2017-03-21 14:35:55 -04:00
|
|
|
} else {
|
2017-05-08 13:58:37 -04:00
|
|
|
// use ServiceLogs api function
|
2023-10-13 14:34:32 -04:00
|
|
|
logfunc = apiClient.ServiceLogs
|
2017-03-20 13:07:04 -04:00
|
|
|
tty = service.Spec.TaskTemplate.ContainerSpec.TTY
|
2017-03-21 14:35:55 -04:00
|
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
|
|
|
// if replicas are initialized, figure out if we need to pad them
|
|
|
|
replicas := *service.Spec.Mode.Replicated.Replicas
|
|
|
|
maxLength = getMaxLength(int(replicas))
|
|
|
|
}
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
|
|
|
|
// we can't prettify tty logs. tell the user that this is the case.
|
|
|
|
// this is why we assign the logs function to a variable and delay calling
|
|
|
|
// it. we want to check this before we make the call and checking twice in
|
|
|
|
// each branch is even sloppier than this CLI disaster already is
|
|
|
|
if tty && !opts.raw {
|
|
|
|
return errors.New("tty service logs only supported with --raw")
|
|
|
|
}
|
|
|
|
|
|
|
|
// now get the logs
|
2023-10-13 14:34:32 -04:00
|
|
|
responseBody, err = logfunc(ctx, opts.target, container.LogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: opts.since,
|
|
|
|
Timestamps: opts.timestamps,
|
|
|
|
Follow: opts.follow,
|
|
|
|
Tail: opts.tail,
|
|
|
|
// get the details if we request it OR if we're not doing raw mode
|
|
|
|
// (we need them for the context to pretty print)
|
|
|
|
Details: opts.details || !opts.raw,
|
|
|
|
})
|
2017-05-08 13:58:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-26 04:19:32 -04:00
|
|
|
defer responseBody.Close()
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// tty logs get straight copied. they're not muxed with stdcopy
|
2017-03-20 13:07:04 -04:00
|
|
|
if tty {
|
|
|
|
_, err = io.Copy(dockerCli.Out(), responseBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// otherwise, logs are multiplexed. if we're doing pretty printing, also
|
|
|
|
// create a task formatter.
|
|
|
|
var stdout, stderr io.Writer
|
|
|
|
stdout = dockerCli.Out()
|
|
|
|
stderr = dockerCli.Err()
|
|
|
|
if !opts.raw {
|
2023-10-13 14:34:32 -04:00
|
|
|
taskFormatter := newTaskFormatter(apiClient, opts, maxLength)
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
stdout = &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: stdout}
|
|
|
|
stderr = &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: stderr}
|
|
|
|
}
|
2016-10-26 04:19:32 -04:00
|
|
|
|
|
|
|
_, err = stdcopy.StdCopy(stdout, stderr, responseBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
// getMaxLength gets the maximum length of the number in base 10
|
|
|
|
func getMaxLength(i int) int {
|
2017-06-09 17:42:16 -04:00
|
|
|
return len(strconv.Itoa(i))
|
2017-03-21 14:35:55 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
type taskFormatter struct {
|
|
|
|
client client.APIClient
|
|
|
|
opts *logsOptions
|
|
|
|
padding int
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
r *idresolver.IDResolver
|
|
|
|
// cache saves a pre-cooked logContext formatted string based on a
|
|
|
|
// logcontext object, so we don't have to resolve names every time
|
2016-12-06 21:57:22 -05:00
|
|
|
cache map[logContext]string
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func newTaskFormatter(apiClient client.APIClient, opts *logsOptions, padding int) *taskFormatter {
|
2016-12-06 21:57:22 -05:00
|
|
|
return &taskFormatter{
|
2023-11-20 11:38:50 -05:00
|
|
|
client: apiClient,
|
2016-12-06 21:57:22 -05:00
|
|
|
opts: opts,
|
|
|
|
padding: padding,
|
2023-11-20 11:38:50 -05:00
|
|
|
r: idresolver.New(apiClient, opts.noResolve),
|
2016-12-06 21:57:22 -05:00
|
|
|
cache: make(map[logContext]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *taskFormatter) format(ctx context.Context, logCtx logContext) (string, error) {
|
|
|
|
if cached, ok := f.cache[logCtx]; ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeName, err := f.r.Resolve(ctx, swarm.Node{}, logCtx.nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName, err := f.r.Resolve(ctx, swarm.Service{}, logCtx.serviceID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
task, _, err := f.client.TaskInspectWithRaw(ctx, logCtx.taskID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
taskName := fmt.Sprintf("%s.%d", serviceName, task.Slot)
|
2017-03-08 19:28:21 -05:00
|
|
|
if !f.opts.noTaskIDs {
|
2016-12-06 21:57:22 -05:00
|
|
|
if f.opts.noTrunc {
|
linting: fmt.Sprintf can be replaced with string concatenation (perfsprint)
cli/registry/client/endpoint.go:128:34: fmt.Sprintf can be replaced with string concatenation (perfsprint)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", th.token))
^
cli/command/telemetry_docker.go:88:14: fmt.Sprintf can be replaced with string concatenation (perfsprint)
endpoint = fmt.Sprintf("unix://%s", path.Join(u.Host, u.Path))
^
cli/command/cli_test.go:195:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
opts := &flags.ClientOptions{Hosts: []string{fmt.Sprintf("unix://%s", socket)}}
^
cli/command/registry_test.go:59:24: fmt.Sprintf can be replaced with string concatenation (perfsprint)
inputServerAddress: fmt.Sprintf("https://%s", testAuthConfigs[1].ServerAddress),
^
cli/command/container/opts_test.go:338:35: fmt.Sprintf can be replaced with string concatenation (perfsprint)
if config, _, _ := mustParse(t, fmt.Sprintf("--hostname=%s", hostname)); config.Hostname != expectedHostname {
^
cli/command/context/options.go:79:24: fmt.Sprintf can be replaced with string concatenation (perfsprint)
errs = append(errs, fmt.Sprintf("%s: unrecognized config key", k))
^
cli/command/image/build.go:461:68: fmt.Sprintf can be replaced with string concatenation (perfsprint)
line = dockerfileFromLinePattern.ReplaceAllLiteralString(line, fmt.Sprintf("FROM %s", reference.FamiliarString(trustedRef)))
^
cli/command/image/remove_test.go:21:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("Error: No such image: %s", n.imageID)
^
cli/command/image/build/context.go:229:102: fmt.Sprintf can be replaced with string concatenation (perfsprint)
progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
^
cli/command/service/logs.go:215:16: fmt.Sprintf can be replaced with string concatenation (perfsprint)
taskName += fmt.Sprintf(".%s", task.ID)
^
cli/command/service/logs.go:217:16: fmt.Sprintf can be replaced with string concatenation (perfsprint)
taskName += fmt.Sprintf(".%s", stringid.TruncateID(task.ID))
^
cli/command/service/progress/progress_test.go:877:18: fmt.Sprintf can be replaced with string concatenation (perfsprint)
ID: fmt.Sprintf("task%s", nodeID),
^
cli/command/stack/swarm/remove.go:61:24: fmt.Sprintf can be replaced with string concatenation (perfsprint)
errs = append(errs, fmt.Sprintf("Failed to remove some resources from stack: %s", namespace))
^
cli/command/swarm/ipnet_slice_test.go:32:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
arg := fmt.Sprintf("--cidrs=%s", strings.Join(vals, ","))
^
cli/command/swarm/ipnet_slice_test.go:137:30: fmt.Sprintf can be replaced with string concatenation (perfsprint)
if err := f.Parse([]string{fmt.Sprintf("--cidrs=%s", strings.Join(test.FlagArg, ","))}); err != nil {
^
cli/compose/schema/schema.go:105:11: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("must be a %s", humanReadableType(expectedType))
^
cli/manifest/store/store.go:165:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("No such manifest: %s", n.object)
^
e2e/image/push_test.go:340:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_ROOT_PASSPHRASE=%s", pwd),
^
e2e/image/push_test.go:341:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_TARGETS_PASSPHRASE=%s", pwd),
^
e2e/image/push_test.go:342:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_SNAPSHOT_PASSPHRASE=%s", pwd),
^
e2e/image/push_test.go:343:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_DELEGATION_PASSPHRASE=%s", pwd),
^
e2e/plugin/trust_test.go:23:16: fmt.Sprintf can be replaced with string concatenation (perfsprint)
pluginName := fmt.Sprintf("%s/plugin-content-trust", registryPrefix)
^
e2e/plugin/trust_test.go:53:8: fmt.Sprintf can be replaced with string concatenation (perfsprint)
Out: fmt.Sprintf("Installed plugin %s", pluginName),
^
e2e/trust/revoke_test.go:62:57: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.RunCommand("docker", "tag", fixtures.AlpineImage, fmt.Sprintf("%s:v1", revokeRepo)).Assert(t, icmd.Success)
^
e2e/trust/revoke_test.go:64:49: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.Command("docker", "-D", "trust", "sign", fmt.Sprintf("%s:v1", revokeRepo)),
^
e2e/trust/revoke_test.go:68:58: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.RunCommand("docker", "tag", fixtures.BusyboxImage, fmt.Sprintf("%s:v2", revokeRepo)).Assert(t, icmd.Success)
^
e2e/trust/revoke_test.go:70:49: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.Command("docker", "-D", "trust", "sign", fmt.Sprintf("%s:v2", revokeRepo)),
^
e2e/trust/sign_test.go:36:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.AlpineSha)))
^
e2e/trust/sign_test.go:53:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.BusyboxSha)))
^
e2e/trust/sign_test.go:65:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.AlpineSha)))
^
opts/file.go:21:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
^
opts/hosts_test.go:26:31: fmt.Sprintf can be replaced with string concatenation (perfsprint)
"tcp://host:": fmt.Sprintf("tcp://host:%s", defaultHTTPPort),
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-06-10 15:07:37 -04:00
|
|
|
taskName += "." + task.ID
|
2016-12-06 21:57:22 -05:00
|
|
|
} else {
|
linting: fmt.Sprintf can be replaced with string concatenation (perfsprint)
cli/registry/client/endpoint.go:128:34: fmt.Sprintf can be replaced with string concatenation (perfsprint)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", th.token))
^
cli/command/telemetry_docker.go:88:14: fmt.Sprintf can be replaced with string concatenation (perfsprint)
endpoint = fmt.Sprintf("unix://%s", path.Join(u.Host, u.Path))
^
cli/command/cli_test.go:195:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
opts := &flags.ClientOptions{Hosts: []string{fmt.Sprintf("unix://%s", socket)}}
^
cli/command/registry_test.go:59:24: fmt.Sprintf can be replaced with string concatenation (perfsprint)
inputServerAddress: fmt.Sprintf("https://%s", testAuthConfigs[1].ServerAddress),
^
cli/command/container/opts_test.go:338:35: fmt.Sprintf can be replaced with string concatenation (perfsprint)
if config, _, _ := mustParse(t, fmt.Sprintf("--hostname=%s", hostname)); config.Hostname != expectedHostname {
^
cli/command/context/options.go:79:24: fmt.Sprintf can be replaced with string concatenation (perfsprint)
errs = append(errs, fmt.Sprintf("%s: unrecognized config key", k))
^
cli/command/image/build.go:461:68: fmt.Sprintf can be replaced with string concatenation (perfsprint)
line = dockerfileFromLinePattern.ReplaceAllLiteralString(line, fmt.Sprintf("FROM %s", reference.FamiliarString(trustedRef)))
^
cli/command/image/remove_test.go:21:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("Error: No such image: %s", n.imageID)
^
cli/command/image/build/context.go:229:102: fmt.Sprintf can be replaced with string concatenation (perfsprint)
progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
^
cli/command/service/logs.go:215:16: fmt.Sprintf can be replaced with string concatenation (perfsprint)
taskName += fmt.Sprintf(".%s", task.ID)
^
cli/command/service/logs.go:217:16: fmt.Sprintf can be replaced with string concatenation (perfsprint)
taskName += fmt.Sprintf(".%s", stringid.TruncateID(task.ID))
^
cli/command/service/progress/progress_test.go:877:18: fmt.Sprintf can be replaced with string concatenation (perfsprint)
ID: fmt.Sprintf("task%s", nodeID),
^
cli/command/stack/swarm/remove.go:61:24: fmt.Sprintf can be replaced with string concatenation (perfsprint)
errs = append(errs, fmt.Sprintf("Failed to remove some resources from stack: %s", namespace))
^
cli/command/swarm/ipnet_slice_test.go:32:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
arg := fmt.Sprintf("--cidrs=%s", strings.Join(vals, ","))
^
cli/command/swarm/ipnet_slice_test.go:137:30: fmt.Sprintf can be replaced with string concatenation (perfsprint)
if err := f.Parse([]string{fmt.Sprintf("--cidrs=%s", strings.Join(test.FlagArg, ","))}); err != nil {
^
cli/compose/schema/schema.go:105:11: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("must be a %s", humanReadableType(expectedType))
^
cli/manifest/store/store.go:165:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("No such manifest: %s", n.object)
^
e2e/image/push_test.go:340:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_ROOT_PASSPHRASE=%s", pwd),
^
e2e/image/push_test.go:341:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_TARGETS_PASSPHRASE=%s", pwd),
^
e2e/image/push_test.go:342:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_SNAPSHOT_PASSPHRASE=%s", pwd),
^
e2e/image/push_test.go:343:4: fmt.Sprintf can be replaced with string concatenation (perfsprint)
fmt.Sprintf("NOTARY_DELEGATION_PASSPHRASE=%s", pwd),
^
e2e/plugin/trust_test.go:23:16: fmt.Sprintf can be replaced with string concatenation (perfsprint)
pluginName := fmt.Sprintf("%s/plugin-content-trust", registryPrefix)
^
e2e/plugin/trust_test.go:53:8: fmt.Sprintf can be replaced with string concatenation (perfsprint)
Out: fmt.Sprintf("Installed plugin %s", pluginName),
^
e2e/trust/revoke_test.go:62:57: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.RunCommand("docker", "tag", fixtures.AlpineImage, fmt.Sprintf("%s:v1", revokeRepo)).Assert(t, icmd.Success)
^
e2e/trust/revoke_test.go:64:49: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.Command("docker", "-D", "trust", "sign", fmt.Sprintf("%s:v1", revokeRepo)),
^
e2e/trust/revoke_test.go:68:58: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.RunCommand("docker", "tag", fixtures.BusyboxImage, fmt.Sprintf("%s:v2", revokeRepo)).Assert(t, icmd.Success)
^
e2e/trust/revoke_test.go:70:49: fmt.Sprintf can be replaced with string concatenation (perfsprint)
icmd.Command("docker", "-D", "trust", "sign", fmt.Sprintf("%s:v2", revokeRepo)),
^
e2e/trust/sign_test.go:36:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.AlpineSha)))
^
e2e/trust/sign_test.go:53:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.BusyboxSha)))
^
e2e/trust/sign_test.go:65:47: fmt.Sprintf can be replaced with string concatenation (perfsprint)
assert.Check(t, is.Contains(result.Stdout(), fmt.Sprintf("v1: digest: sha256:%s", fixtures.AlpineSha)))
^
opts/file.go:21:9: fmt.Sprintf can be replaced with string concatenation (perfsprint)
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
^
opts/hosts_test.go:26:31: fmt.Sprintf can be replaced with string concatenation (perfsprint)
"tcp://host:": fmt.Sprintf("tcp://host:%s", defaultHTTPPort),
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-06-10 15:07:37 -04:00
|
|
|
taskName += "." + stringid.TruncateID(task.ID)
|
2016-12-06 21:57:22 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-21 14:35:55 -04:00
|
|
|
|
2017-05-16 15:27:31 -04:00
|
|
|
paddingCount := f.padding - getMaxLength(task.Slot)
|
|
|
|
padding := ""
|
|
|
|
if paddingCount > 0 {
|
|
|
|
padding = strings.Repeat(" ", paddingCount)
|
|
|
|
}
|
|
|
|
formatted := taskName + "@" + nodeName + padding
|
2016-12-06 21:57:22 -05:00
|
|
|
f.cache[logCtx] = formatted
|
|
|
|
return formatted, nil
|
|
|
|
}
|
|
|
|
|
2016-10-26 04:19:32 -04:00
|
|
|
type logWriter struct {
|
|
|
|
ctx context.Context
|
|
|
|
opts *logsOptions
|
2016-12-06 21:57:22 -05:00
|
|
|
f *taskFormatter
|
2016-10-26 04:19:32 -04:00
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *logWriter) Write(buf []byte) (int, error) {
|
2017-05-08 13:58:37 -04:00
|
|
|
// this works but ONLY because stdcopy calls write a whole line at a time.
|
|
|
|
// if this ends up horribly broken or panics, check to see if stdcopy has
|
2017-05-21 21:39:06 -04:00
|
|
|
// reneged on that assumption. (@god forgive me)
|
2017-05-08 13:58:37 -04:00
|
|
|
// also this only works because the logs format is, like, barely parsable.
|
|
|
|
// if something changes in the logs format, this is gonna break
|
|
|
|
|
|
|
|
// there should always be at least 2 parts: details and message. if there
|
|
|
|
// is no timestamp, details will be first (index 0) when we split on
|
|
|
|
// spaces. if there is a timestamp, details will be 2nd (`index 1)
|
|
|
|
detailsIndex := 0
|
2016-10-26 04:19:32 -04:00
|
|
|
numParts := 2
|
|
|
|
if lw.opts.timestamps {
|
2017-05-08 13:58:37 -04:00
|
|
|
detailsIndex++
|
2016-10-26 04:19:32 -04:00
|
|
|
numParts++
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// break up the log line into parts.
|
2016-10-26 04:19:32 -04:00
|
|
|
parts := bytes.SplitN(buf, []byte(" "), numParts)
|
|
|
|
if len(parts) != numParts {
|
2017-03-09 13:23:45 -05:00
|
|
|
return 0, errors.Errorf("invalid context in log message: %v", string(buf))
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
// parse the details out
|
2017-09-07 12:44:52 -04:00
|
|
|
details, err := logs.ParseLogDetails(string(parts[detailsIndex]))
|
2017-05-08 13:58:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// and then create a context from the details
|
|
|
|
// this removes the context-specific details from the details map, so we
|
|
|
|
// can more easily print the details later
|
|
|
|
logCtx, err := lw.parseContext(details)
|
2016-10-26 04:19:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := []byte{}
|
2017-05-08 13:58:37 -04:00
|
|
|
// if we included timestamps, add them to the front
|
|
|
|
if lw.opts.timestamps {
|
|
|
|
output = append(output, parts[0]...)
|
|
|
|
output = append(output, ' ')
|
|
|
|
}
|
|
|
|
// add the context, nice and formatted
|
|
|
|
formatted, err := lw.f.format(lw.ctx, logCtx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
output = append(output, []byte(formatted+" | ")...)
|
|
|
|
// if the user asked for details, add them to be log message
|
|
|
|
if lw.opts.details {
|
|
|
|
// ugh i hate this it's basically a dupe of api/server/httputils/write_log_stream.go:stringAttrs()
|
|
|
|
// ok but we're gonna do it a bit different
|
|
|
|
|
|
|
|
// there are optimizations that can be made here. for starters, i'd
|
|
|
|
// suggest caching the details keys. then, we can maybe draw maps and
|
|
|
|
// slices from a pool to avoid alloc overhead on them. idk if it's
|
|
|
|
// worth the time yet.
|
|
|
|
|
|
|
|
// first we need a slice
|
|
|
|
d := make([]string, 0, len(details))
|
|
|
|
// then let's add all the pairs
|
|
|
|
for k := range details {
|
|
|
|
d = append(d, k+"="+details[k])
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
// then sort em
|
|
|
|
sort.Strings(d)
|
|
|
|
// then join and append
|
|
|
|
output = append(output, []byte(strings.Join(d, ","))...)
|
|
|
|
output = append(output, ' ')
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
|
|
|
|
// add the log message itself, finally
|
|
|
|
output = append(output, parts[detailsIndex+1]...)
|
|
|
|
|
2016-10-26 04:19:32 -04:00
|
|
|
_, err = lw.w.Write(output)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// parseContext returns a log context and REMOVES the context from the details map
|
|
|
|
func (lw *logWriter) parseContext(details map[string]string) (logContext, error) {
|
|
|
|
nodeID, ok := details["com.docker.swarm.node.id"]
|
2016-10-26 04:19:32 -04:00
|
|
|
if !ok {
|
2017-05-08 13:58:37 -04:00
|
|
|
return logContext{}, errors.Errorf("missing node id in details: %v", details)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
delete(details, "com.docker.swarm.node.id")
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
serviceID, ok := details["com.docker.swarm.service.id"]
|
2016-10-26 04:19:32 -04:00
|
|
|
if !ok {
|
2017-05-08 13:58:37 -04:00
|
|
|
return logContext{}, errors.Errorf("missing service id in details: %v", details)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
delete(details, "com.docker.swarm.service.id")
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
taskID, ok := details["com.docker.swarm.task.id"]
|
2016-12-06 21:57:22 -05:00
|
|
|
if !ok {
|
2017-05-08 13:58:37 -04:00
|
|
|
return logContext{}, errors.Errorf("missing task id in details: %s", details)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
delete(details, "com.docker.swarm.task.id")
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
return logContext{
|
|
|
|
nodeID: nodeID,
|
|
|
|
serviceID: serviceID,
|
|
|
|
taskID: taskID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type logContext struct {
|
|
|
|
nodeID string
|
|
|
|
serviceID string
|
|
|
|
taskID string
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|