Browse Source

ci: tweak restricted imports in linter (#10992)

* Eliminate direct dependency on gopkg.in/yaml.v2
* Add gopkg.in/yaml.v2 as a restricted import
* Add github.com/distribution/distribution as a restricted dependency in favor of distribution/reference which is the subset of functionality that Compose needs
* Remove an unused exclusion

NOTE: This does change the `compose config` output slightly but does NOT  change the semantics:
* YAML indentation is slightly different for lists (this is a `v2` / `v3` thing)
* JSON is now "minified" instead of pretty-printed (I think this generally desirable and more consistent with other JSON command outputs)

Signed-off-by: Milas Bowman <[email protected]>
Milas Bowman 2 years ago
parent
commit
19bbb12fac
3 changed files with 8 additions and 9 deletions
  1. 4 2
      .golangci.yml
  2. 1 1
      go.mod
  3. 3 6
      pkg/compose/compose.go

+ 4 - 2
.golangci.yml

@@ -36,9 +36,13 @@ linters-settings:
         deny:
           - pkg: io/ioutil
             desc: 'io/ioutil package has been deprecated'
+          - pkg: gopkg.in/yaml.v2
+            desc: 'compose-go uses yaml.v3'
   gomodguard:
     blocked:
       versions:
+        - github.com/distribution/distribution:
+            reason: "use distribution/reference"
         - gotest.tools:
             version: "< 3.0.0"
             reason: "deprecated, pre-modules version"
@@ -61,5 +65,3 @@ issues:
   # golangci hides some golint warnings (the warning about exported things
   # withtout documentation for example), this will make it show them anyway.
   exclude-use-default: false
-  exclude:
-    - should not use dot imports

+ 1 - 1
go.mod

@@ -48,7 +48,6 @@ require (
 	go.uber.org/goleak v1.2.1
 	golang.org/x/sync v0.3.0
 	google.golang.org/grpc v1.58.0
-	gopkg.in/yaml.v2 v2.4.0
 	gotest.tools/v3 v3.5.0
 )
 
@@ -172,6 +171,7 @@ require (
 	google.golang.org/protobuf v1.31.0 // indirect
 	gopkg.in/inf.v0 v0.9.1 // indirect
 	gopkg.in/ini.v1 v1.67.0 // indirect
+	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 	k8s.io/api v0.26.2 // indirect
 	k8s.io/apimachinery v0.26.2 // indirect

+ 3 - 6
pkg/compose/compose.go

@@ -18,7 +18,6 @@ package compose
 
 import (
 	"context"
-	"encoding/json"
 	"fmt"
 	"io"
 	"os"
@@ -36,15 +35,13 @@ import (
 	"github.com/docker/cli/cli/config/configfile"
 	"github.com/docker/cli/cli/flags"
 	"github.com/docker/cli/cli/streams"
+	"github.com/docker/compose/v2/pkg/api"
 	moby "github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/filters"
 	"github.com/docker/docker/api/types/swarm"
 	"github.com/docker/docker/client"
 	"github.com/opencontainers/go-digest"
 	"github.com/pkg/errors"
-	"gopkg.in/yaml.v2"
-
-	"github.com/docker/compose/v2/pkg/api"
 )
 
 var stdioToStdout bool
@@ -169,9 +166,9 @@ func (s *composeService) Config(ctx context.Context, project *types.Project, opt
 
 	switch options.Format {
 	case "json":
-		return json.MarshalIndent(project, "", "  ")
+		return project.MarshalJSON()
 	case "yaml":
-		return yaml.Marshal(project)
+		return project.MarshalYAML()
 	default:
 		return nil, fmt.Errorf("unsupported format %q", options.Format)
 	}