11package cmd
22
33import (
4+ "context"
45 "fmt"
56 "os"
67
78 "github.com/compozy/releasepr/internal/config"
9+ "github.com/compozy/releasepr/internal/logger"
810 "github.com/compozy/releasepr/internal/orchestrator"
911 "github.com/compozy/releasepr/internal/repository"
1012 "github.com/compozy/releasepr/internal/service"
1113 "github.com/spf13/afero"
14+ "github.com/spf13/cobra"
15+ "go.uber.org/zap"
1216)
1317
1418// container holds all the dependencies for the application.
@@ -65,11 +69,25 @@ func InitCommands() error {
6569 if err != nil {
6670 return err
6771 }
72+ ctx := rootCmd .Context ()
73+ if ctx == nil {
74+ return fmt .Errorf ("root command context not initialized" )
75+ }
76+ ctx = config .IntoContext (ctx , c .cfg )
77+ appLogger , err := logger .New (c .cfg .LoggerConfig ())
78+ if err != nil {
79+ return fmt .Errorf ("failed to initialize logger: %w" , err )
80+ }
81+ ctx = logger .IntoContext (ctx , appLogger )
82+ rootCmd .SetContext (ctx )
83+ rootCmd .PersistentPostRunE = func (cmd * cobra.Command , _ []string ) error {
84+ return logger .Sync (logger .FromContext (cmd .Context ()))
85+ }
6886
6987 // Individual commands have been replaced by orchestrator commands
7088
7189 // Add orchestrator-based commands
72- if err := addOrchestratorCommands (c ); err != nil {
90+ if err := addOrchestratorCommands (ctx , c ); err != nil {
7391 return err
7492 }
7593
@@ -79,7 +97,8 @@ func InitCommands() error {
7997}
8098
8199// addOrchestratorCommands adds the new consolidated commands
82- func addOrchestratorCommands (c * container ) error {
100+ func addOrchestratorCommands (ctx context.Context , c * container ) error {
101+ log := logger .FromContext (ctx ).Named ("cmd.container" )
83102 // Initialize extended repositories for orchestrators
84103 gitExtRepo , err := repository .NewGitExtendedRepository ()
85104 if err != nil {
@@ -98,23 +117,28 @@ func addOrchestratorCommands(c *container) error {
98117 }
99118 owner := c .cfg .GithubOwner
100119 repo := c .cfg .GithubRepo
101- fmt .Printf ("GitHub configuration: owner=%s, repo=%s, token_source=%s, token_present=%t, token_length=%d\n " ,
102- owner , repo , tokenSource , token != "" , len (token ))
120+ log .Info ("GitHub configuration" ,
121+ zap .String ("owner" , owner ),
122+ zap .String ("repo" , repo ),
123+ zap .String ("token_source" , tokenSource ),
124+ zap .Bool ("token_present" , token != "" ),
125+ zap .Int ("token_length" , len (token )),
126+ )
103127 if owner == "" || repo == "" {
104128 return fmt .Errorf ("github owner/repo not configured; set GITHUB_REPOSITORY or config values" )
105129 }
106130 var githubExtRepo repository.GithubExtendedRepository
107131 if token == "" {
108- fmt . Fprintln ( os . Stderr , "GitHub token not provided; GitHub operations will be skipped" )
132+ log . Warn ( "GitHub token not provided; GitHub operations will be skipped" )
109133 githubExtRepo = repository .NewGithubNoopExtendedRepository (owner , repo )
110134 } else {
111- fmt . Printf ("Initializing GitHub extended repository with token (length=%d) \n " , len (token ))
135+ log . Info ("Initializing GitHub extended repository" , zap . Int ( "token_length" , len (token ) ))
112136 var err error
113137 githubExtRepo , err = repository .NewGithubExtendedRepository (token , owner , repo )
114138 if err != nil {
115139 return fmt .Errorf ("failed to initialize GitHub extended repository: %w" , err )
116140 }
117- fmt . Printf ( "Successfully initialized GitHub extended repository for %s/%s \n " , owner , repo )
141+ log . Info ( "Initialized GitHub extended repository" , zap . String ( "owner" , owner ), zap . String ( " repo" , repo ) )
118142 }
119143
120144 // Create PR Release orchestrator
0 commit comments