refactor: fixed in-line documentation and added line-wraps where needed

This commit is contained in:
ChrisERo 2022-01-10 19:00:37 -05:00 committed by chrrodri
parent f67669662b
commit 0df6e005d3
2 changed files with 20 additions and 14 deletions

View File

@ -174,9 +174,9 @@ var (
}, []string{"status"}) }, []string{"status"})
) )
// Channels for ensuring hooks execute at least once before terminating when --one-time flag is set. // Channels for ensuring hooks execute at least once before terminating when
// Should be nil if and only if corresponding hook is not defined and if initialised, will only every get written // --one-time flag is set. Should be nil if and only if corresponding hook is
// to once. // not defined and if initialised, will only ever get written to once.
var exechookChannel, webhookChannel chan bool var exechookChannel, webhookChannel chan bool
const ( const (
@ -475,8 +475,8 @@ func main() {
run: cmdRunner, run: cmdRunner,
} }
// This context is used only for git credentials initialization. There are no long-running operations like // This context is used only for git credentials initialization. There are
// `git clone`, so hopefully 30 seconds will be enough. // no long-running operations like `git clone`, so hopefully 30 seconds will be enough.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
if *flUsername != "" { if *flUsername != "" {
@ -638,9 +638,11 @@ func main() {
if initialSync { if initialSync {
// Determine if git-sync should terminate for one of several reasons // Determine if git-sync should terminate for one of several reasons
if *flOneTime { if *flOneTime {
// Wait for hooks to complete at least once, if not nil, before checking whether to stop program // Wait for hooks to complete at least once, if not nil, before
// Assumes that if hook channels are not nil, they will have at least one value before getting closed // checking whether to stop program.
exitCode := 0 // if all hooks succeeded, exit code is 0, else set to 1 // 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 { if exechookChannel != nil {
exechookChannelFinishedSuccessfully := <-exechookChannel exechookChannelFinishedSuccessfully := <-exechookChannel
if !exechookChannelFinishedSuccessfully { if !exechookChannelFinishedSuccessfully {

View File

@ -96,8 +96,10 @@ type HookRunner struct {
data *hookData data *hookData
// Logger // Logger
logger *logging.Logger logger *logging.Logger
// hasCompletedOnce is used to send true if and only if first run executed successfully and false otherwise to some receiver. // hasCompletedOnce is used to send true if and only if first run executed
// should be initialised to a buffered channel of size 1. Is only meant to be used within sendCompletedOnceMessageIfApplicable // 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 hasCompletedOnce chan bool
} }
@ -126,7 +128,7 @@ func (r *HookRunner) Run(ctx context.Context) {
if err := r.hook.Do(ctx, hash); err != nil { if err := r.hook.Do(ctx, hash); err != nil {
r.logger.Error(err, "hook failed") r.logger.Error(err, "hook failed")
updateHookRunCountMetric(r.hook.Name(), "error") 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) r.sendCompletedOnceMessageIfApplicable(false)
time.Sleep(r.backoff) time.Sleep(r.backoff)
} else { } 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 // If hasCompletedOnce is nil, does nothing. Otherwise, forwards the caller
// HookRunner to receivers of hasCompletedOnce, closes said chanel, and sets hasCompletedOnce to nil. // provided success status (as a boolean) of HookRunner to receivers of
// Using this function to write to hasCompletedOnce ensures it is only every written to once. // 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) { func (r *HookRunner) sendCompletedOnceMessageIfApplicable(completedSuccessfully bool) {
if r.hasCompletedOnce != nil { if r.hasCompletedOnce != nil {
r.hasCompletedOnce <- completedSuccessfully r.hasCompletedOnce <- completedSuccessfully