remove all references to cli from compose.v2 cmd package

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-08-31 15:41:20 +02:00
parent 52df801287
commit fcb91096b8
No known key found for this signature in database
GPG key ID: 9858809D6F8F6E7E
30 changed files with 71 additions and 65 deletions

View file

@ -23,12 +23,13 @@ import (
"sort"
"strings"
formatter2 "github.com/docker/compose-cli/cmd/formatter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
apicontext "github.com/docker/compose-cli/api/context"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/cli/mobycli"
)
@ -68,7 +69,7 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
return err
}
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateLegacyJSON {
if format != "" && format != formatter2.JSON && format != formatter2.PRETTY && format != formatter2.TemplateLegacyJSON {
mobycli.Exec(cmd.Root())
return nil
}
@ -91,15 +92,15 @@ func runList(cmd *cobra.Command, opts lsOpts) error {
return nil
}
if opts.json || format == formatter.JSON {
opts.format = formatter.JSON
if opts.json || format == formatter2.JSON {
opts.format = formatter2.JSON
}
if format == formatter.TemplateLegacyJSON {
opts.format = formatter.TemplateLegacyJSON
if format == formatter2.TemplateLegacyJSON {
opts.format = formatter2.TemplateLegacyJSON
}
view := viewFromContextList(contexts, currentContext)
return formatter.Print(view, opts.format, os.Stdout,
return formatter2.Print(view, opts.format, os.Stdout,
func(w io.Writer) {
for _, c := range view {
contextName := c.Name

View file

@ -20,12 +20,13 @@ import (
"errors"
"fmt"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
apicontext "github.com/docker/compose-cli/api/context"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/cli/formatter"
)
type removeOpts struct {

View file

@ -20,6 +20,8 @@ import (
"context"
"fmt"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/Azure/go-autorest/autorest/to"
"github.com/compose-spec/compose-go/types"
"github.com/pkg/errors"
@ -27,7 +29,6 @@ import (
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/containers"
"github.com/docker/compose-cli/cli/formatter"
)
// InspectCommand inspects into containers

View file

@ -20,12 +20,13 @@ import (
"context"
"fmt"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/pkg/api"
)

View file

@ -23,12 +23,13 @@ import (
"os"
"strings"
formatter3 "github.com/docker/compose-cli/cmd/formatter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/containers"
formatter2 "github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/utils/formatter"
)
@ -89,11 +90,11 @@ func runPs(ctx context.Context, opts psOpts) error {
}
if opts.json {
opts.format = formatter2.JSON
opts.format = formatter3.JSON
}
view := viewFromContainerList(containerList)
return formatter2.Print(view, opts.format, os.Stdout, func(w io.Writer) {
return formatter3.Print(view, opts.format, os.Stdout, func(w io.Writer) {
for _, c := range view {
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", c.ID, c.Image, c.Command, c.Status,
strings.Join(c.Ports, ", "))

View file

@ -20,13 +20,14 @@ import (
"context"
"fmt"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/containers"
"github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/pkg/api"
)

View file

@ -22,11 +22,12 @@ import (
"io/ioutil"
"os"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/secrets"
"github.com/docker/compose-cli/cli/formatter"
)
// SecretCommand manage secrets

View file

@ -20,12 +20,13 @@ import (
"context"
"fmt"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/pkg/api"
)

View file

@ -20,12 +20,13 @@ import (
"context"
"fmt"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/pkg/api"
)

View file

@ -21,10 +21,12 @@ import (
"os"
"strings"
"github.com/docker/compose-cli/cmd/formatter"
"github.com/spf13/cobra"
"github.com/docker/cli/cli"
"github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/cli/mobycli"
"github.com/docker/compose-cli/internal"
)

View file

@ -20,11 +20,11 @@ import (
"context"
"fmt"
format "github.com/docker/compose-cli/cmd/formatter"
"github.com/docker/compose-cli/aci"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/cli/formatter"
format "github.com/docker/compose-cli/cli/formatter"
"github.com/docker/compose-cli/ecs"
"github.com/docker/compose-cli/pkg/progress"
@ -125,7 +125,7 @@ func rmVolume() *cobra.Command {
}
fmt.Println(id)
}
formatter.SetMultiErrorFormat(errs)
format.SetMultiErrorFormat(errs)
return errs.ErrorOrNil()
},
}

View file

@ -21,11 +21,12 @@ import (
"io"
"os"
formatter2 "github.com/docker/compose-cli/cmd/formatter"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/volumes"
"github.com/docker/compose-cli/cli/formatter"
)
type listVolumeOpts struct {
@ -55,14 +56,14 @@ func listVolume() *cobra.Command {
return nil
}
view := viewFromVolumeList(vols)
return formatter.Print(view, opts.format, os.Stdout, func(w io.Writer) {
return formatter2.Print(view, opts.format, os.Stdout, func(w io.Writer) {
for _, vol := range view {
_, _ = fmt.Fprintf(w, "%s\t%s\n", vol.ID, vol.Description)
}
}, "ID", "DESCRIPTION")
},
}
cmd.Flags().StringVar(&opts.format, "format", formatter.PRETTY, "Format the output. Values: [pretty | json]. (Default: pretty)")
cmd.Flags().StringVar(&opts.format, "format", formatter2.PRETTY, "Format the output. Values: [pretty | json]. (Default: pretty)")
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
return cmd
}

View file

@ -1,124 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"fmt"
"os"
"strconv"
"github.com/mattn/go-isatty"
)
var names = []string{
"grey",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
}
const (
// Never use ANSI codes
Never = "never"
// Always use ANSI codes
Always = "always"
// Auto detect terminal is a tty and can use ANSI codes
Auto = "auto"
)
// SetANSIMode configure formatter for colored output on ANSI-compliant console
func SetANSIMode(ansi string) {
if !useAnsi(ansi) {
nextColor = func() colorFunc {
return monochrome
}
}
}
func useAnsi(ansi string) bool {
switch ansi {
case Always:
return true
case Auto:
return isatty.IsTerminal(os.Stdout.Fd())
}
return false
}
// colorFunc use ANSI codes to render colored text on console
type colorFunc func(s string) string
var monochrome = func(s string) string {
return s
}
func ansiColor(code, s string) string {
return fmt.Sprintf("%s%s%s", ansi(code), s, ansi("0"))
}
func ansi(code string) string {
return fmt.Sprintf("\033[%sm", code)
}
func makeColorFunc(code string) colorFunc {
return func(s string) string {
return ansiColor(code, s)
}
}
var nextColor = rainbowColor
func rainbowColor() colorFunc {
return <-loop
}
var loop = make(chan colorFunc)
func init() {
colors := map[string]colorFunc{}
for i, name := range names {
colors[name] = makeColorFunc(strconv.Itoa(30 + i))
colors["intense_"+name] = makeColorFunc(strconv.Itoa(30+i) + ";1")
}
go func() {
i := 0
rainbow := []colorFunc{
colors["cyan"],
colors["yellow"],
colors["green"],
colors["magenta"],
colors["blue"],
colors["intense_cyan"],
colors["intense_yellow"],
colors["intense_green"],
colors["intense_magenta"],
colors["intense_blue"],
}
for {
loop <- rainbow[i]
i = (i + 1) % len(rainbow)
}
}()
}

View file

@ -1,26 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
const (
// JSON is the constant for Json formats on list commands
JSON = "json"
// TemplateLegacyJSON the legacy json formatting value using go template
TemplateLegacyJSON = "{{json.}}"
// PRETTY is the constant for default formats on list commands
PRETTY = "pretty"
)

View file

@ -1,73 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"fmt"
"io"
"reflect"
"strings"
"github.com/docker/compose-cli/pkg/api"
"github.com/pkg/errors"
)
// Print prints formatted lists in different formats
func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error {
switch strings.ToLower(format) {
case PRETTY, "":
return PrintPrettySection(outWriter, writerFn, headers...)
case TemplateLegacyJSON:
switch reflect.TypeOf(toJSON).Kind() {
case reflect.Slice:
s := reflect.ValueOf(toJSON)
for i := 0; i < s.Len(); i++ {
obj := s.Index(i).Interface()
outJSON, err := ToJSON(obj, "", "")
if err != nil {
return err
}
_, _ = fmt.Fprint(outWriter, outJSON)
}
default:
outJSON, err := ToStandardJSON(toJSON)
if err != nil {
return err
}
_, _ = fmt.Fprintln(outWriter, outJSON)
}
case JSON:
switch reflect.TypeOf(toJSON).Kind() {
case reflect.Slice:
outJSON, err := ToJSON(toJSON, "", "")
if err != nil {
return err
}
_, _ = fmt.Fprint(outWriter, outJSON)
default:
outJSON, err := ToStandardJSON(toJSON)
if err != nil {
return err
}
_, _ = fmt.Fprintln(outWriter, outJSON)
}
default:
return errors.Wrapf(api.ErrParsingFailed, "format value %q could not be parsed", format)
}
return nil
}

View file

@ -1,73 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"bytes"
"fmt"
"io"
"testing"
"gotest.tools/assert"
)
type testStruct struct {
Name string
Status string
}
// Print prints formatted lists in different formats
func TestPrint(t *testing.T) {
testList := []testStruct{
{
Name: "myName1",
Status: "myStatus1",
},
{
Name: "myName2",
Status: "myStatus2",
},
}
b := &bytes.Buffer{}
assert.NilError(t, Print(testList, PRETTY, b, func(w io.Writer) {
for _, t := range testList {
_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
}
}, "NAME", "STATUS"))
assert.Equal(t, b.String(), "NAME STATUS\nmyName1 myStatus1\nmyName2 myStatus2\n")
b.Reset()
assert.NilError(t, Print(testList, JSON, b, func(w io.Writer) {
for _, t := range testList {
_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
}
}, "NAME", "STATUS"))
assert.Equal(t, b.String(), `[{"Name":"myName1","Status":"myStatus1"},{"Name":"myName2","Status":"myStatus2"}]
`)
b.Reset()
assert.NilError(t, Print(testList, TemplateLegacyJSON, b, func(w io.Writer) {
for _, t := range testList {
_, _ = fmt.Fprintf(w, "%s\t%s\n", t.Name, t.Status)
}
}, "NAME", "STATUS"))
json := b.String()
assert.Equal(t, json, `{"Name":"myName1","Status":"myStatus1"}
{"Name":"myName2","Status":"myStatus2"}
`)
}

View file

@ -1,39 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"bytes"
"encoding/json"
)
const standardIndentation = " "
// ToStandardJSON return a string with the JSON representation of the interface{}
func ToStandardJSON(i interface{}) (string, error) {
return ToJSON(i, "", standardIndentation)
}
// ToJSON return a string with the JSON representation of the interface{}
func ToJSON(i interface{}, prefix string, indentation string) (string, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent(prefix, indentation)
err := encoder.Encode(i)
return buffer.String(), err
}

View file

@ -1,115 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"github.com/docker/compose-cli/pkg/api"
)
// NewLogConsumer creates a new LogConsumer
func NewLogConsumer(ctx context.Context, w io.Writer, color bool, prefix bool) api.LogConsumer {
return &logConsumer{
ctx: ctx,
presenters: map[string]*presenter{},
width: 0,
writer: w,
color: color,
prefix: prefix,
}
}
func (l *logConsumer) Register(name string) {
l.register(name)
}
func (l *logConsumer) register(name string) *presenter {
cf := monochrome
if l.color {
cf = nextColor()
}
p := &presenter{
colors: cf,
name: name,
}
l.presenters[name] = p
if l.prefix {
l.computeWidth()
for _, p := range l.presenters {
p.setPrefix(l.width)
}
}
return p
}
// Log formats a log message as received from name/container
func (l *logConsumer) Log(container, service, message string) {
if l.ctx.Err() != nil {
return
}
p, ok := l.presenters[container]
if !ok { // should have been registered, but ¯\_(ツ)_/¯
p = l.register(container)
}
for _, line := range strings.Split(message, "\n") {
fmt.Fprintf(l.writer, "%s %s\n", p.prefix, line) // nolint:errcheck
}
}
func (l *logConsumer) Status(container, msg string) {
p, ok := l.presenters[container]
if !ok {
p = l.register(container)
}
s := p.colors(fmt.Sprintf("%s %s\n", container, msg))
l.writer.Write([]byte(s)) // nolint:errcheck
}
func (l *logConsumer) computeWidth() {
width := 0
for _, p := range l.presenters {
if len(p.name) > width {
width = len(p.name)
}
}
l.width = width + 1
}
// LogConsumer consume logs from services and format them
type logConsumer struct {
ctx context.Context
presenters map[string]*presenter
width int
writer io.Writer
color bool
prefix bool
}
type presenter struct {
colors colorFunc
name string
prefix string
}
func (p *presenter) setPrefix(width int) {
p.prefix = p.colors(fmt.Sprintf("%-"+strconv.Itoa(width)+"s |", p.name))
}

View file

@ -1,38 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"strings"
"github.com/hashicorp/go-multierror"
)
// SetMultiErrorFormat set cli default format for multi-errors
func SetMultiErrorFormat(errs *multierror.Error) {
if errs != nil {
errs.ErrorFormat = formatErrors
}
}
func formatErrors(errs []error) string {
messages := make([]string, len(errs))
for i, err := range errs {
messages[i] = "Error: " + err.Error()
}
return strings.Join(messages, "\n")
}

View file

@ -1,32 +0,0 @@
/*
Copyright 2020 Docker Compose CLI authors
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.
*/
package formatter
import (
"fmt"
"io"
"strings"
"text/tabwriter"
)
// PrintPrettySection prints a tabbed section on the writer parameter
func PrintPrettySection(out io.Writer, printer func(writer io.Writer), headers ...string) error {
w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
_, _ = fmt.Fprintln(w, strings.Join(headers, "\t"))
printer(w)
return w.Flush()
}

View file

@ -228,7 +228,7 @@ func main() {
// On default context, "compose" is implemented by CLI Plugin
proxy := api.NewServiceProxy().WithService(service.ComposeService())
command := compose2.RootCommand(ctype, proxy)
command := compose2.RootCommand(proxy)
if ctype == store.AciContextType {
customizeCliForACI(command, proxy)