Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 4840258

Browse files
committed
replace passing parameters by context with singletons
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 9301c29 commit 4840258

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+168
-204
lines changed

aci/backend.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package aci
1818

1919
import (
20-
"context"
2120
"strings"
2221

2322
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
@@ -68,9 +67,9 @@ func init() {
6867
backend.Register(backendType, backendType, service, getCloudService)
6968
}
7069

71-
func service(ctx context.Context) (backend.Service, error) {
72-
contextStore := store.ContextStore(ctx)
73-
currentContext := apicontext.CurrentContext(ctx)
70+
func service() (backend.Service, error) {
71+
contextStore := store.Instance()
72+
currentContext := apicontext.Current()
7473
var aciContext store.AciContext
7574

7675
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {

api/backend/backend.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package backend
1818

1919
import (
20-
"context"
2120
"errors"
2221
"fmt"
2322

@@ -38,7 +37,7 @@ var (
3837
errTypeRegistered = errors.New("backend: already registered")
3938
)
4039

41-
type initFunc func(context.Context) (Service, error)
40+
type initFunc func() (Service, error)
4241
type getCloudServiceFunc func() (cloud.Service, error)
4342

4443
type registeredBackend struct {
@@ -52,6 +51,18 @@ var backends = struct {
5251
r []*registeredBackend
5352
}{}
5453

54+
var instance Service
55+
56+
// Current return the active backend instance
57+
func Current() Service {
58+
return instance
59+
}
60+
61+
// WithBackend set the active backend instance
62+
func WithBackend(s Service) {
63+
instance = s
64+
}
65+
5566
// Service aggregates the service interfaces
5667
type Service interface {
5768
ContainerService() containers.Service
@@ -85,10 +96,10 @@ func Register(name string, backendType string, init initFunc, getCoudService get
8596

8697
// Get returns the backend registered for a particular type, it returns
8798
// an error if there is no registered backends for the given type.
88-
func Get(ctx context.Context, backendType string) (Service, error) {
99+
func Get(backendType string) (Service, error) {
89100
for _, b := range backends.r {
90101
if b.backendType == backendType {
91-
return b.init(ctx)
102+
return b.init()
92103
}
93104
}
94105

@@ -97,7 +108,7 @@ func Get(ctx context.Context, backendType string) (Service, error) {
97108

98109
// GetCloudService returns the backend registered for a particular type, it returns
99110
// an error if there is no registered backends for the given type.
100-
func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
111+
func GetCloudService(backendType string) (cloud.Service, error) {
101112
for _, b := range backends.r {
102113
if b.backendType == backendType {
103114
return b.getCloudService()

api/client/client.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,25 @@ import (
2525
"github.com/docker/compose-cli/api/containers"
2626
apicontext "github.com/docker/compose-cli/api/context"
2727
"github.com/docker/compose-cli/api/context/store"
28+
"github.com/docker/compose-cli/api/errdefs"
2829
"github.com/docker/compose-cli/api/resources"
2930
"github.com/docker/compose-cli/api/secrets"
3031
"github.com/docker/compose-cli/api/volumes"
3132
)
3233

3334
// New returns a backend client associated with current context
3435
func New(ctx context.Context) (*Client, error) {
35-
return newWithDefaultBackend(ctx, "")
36-
}
37-
38-
// NewWithDefaultLocalBackend returns a backend client associated with current context or local backend if on default context type
39-
func NewWithDefaultLocalBackend(ctx context.Context) (*Client, error) {
40-
return newWithDefaultBackend(ctx, store.LocalContextType)
41-
}
42-
43-
func newWithDefaultBackend(ctx context.Context, defaultBackend string) (*Client, error) {
44-
currentContext := apicontext.CurrentContext(ctx)
45-
s := store.ContextStore(ctx)
36+
currentContext := apicontext.Current()
37+
s := store.Instance()
4638

4739
cc, err := s.Get(currentContext)
4840
if err != nil {
4941
return nil, err
5042
}
5143

52-
backendName := cc.Type()
53-
if backendName == store.DefaultContextType && defaultBackend != "" {
54-
backendName = defaultBackend
55-
}
56-
57-
service, err := backend.Get(ctx, backendName)
58-
if err != nil {
59-
return nil, err
44+
service := backend.Current()
45+
if service == nil {
46+
return nil, errdefs.ErrNotFound
6047
}
6148

6249
client := NewClient(cc.Type(), service)
@@ -73,7 +60,7 @@ func NewClient(backendType string, service backend.Service) Client {
7360

7461
// GetCloudService returns a backend CloudService (typically login, create context)
7562
func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
76-
return backend.GetCloudService(ctx, backendType)
63+
return backend.GetCloudService(backendType)
7764
}
7865

7966
// Client is a multi-backend client

api/config/config.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package config
1818

1919
import (
20-
"context"
2120
"encoding/json"
2221
"io/ioutil"
2322
"os"
@@ -28,17 +27,16 @@ import (
2827
"github.com/docker/compose-cli/api/context/store"
2928
)
3029

31-
type dirKey struct{}
30+
var configDir string
3231

3332
// WithDir sets the config directory path in the context
34-
func WithDir(ctx context.Context, path string) context.Context {
35-
return context.WithValue(ctx, dirKey{}, path)
33+
func WithDir(path string) {
34+
configDir = path
3635
}
3736

3837
// Dir returns the config directory path
39-
func Dir(ctx context.Context) string {
40-
cd, _ := ctx.Value(dirKey{}).(string)
41-
return cd
38+
func Dir() string {
39+
return configDir
4240
}
4341

4442
// LoadFile loads the docker configuration

api/context/context.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,14 @@
1616

1717
package context
1818

19-
import (
20-
gocontext "context"
21-
22-
"golang.org/x/net/context"
23-
24-
cliflags "github.com/docker/cli/cli/flags"
25-
)
26-
27-
type currentContextKey struct{}
28-
type cliOptionsKey struct{}
19+
var current string
2920

3021
// WithCurrentContext sets the name of the current docker context
31-
func WithCurrentContext(ctx gocontext.Context, contextName string) context.Context {
32-
return context.WithValue(ctx, currentContextKey{}, contextName)
33-
}
34-
35-
// CurrentContext returns the current context name
36-
func CurrentContext(ctx context.Context) string {
37-
cc, _ := ctx.Value(currentContextKey{}).(string)
38-
return cc
39-
}
40-
41-
// WithCliOptions sets CLI options
42-
func WithCliOptions(ctx gocontext.Context, options cliflags.CommonOptions) context.Context {
43-
return context.WithValue(ctx, cliOptionsKey{}, options)
22+
func WithCurrentContext(contextName string) {
23+
current = contextName
4424
}
4525

46-
// CliOptions returns common cli options
47-
func CliOptions(ctx context.Context) cliflags.CommonOptions {
48-
cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions)
49-
return cc
26+
// Current returns the current context name
27+
func Current() string {
28+
return current
5029
}

api/context/store/store.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package store
1818

1919
import (
20-
"context"
2120
"encoding/json"
2221
"fmt"
2322
"io/ioutil"
@@ -67,17 +66,16 @@ const (
6766
metaFile = "meta.json"
6867
)
6968

70-
type contextStoreKey struct{}
69+
var instance Store
7170

7271
// WithContextStore adds the store to the context
73-
func WithContextStore(ctx context.Context, store Store) context.Context {
74-
return context.WithValue(ctx, contextStoreKey{}, store)
72+
func WithContextStore(store Store) {
73+
instance = store
7574
}
7675

77-
// ContextStore returns the store from the context
78-
func ContextStore(ctx context.Context) Store {
79-
s, _ := ctx.Value(contextStoreKey{}).(Store)
80-
return s
76+
// Instance returns the store from the context
77+
func Instance() Store {
78+
return instance
8179
}
8280

8381
// Store is the context store

cli/cmd/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func buildCommand(p *projectOptions) *cobra.Command {
6363
}
6464

6565
func runBuild(ctx context.Context, opts buildOptions, services []string) error {
66-
c, err := client.NewWithDefaultLocalBackend(ctx)
66+
c, err := client.New(ctx)
6767
if err != nil {
6868
return err
6969
}

cli/cmd/compose/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func convertCommand(p *projectOptions) *cobra.Command {
7777

7878
func runConvert(ctx context.Context, opts convertOptions, services []string) error {
7979
var json []byte
80-
c, err := client.NewWithDefaultLocalBackend(ctx)
80+
c, err := client.New(ctx)
8181
if err != nil {
8282
return err
8383
}
@@ -88,7 +88,7 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err
8888
}
8989

9090
if opts.resolve {
91-
configFile, err := cliconfig.Load(config.Dir(ctx))
91+
configFile, err := cliconfig.Load(config.Dir())
9292
if err != nil {
9393
return err
9494
}

cli/cmd/compose/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func downCommand(p *projectOptions, contextType string) *cobra.Command {
6969
}
7070

7171
func runDown(ctx context.Context, opts downOptions) error {
72-
c, err := client.NewWithDefaultLocalBackend(ctx)
72+
c, err := client.New(ctx)
7373
if err != nil {
7474
return err
7575
}

cli/cmd/compose/events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func eventsCommand(p *projectOptions) *cobra.Command {
5151
}
5252

5353
func runEvents(ctx context.Context, opts eventsOpts, services []string) error {
54-
c, err := client.NewWithDefaultLocalBackend(ctx)
54+
c, err := client.New(ctx)
5555
if err != nil {
5656
return err
5757
}

0 commit comments

Comments
 (0)