1+ // Package cli provides the command-line interface logic and orchestration for gitcommit.
12package cli
23
34import (
@@ -39,30 +40,61 @@ func (a *App) Run() error {
3940 slog .Debug ("Git repository detected" )
4041
4142 // Step 2: Get last commit date (if any)
42- var lastCommitDate * time.Time
43- if git .HasCommits () {
44- lastDate , err := git .GetLastCommitDate ()
45- if err != nil {
46- slog .Warn ("Could not retrieve last commit date" , "error" , err )
47- } else {
48- lastCommitDate = & lastDate
49- slog .Debug ("Last commit date retrieved" , "date" , lastDate )
50- }
51- } else {
43+ lastCommitDate := a .getLastCommitDate ()
44+
45+ // Step 3: Parse and validate the date
46+ parsedDate , err := a .parseAndValidateDate (request .InputDate , lastCommitDate )
47+ if err != nil {
48+ return err
49+ }
50+ request .ParsedDate = parsedDate
51+
52+ // Step 4: Format the date for Git
53+ gitFormattedDate := datetime .FormatForGit (parsedDate )
54+ request .GitFormattedDate = gitFormattedDate
55+ slog .Debug ("Date formatted for Git" , "formatted" , gitFormattedDate )
56+
57+ // Step 5: Execute the commit
58+ if err := git .ExecuteCommit (gitFormattedDate , request .CommitMessage ); err != nil {
59+ slog .Error ("Git commit failed" , "error" , err )
60+ return NewGitCommandError (err .Error ())
61+ }
62+
63+ // Step 6: Display success message
64+ fmt .Println (FormatSuccessMessage (gitFormattedDate ))
65+ slog .Info ("Commit created successfully" )
66+ return nil
67+ }
68+
69+ // getLastCommitDate retrieves the last commit date from the repository.
70+ func (a * App ) getLastCommitDate () * time.Time {
71+ if ! git .HasCommits () {
5272 slog .Debug ("No previous commits in repository" )
73+ return nil
5374 }
5475
55- // Step 3: Parse the date
56- parsedDate , err := datetime .ParseDate (request .InputDate )
76+ lastDate , err := git .GetLastCommitDate ()
77+ if err != nil {
78+ slog .Warn ("Could not retrieve last commit date" , "error" , err )
79+ return nil
80+ }
81+
82+ slog .Debug ("Last commit date retrieved" , "date" , lastDate )
83+ return & lastDate
84+ }
85+
86+ // parseAndValidateDate parses and validates the commit date.
87+ func (a * App ) parseAndValidateDate (dateStr string , lastCommitDate * time.Time ) (time.Time , error ) {
88+ // Parse the date
89+ parsedDate , err := datetime .ParseDate (dateStr )
5790 if err != nil {
5891 slog .Error ("Date parsing failed" , "error" , err )
59- return NewInvalidDateFormatError (request . InputDate )
92+ return time. Time {}, NewInvalidDateFormatError (dateStr )
6093 }
61- request .ParsedDate = parsedDate
6294
6395 slog .Debug ("Date parsed successfully" , "parsed" , parsedDate )
6496
65- // Step 4: Validate chronology
97+ // Validate chronology
6698 if lastCommitDate != nil {
6799 valid , errorType := datetime .ValidateChronology (parsedDate , lastCommitDate )
68100 if ! valid {
@@ -72,7 +104,7 @@ func (a *App) Run() error {
72104 "errorType" , errorType )
73105
74106 equal := errorType == "chronology_violation_equal"
75- return NewChronologyViolationError (
107+ return time. Time {}, NewChronologyViolationError (
76108 datetime .FormatForGit (parsedDate ),
77109 datetime .FormatForGit (* lastCommitDate ),
78110 equal ,
@@ -81,22 +113,5 @@ func (a *App) Run() error {
81113 }
82114
83115 slog .Debug ("Chronology validation passed" )
84-
85- // Step 5: Format the date for Git
86- gitFormattedDate := datetime .FormatForGit (parsedDate )
87- request .GitFormattedDate = gitFormattedDate
88-
89- slog .Debug ("Date formatted for Git" , "formatted" , gitFormattedDate )
90-
91- // Step 6: Execute the commit
92- if err := git .ExecuteCommit (gitFormattedDate , request .CommitMessage ); err != nil {
93- slog .Error ("Git commit failed" , "error" , err )
94- return NewGitCommandError (err .Error ())
95- }
96-
97- // Step 7: Display success message
98- fmt .Println (FormatSuccessMessage (gitFormattedDate ))
99-
100- slog .Info ("Commit created successfully" )
101- return nil
116+ return parsedDate , nil
102117}
0 commit comments