Skip to content

Commit 1424f80

Browse files
authored
rm Topaz CLI CommonCtx.Context (#701)
* Remove embedded context.Context from the CommonCtx struct * Explicitly pass ctx parameter * Rename CommonCtx struct to Config * Make cc functions static
1 parent dfe4501 commit 1424f80

Some content is hidden

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

69 files changed

+607
-631
lines changed

.vscode/settings.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"formatting.gofumpt": true
55
},
66
"go.testFlags": [
7-
"-race"
7+
"-v",
8+
"-gcflags=all=-N -l",
9+
"-count=1"
810
],
911
"sarif-viewer.connectToGithubCodeScanning": "off",
1012
"cSpell.words": [
@@ -332,5 +334,8 @@
332334
"zeroinstall",
333335
"zerolog",
334336
"zpages"
335-
]
337+
],
338+
"go.alternateTools": {
339+
340+
}
336341
}

topaz/cc/cc.go

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"sync"
1010

1111
"github.com/aserto-dev/topaz/internal/fs"
12-
"github.com/aserto-dev/topaz/topaz/cc/iostream"
1312
"github.com/aserto-dev/topaz/topaz/dockerx"
1413
"github.com/docker/docker/api/types/container"
1514
"github.com/pkg/errors"
@@ -21,13 +20,7 @@ var (
2120
ErrIsRunning = errors.New("topaz is already running, use 'topaz stop' to stop")
2221
)
2322

