Skip to content

Commit 5ac3c59

Browse files
committed
refactor(config): pass project path explicitly to LoadConfig
- Changed 'LoadConfig' to accept 'projectPath' as a parameter for better flexibility. - Updated flag variable 'projectPath' to 'ProjectPath' to align with Go naming conventions. - Introduced 'InitializeFlags' function to parse command-line flags before execution. - Adjusted 'main.go' to call 'InitializeFlags' before loading configuration.
1 parent d315a92 commit 5ac3c59

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

cmd/review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func executeReview() {
2222
initialCommit = formatCommitHash(initialCommit)
2323

2424
// Convert relative path to absolute if needed
25-
absProjectPath, err := filepath.Abs(projectPath)
25+
absProjectPath, err := filepath.Abs(ProjectPath)
2626
if err != nil {
2727
fmt.Printf("Error resolving project path: %v\n", err)
2828
os.Exit(1)

cmd/root.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
initialCommit string
1414
finalCommit string
1515
mainBranch string
16-
projectPath string
16+
ProjectPath string
1717
outputDir string
1818

1919
// Application version
@@ -58,6 +58,12 @@ func SetConfig(cfg *config.Config) {
5858
appConfig = cfg
5959
}
6060

61+
// InitializeFlags parses command line flags before executing the main command
62+
func InitializeFlags() error {
63+
// Parse flags without executing the root command
64+
return rootCmd.ParseFlags(os.Args[1:])
65+
}
66+
6167
// Execute adds all child commands to the root command and sets flags
6268
func Execute() {
6369
if err := rootCmd.Execute(); err != nil {
@@ -71,7 +77,7 @@ func init() {
7177
rootCmd.Flags().StringVarP(&initialCommit, "initial", "i", "", "Starting commit hash for comparison (required)")
7278
rootCmd.Flags().StringVarP(&finalCommit, "final", "f", "HEAD", "Ending commit hash (defaults to HEAD)")
7379
rootCmd.Flags().StringVarP(&mainBranch, "main-branch", "m", "", "Main branch for refined comparisons")
74-
rootCmd.Flags().StringVarP(&projectPath, "project-path", "p", ".", "Project directory path")
80+
rootCmd.Flags().StringVarP(&ProjectPath, "project-path", "p", ".", "Project directory path")
7581
rootCmd.Flags().StringVarP(&outputDir, "output-dir", "o", "git-review", "Output directory for diff files")
7682

7783
// Mark initialCommit as required

internal/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Config struct {
2020
}
2121

2222
// LoadConfig reads configuration from files and environment variables
23-
func LoadConfig() (*Config, error) {
23+
func LoadConfig(projectPath string) (*Config, error) {
2424
v := viper.New()
2525

2626
// Set config name and type
@@ -40,7 +40,7 @@ func LoadConfig() (*Config, error) {
4040
v.AddConfigPath(filepath.Join(homeDir, "AppData", "Roaming", "git-review"))
4141

4242
// Local project config
43-
v.AddConfigPath(".")
43+
v.AddConfigPath(projectPath)
4444
v.SetConfigName(".gitreview")
4545

4646
// Read config

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ import (
1212
var AppVersion = "dev"
1313

1414
func main() {
15-
// Load configuration
16-
cfg, err := config.LoadConfig()
15+
// Initialize command flags before loading config
16+
if err := cmd.InitializeFlags(); err != nil {
17+
fmt.Printf("Error initializing flags: %v\n", err)
18+
os.Exit(1)
19+
}
20+
21+
// Load configuration with project path
22+
cfg, err := config.LoadConfig(cmd.ProjectPath)
1723
if err != nil {
1824
fmt.Printf("Error loading config: %v\n", err)
1925
os.Exit(1)

0 commit comments

Comments
 (0)