File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -88,7 +88,17 @@ func Execute(rootCmd *cobra.Command) error {
8888 return nil
8989}
9090
91- func NewRootCmd (l zerolog.Logger ) * cobra.Command {
91+ type RootCmd struct {
92+ * cobra.Command
93+ // ActionOpts is a pointer-to-pointer to the global actionOpts variable.
94+ // This allows the RootCmd to reference the global state that gets initialized
95+ // in PersistentPreRunE and used across all subcommands. The double indirection
96+ // ensures that when the global actionOpts is updated (e.g., with new connection),
97+ // all references automatically point to the updated value.
98+ ActionOpts * * options.ActionsOpts
99+ }
100+
101+ func NewRootCmd (l zerolog.Logger ) * RootCmd {
92102 rootCmd := & cobra.Command {
93103 Use : appName ,
94104 Short : "Chainloop Command Line Interface" ,
@@ -155,6 +165,7 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
155165 if err != nil {
156166 return err
157167 }
168+
158169 actionOpts = newActionOpts (logger , conn , authToken )
159170
160171 if ! isTelemetryDisabled () {
@@ -259,7 +270,7 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
259270 }
260271 }
261272
262- return rootCmd
273+ return & RootCmd { Command : rootCmd , ActionOpts : & actionOpts }
263274}
264275
265276// this could have been done using automatic + prefix but we want to have control and know the values
Original file line number Diff line number Diff line change @@ -35,8 +35,7 @@ func main() {
3535 // Couldn't find an easier way to disable the timestamp
3636 logger := zerolog .New (zerolog.ConsoleWriter {Out : os .Stderr , FormatTimestamp : func (interface {}) string { return "" }})
3737 rootCmd := cmd .NewRootCmd (logger )
38- // Run the command
39- if err := cmd .Execute (rootCmd ); err != nil {
38+ if err := cmd .Execute (rootCmd .Command ); err != nil {
4039 msg , exitCode := errorInfo (err , logger )
4140 logger .Error ().Msg (msg )
4241 os .Exit (exitCode )
You can’t perform that action at this time.
0 commit comments