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"
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
6356var (
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- }
0 commit comments