diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 05d909f..a71ac69 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -174,9 +174,9 @@ var ( }, []string{"status"}) ) -// Channels for ensuring hooks execute at least once before terminating when --one-time flag is set. -// Should be nil if and only if corresponding hook is not defined and if initialised, will only every get written -// to once. +// Channels for ensuring hooks execute at least once before terminating when +// --one-time flag is set. Should be nil if and only if corresponding hook is +// not defined and if initialised, will only ever get written to once. var exechookChannel, webhookChannel chan bool const ( @@ -475,8 +475,8 @@ func main() { run: cmdRunner, } - // This context is used only for git credentials initialization. There are no long-running operations like - // `git clone`, so hopefully 30 seconds will be enough. + // This context is used only for git credentials initialization. There are + // no long-running operations like `git clone`, so hopefully 30 seconds will be enough. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if *flUsername != "" { @@ -638,9 +638,11 @@ func main() { if initialSync { // Determine if git-sync should terminate for one of several reasons if *flOneTime { - // Wait for hooks to complete at least once, if not nil, before checking whether to stop program - // Assumes that if hook channels are not nil, they will have at least one value before getting closed - exitCode := 0 // if all hooks succeeded, exit code is 0, else set to 1 + // Wait for hooks to complete at least once, if not nil, before + // checking whether to stop program. + // Assumes that if hook channels are not nil, they will have at + // least one value before getting closed + exitCode := 0 // is 0 if all hooks succeed, else is 1 if exechookChannel != nil { exechookChannelFinishedSuccessfully := <-exechookChannel if !exechookChannelFinishedSuccessfully { diff --git a/pkg/hook/hook.go b/pkg/hook/hook.go index 86febea..06e68cd 100644 --- a/pkg/hook/hook.go +++ b/pkg/hook/hook.go @@ -96,8 +96,10 @@ type HookRunner struct { data *hookData // Logger logger *logging.Logger - // hasCompletedOnce is used to send true if and only if first run executed successfully and false otherwise to some receiver. - // should be initialised to a buffered channel of size 1. Is only meant to be used within sendCompletedOnceMessageIfApplicable + // hasCompletedOnce is used to send true if and only if first run executed + // successfully and false otherwise to some receiver. Should be + // initialised to a buffered channel of size 1. + // Is only meant for use within sendCompletedOnceMessageIfApplicable. hasCompletedOnce chan bool } @@ -126,7 +128,7 @@ func (r *HookRunner) Run(ctx context.Context) { if err := r.hook.Do(ctx, hash); err != nil { r.logger.Error(err, "hook failed") updateHookRunCountMetric(r.hook.Name(), "error") - // don't want to sleep unnecessarily if we are going to terminate anyways + // don't want to sleep unnecessarily terminating anyways r.sendCompletedOnceMessageIfApplicable(false) time.Sleep(r.backoff) } else { @@ -139,9 +141,11 @@ func (r *HookRunner) Run(ctx context.Context) { } } -// If hasCompletedOnce is nil, does nothing. Otherwise, forwards the caller provided success status (as a boolean) of -// HookRunner to receivers of hasCompletedOnce, closes said chanel, and sets hasCompletedOnce to nil. -// Using this function to write to hasCompletedOnce ensures it is only every written to once. +// If hasCompletedOnce is nil, does nothing. Otherwise, forwards the caller +// provided success status (as a boolean) of HookRunner to receivers of +// hasCompletedOnce, closes said chanel, and sets hasCompletedOnce to nil. +// Using this function to write to hasCompletedOnce ensures it is only ever +// written to once. func (r *HookRunner) sendCompletedOnceMessageIfApplicable(completedSuccessfully bool) { if r.hasCompletedOnce != nil { r.hasCompletedOnce <- completedSuccessfully