24-
type CommonCtx struct {
25-
Context context.Context
26-
std *iostream.StdIO
27-
Config *CLIConfig
28-
}
29-
30-
type CLIConfig struct {
23+
type Config struct {
3124
Version int `json:"version"`
3225
Active ActiveConfig `json:"active"`
3326
Running RunningConfig `json:"running"`
@@ -61,25 +54,22 @@ const (
6154
)
6255

6356
var (
57+
config *Config
6458
defaults DefaultsConfig
6559
once sync.Once
6660
)
6761

68-
func NewCommonContext(ctx context.Context, noCheck bool, configFilePath string) (*CommonCtx, error) {
69-
commonCtx := &CommonCtx{
70-
Context: ctx,
71-
std: iostream.DefaultIO(),
72-
Config: &CLIConfig{
73-
Version: 1,
74-
Active: ActiveConfig{
75-
ConfigFile: filepath.Join(GetTopazCfgDir(), "config.yaml"),
76-
Config: "config.yaml",
77-
},
78-
Defaults: DefaultsConfig{
79-
NoCheck: noCheck,
80-
},
81-
Running: RunningConfig{},
62+
func NewConfig(ctx context.Context, noCheck bool, configFilePath string) (*Config, error) {
63+
cfg := &Config{
64+
Version: 1,
65+
Active: ActiveConfig{
66+
ConfigFile: filepath.Join(GetTopazCfgDir(), "config.yaml"),
67+
Config: "config.yaml",
68+
},
69+
Defaults: DefaultsConfig{
70+
NoCheck: noCheck,
8271
},
72+
Running: RunningConfig{},
8373
}
8474

8575
if _, err := os.Stat(configFilePath); err == nil {
@@ -88,24 +78,32 @@ func NewCommonContext(ctx context.Context, noCheck bool, configFilePath string)
8878
return nil, err
8979
}
9080

91-
if err := json.Unmarshal(data, commonCtx.Config); err != nil {
81+
if err := json.Unmarshal(data, cfg); err != nil {
9282
return nil, err
9383
}
9484
}
9585

96-
setDefaults(commonCtx)
86+
setDefaults(cfg)
9787

98-
return commonCtx, nil
88+
return cfg, nil
9989
}
10090

101-
func (c *CommonCtx) CheckRunStatus(containerName string, expectedStatus runStatus) bool {
102-
if c.Config.Defaults.NoCheck {
91+
func GetConfig() *Config {
92+
if config == nil {
93+
panic("config not initialized")
94+
}
95+
96+
return config
97+
}
98+
99+
func (cfg *Config) CheckRunStatus(containerName string, expectedStatus runStatus) bool {
100+
if cfg.Defaults.NoCheck {
103101
return false
104102
}
105103

106104
// set default container name if not specified.
107105
if containerName == "" {
108-
containerName = ContainerName(c.Config.Active.ConfigFile)
106+
containerName = ContainerName(cfg.Active.ConfigFile)
109107
}
110108

111109
dc, err := dockerx.New()
@@ -123,7 +121,7 @@ func (c *CommonCtx) CheckRunStatus(containerName string, expectedStatus runStatu
123121
return lo.Ternary(running, StatusRunning, StatusNotRunning) == expectedStatus
124122
}
125123

126-
func (c *CommonCtx) GetRunningContainers() ([]container.Summary, error) {
124+
func (cfg *Config) GetRunningContainers() ([]container.Summary, error) {
127125
dc, err := dockerx.New()
128126
if err != nil {
129127
return nil, err
@@ -137,10 +135,10 @@ func (c *CommonCtx) GetRunningContainers() ([]container.Summary, error) {
137135
return topazContainers, nil
138136
}
139137

140-
func (c *CommonCtx) SaveContextConfig(configurationFile string) error {
138+
func (cfg *Config) SaveContextConfig(configurationFile string) error {
141139
cliConfig := filepath.Join(GetTopazDir(), configurationFile)
142140

143-
kongConfigBytes, err := json.Marshal(c.Config)
141+
kongConfigBytes, err := json.Marshal(cfg)
144142
if err != nil {
145143
return err
146144
}
@@ -149,21 +147,14 @@ func (c *CommonCtx) SaveContextConfig(configurationFile string) error {
149147
return err
150148
}
151149

152-
defaults = c.Config.Defaults
150+
defaults = cfg.Defaults
153151

154152
return nil
155153
}
156154

157-
func setDefaults(ctx *CommonCtx) {
155+
func setDefaults(cfg *Config) {
158156
once.Do(func() {
159-
defaults = ctx.Config.Defaults
157+
config = cfg
158+
defaults = cfg.Defaults
160159
})
161160
}
162-
163-
func (c *CommonCtx) StdOut() *os.File {
164-
return c.std.StdOut()
165-
}
166-
167-
func (c *CommonCtx) StdErr() *os.File {
168-
return c.std.StdErr()
169-
}

topaz/cc/client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const (
1818
defaultInsecure = false
1919
defaultPlaintext = false
2020
defaultTimeout = 5 * time.Second
21-
defaultNoCheck = false
22-
defaultNoColor = false
2321
)
2422

2523
func DirectorySvc() string {

topaz/cc/conmsg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ type ConMsg struct {
1414
}
1515

1616
// Con() - console message send to StdErr, default no-color.
17-
func (c *CommonCtx) Con() *ConMsg {
17+
func Con() *ConMsg {
1818
return &ConMsg{
1919
out: color.Error,
2020
color: color.New(),
2121
}
2222
}
2323

2424
// Out() - console output message send to StdOut, default no-color.
25-
func (c *CommonCtx) Out() *ConMsg {
25+
func Out() *ConMsg {
2626
return &ConMsg{
2727
out: color.Output,
2828
color: color.New(),

topaz/cc/iostream/iostream.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

topaz/certs/generator.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"path/filepath"
66

77
"github.com/aserto-dev/certs"
8-
"github.com/aserto-dev/topaz/topaz/cc"
98
"github.com/aserto-dev/topaz/topaz/table"
109
"github.com/pkg/errors"
1110
"github.com/rs/zerolog"
@@ -31,15 +30,15 @@ func (c *CertPaths) FindExisting() []string {
3130
return existing
3231
}
3332

34-
func GenerateCerts(c *cc.CommonCtx, force bool, dnsNames []string, certPaths ...*CertPaths) error {
33+
func GenerateCerts(force bool, dnsNames []string, certPaths ...*CertPaths) error {
3534
if !force {
3635
existingFiles := make([]string, 0, len(certPaths))
3736
for _, cert := range certPaths {
3837
existingFiles = append(existingFiles, cert.FindExisting()...)
3938
}
4039

4140
if len(existingFiles) != 0 {
42-
tab := table.New(c.StdErr())
41+
tab := table.New(os.Stderr)
4342
defer tab.Close()
4443

4544
tab.Header("File", "Action")
@@ -56,14 +55,14 @@ func GenerateCerts(c *cc.CommonCtx, force bool, dnsNames []string, certPaths ...
5655
}
5756
}
5857

59-
return generate(c, dnsNames, certPaths...)
58+
return generate(dnsNames, certPaths...)
6059
}
6160

62-
func generate(c *cc.CommonCtx, dnsNames []string, certPaths ...*CertPaths) error {
61+
func generate(dnsNames []string, certPaths ...*CertPaths) error {
6362
logger := zerolog.Nop()
6463
generator := certs.NewGenerator(&logger)
6564

66-
tab := table.New(c.StdErr())
65+
tab := table.New(os.Stderr)
6766
defer tab.Close()
6867

6968
tab.Header("File", "Action")

topaz/clients/authorizer/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"google.golang.org/grpc"
1010

1111
az2 "github.com/aserto-dev/go-authorizer/aserto/authorizer/v2"
12-
"github.com/aserto-dev/topaz/topaz/cc"
1312
"github.com/aserto-dev/topaz/topaz/clients"
1413
)
1514

@@ -37,8 +36,8 @@ func New(conn *grpc.ClientConn) *Client {
3736
}
3837
}
3938

40-
func NewClient(c *cc.CommonCtx, cfg *Config) (*Client, error) {
41-
conn, err := cfg.Connect(c.Context)
39+
func NewClient(ctx context.Context, cfg *Config) (*Client, error) {
40+
conn, err := cfg.Connect(ctx)
4241
if err != nil {
4342
return nil, err
4443
}

topaz/clients/directory/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
dsm3 "github.com/aserto-dev/go-directory/aserto/directory/model/v3"
1212
dsr3 "github.com/aserto-dev/go-directory/aserto/directory/reader/v3"
1313
dsw3 "github.com/aserto-dev/go-directory/aserto/directory/writer/v3"
14-
"github.com/aserto-dev/topaz/topaz/cc"
1514
"github.com/aserto-dev/topaz/topaz/clients"
1615
acc1 "github.com/authzen/access.go/api/access/v1"
1716

@@ -55,8 +54,8 @@ func New(conn *grpc.ClientConn) *Client {
5554
}
5655
}
5756

58-
func NewClient(c *cc.CommonCtx, cfg *Config) (*Client, error) {
59-
conn, err := cfg.Connect(c.Context)
57+
func NewClient(ctx context.Context, cfg *Config) (*Client, error) {
58+
conn, err := cfg.Connect(ctx)
6059
if err != nil {
6160
return nil, err
6261
}

topaz/clients/request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io"
66
"os"
77

8-
"github.com/aserto-dev/topaz/topaz/cc"
98
"github.com/aserto-dev/topaz/topaz/edit"
109
"github.com/aserto-dev/topaz/topaz/fflag"
1110
"github.com/aserto-dev/topaz/topaz/jsonx"
@@ -22,7 +21,7 @@ type RequestArgs struct {
2221
Editor bool `name:"edit" short:"e" help:"open request in editor" hidden:"" type:"fflag.Editor"`
2322
}
2423

25-
func (cmd *RequestArgs) Process(c *cc.CommonCtx, req proto.Message, tmpl func() proto.Message) error {
24+
func (cmd *RequestArgs) Process(req proto.Message, tmpl func() proto.Message) error {
2625
if cmd.Request == "" && cmd.Editor && fflag.Enabled(fflag.Editor) {
2726
req, err := edit.Msg(tmpl())
2827
if err != nil {

topaz/cmd/access/actionsearch.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package access
22

33
import (
4-
"github.com/aserto-dev/topaz/topaz/cc"
4+
"context"
5+
"os"
6+
57
"github.com/aserto-dev/topaz/topaz/clients"
68
dsc "github.com/aserto-dev/topaz/topaz/clients/directory"
79
"github.com/aserto-dev/topaz/topaz/jsonx"
@@ -19,20 +21,20 @@ type ActionSearchCmd struct {
1921
resp dsa1.ActionSearchResponse
2022
}
2123

22-
func (cmd *ActionSearchCmd) Run(c *cc.CommonCtx) error {
24+
func (cmd *ActionSearchCmd) Run(ctx context.Context) error {
2325
if cmd.Template {
24-
return jsonx.OutputJSONPB(c.StdOut(), cmd.template())
26+
return jsonx.OutputJSONPB(os.Stdout, cmd.template())
2527
}
2628

27-
if err := cmd.Process(c, &cmd.req, cmd.template); err != nil {
29+
if err := cmd.Process(&cmd.req, cmd.template); err != nil {
2830
return err
2931
}
3032

31-
if err := cmd.Invoke(c.Context, dsa1.Access_ActionSearch_FullMethodName, &cmd.req, &cmd.resp); err != nil {
33+
if err := cmd.Invoke(ctx, dsa1.Access_ActionSearch_FullMethodName, &cmd.req, &cmd.resp); err != nil {
3234
return err
3335
}
3436

35-
return jsonx.OutputJSONPB(c.StdOut(), &cmd.resp)
37+
return jsonx.OutputJSONPB(os.Stdout, &cmd.resp)
3638
}
3739

3840
func (cmd *ActionSearchCmd) template() proto.Message {

0 commit comments

Comments
 (0)