@@ -19,44 +19,12 @@ import (
1919 "scbake/pkg/templates"
2020)
2121
22- // Define constants for step logging and cyclomatic complexity reduction
23- const (
24- runApplyTotalSteps = 5 // Now 5 steps, as git steps are part of template logic
25- langApplyTotalSteps = 5
26- )
27-
28- // StepLogger helps print consistent step messages
29- type StepLogger struct {
30- currentStep int
31- totalSteps int // Keep unexported
32- DryRun bool
33- }
34-
35- // NewStepLogger creates a new StepLogger instance.
36- func NewStepLogger (totalSteps int , dryRun bool ) * StepLogger {
37- return & StepLogger {totalSteps : totalSteps , DryRun : dryRun }
38- }
39-
40- // Log prints the current step message.
41- func (l * StepLogger ) Log (emoji , message string ) {
42- l .currentStep ++
43- if l .DryRun && l .currentStep > 2 {
44- return
45- }
46- fmt .Printf ("[%d/%d] %s %s\n " , l .currentStep , l .totalSteps , emoji , message )
47- }
48-
49- // SetTotalSteps updates the total number of steps for logging.
50- func (l * StepLogger ) SetTotalSteps (newTotal int ) {
51- l .totalSteps = newTotal
52- }
53-
54- // RunContext holds all the flags and args for a run.
22+ // RunContext holds the flags and arguments for a single execution run.
5523type RunContext struct {
5624 LangFlag string
5725 WithFlag []string
58- TargetPath string // Used for execution (absolute path)
59- ManifestPathArg string // Used for manifest/template rendering (relative path)
26+ TargetPath string // Absolute path for execution stability.
27+ ManifestPathArg string // Relative path for manifest portability.
6028 DryRun bool
6129 Force bool
6230}
@@ -67,11 +35,9 @@ type manifestChanges struct {
6735 Templates []types.Template
6836}
6937
70- // RunApply is the main logic for the 'apply' command, extracted.
71- func RunApply (rc RunContext ) error {
72- logger := NewStepLogger (runApplyTotalSteps , rc .DryRun )
73-
74- logger .Log ("📖" , "Loading manifest (" + fileutil .ManifestFileName + ")..." )
38+ // RunApply orchestrates the template application process.
39+ func RunApply (rc RunContext , reporter types.Reporter ) error {
40+ reporter .Step ("📖" , "Loading manifest (" + fileutil .ManifestFileName + ")..." )
7541
7642 // 1. Root Discovery & Manifest Load
7743 m , rootPath , err := manifest .Load (rc .TargetPath )
@@ -101,7 +67,7 @@ func RunApply(rc RunContext) error {
10167 }()
10268 }
10369
104- logger . Log ("📝" , "Building execution plan..." )
70+ reporter . Step ("📝" , "Building execution plan..." )
10571
10672 // Deduplicate requested templates to ensure idempotency
10773 rc .WithFlag = deduplicateTemplates (rc .WithFlag )
@@ -127,34 +93,27 @@ func RunApply(rc RunContext) error {
12793 }
12894
12995 if rc .DryRun {
130- fmt .Println ("DRY RUN: No changes will be made." )
131- fmt .Println ("Plan contains the following tasks:" )
132- return Execute (plan , tc )
96+ return Execute (plan , tc , reporter )
13397 }
13498
135- // 3. Execute and Finalize
136- // We pass the transaction and paths down.
137- return executeAndFinalize (logger , plan , tc , m , changes , rootPath , tx )
99+ return executeAndFinalize (reporter , plan , tc , m , changes , rootPath , tx )
138100}
139101
140- // executeAndFinalize runs the plan, updates manifest, and commits the transaction.
141102func executeAndFinalize (
142- logger * StepLogger ,
103+ reporter types. Reporter ,
143104 plan * types.Plan ,
144105 tc types.TaskContext ,
145106 m * types.Manifest ,
146107 changes * manifestChanges ,
147108 rootPath string ,
148109 tx * transaction.Manager ,
149110) error {
150- logger .Log ("🚀" , "Executing plan..." )
151-
152- // Run all tasks. They will auto-track changes via tc.Tx.
153- if err := Execute (plan , tc ); err != nil {
154- return fmt .Errorf ("task execution failed: %w" , err )
111+ reporter .Step ("🚀" , "Executing plan..." )
112+ if err := Execute (plan , tc , reporter ); err != nil {
113+ return err
155114 }
156115
157- logger . Log ("✍️" , "Updating manifest..." )
116+ reporter . Step ("✍️" , "Updating manifest..." )
158117 updateManifest (m , changes )
159118
160119 // We track the manifest file itself before saving.
@@ -169,7 +128,7 @@ func executeAndFinalize(
169128 return fmt .Errorf ("manifest save failed: %w" , err )
170129 }
171130
172- logger . Log ("✅" , "Committing transaction..." )
131+ reporter . Step ("✅" , "Committing transaction..." )
173132 // Point of No Return: We delete the backups.
174133 if err := tx .Commit (); err != nil {
175134 return fmt .Errorf ("failed to commit transaction: %w" , err )
@@ -221,7 +180,7 @@ func buildPlan(rc RunContext) (*types.Plan, string, *manifestChanges, error) {
221180 plan := & types.Plan {Tasks : []types.Task {}}
222181 changes := & manifestChanges {}
223182 commitMessage := "scbake: Apply templates"
224- didSomething := false // Flag to ensure at least one action is requested
183+ didSomething := false
225184
226185 if rc .LangFlag != "" {
227186 didSomething = true
@@ -245,7 +204,7 @@ func buildPlan(rc RunContext) (*types.Plan, string, *manifestChanges, error) {
245204
246205 // Only fail if neither a language nor tooling was specified.
247206 if ! didSomething {
248- return nil , "" , nil , errors .New ("no language or templates specified. Use --lang or --with " )
207+ return nil , "" , nil , errors .New ("no language or templates specified" )
249208 }
250209
251210 return plan , commitMessage , changes , nil
@@ -281,7 +240,6 @@ func handleLangFlag(rc RunContext, plan *types.Plan, changes *manifestChanges) (
281240
282241 plan .Tasks = append (plan .Tasks , langTasks ... )
283242
284- // Sanitize the project name for the manifest
285243 projectName , err := util .SanitizeModuleName (rc .ManifestPathArg )
286244 if err != nil {
287245 return "" , fmt .Errorf ("could not determine project name: %w" , err )
0 commit comments