github.com/ngicks/gokugen@v0.0.5/cron/builder.go (about)

     1  package cron
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // Builder helps easy building of Row.
     8  type Builder struct {
     9  	cron Row
    10  }
    11  
    12  // Minute overwrites Minute field.
    13  // Passing zero arguments set it as wildcard.
    14  func (b Builder) Minute(in ...uint8) Builder {
    15  	minutes := Minutes([]Minute{})
    16  	for _, m := range in {
    17  		minutes = append(minutes, Minute(int(m)))
    18  	}
    19  	b.cron.Minute = &minutes
    20  	return b
    21  }
    22  
    23  // Hour overwrites Hour field.
    24  // Passing zero arguments set it as wildcard.
    25  func (b Builder) Hour(in ...uint8) Builder {
    26  	hours := Hours([]Hour{})
    27  	for _, m := range in {
    28  		hours = append(hours, Hour(int(m)))
    29  	}
    30  	b.cron.Hour = &hours
    31  	return b
    32  }
    33  
    34  // Day overwrites Day field.
    35  // Passing zero arguments set it as wildcard.
    36  func (b Builder) Day(in ...uint8) Builder {
    37  	days := Days([]Day{})
    38  	for _, m := range in {
    39  		days = append(days, Day(int(m)))
    40  	}
    41  	b.cron.Day = &days
    42  	return b
    43  }
    44  
    45  // Month overwrites Month field.
    46  // Passing zero arguments set it as wildcard.
    47  func (b Builder) Month(in ...time.Month) Builder {
    48  	months := Months([]time.Month{})
    49  	for _, m := range in {
    50  		months = append(months, m)
    51  	}
    52  	b.cron.Month = &months
    53  	return b
    54  }
    55  
    56  // WeekDay overwrites WeekDay field.
    57  // Passing zero arguments set it as wildcard.
    58  func (b Builder) WeekDay(in ...time.Weekday) Builder {
    59  	weekdays := Weekdays([]time.Weekday{})
    60  	for _, w := range in {
    61  		weekdays = append(weekdays, w)
    62  	}
    63  	b.cron.Weekday = &weekdays
    64  	return b
    65  }
    66  
    67  // Command overwrites Command field.
    68  func (b Builder) Command(in []string) Builder {
    69  	b.cron.Command = in
    70  	return b
    71  }
    72  
    73  func (b Builder) clearTime() Builder {
    74  	newRow := Row{}
    75  	newRow.Command = b.cron.Command
    76  	b.cron = newRow
    77  	return b
    78  }
    79  
    80  func (b Builder) Yearly(month []time.Month, day, hour, minute []uint8) Builder {
    81  	return b.clearTime().Month(month...).Day(day...).Hour(hour...).Minute(minute...)
    82  }
    83  
    84  func (b Builder) Monthly(day, hour, minute []uint8) Builder {
    85  	return b.clearTime().Day(day...).Hour(hour...).Minute(minute...)
    86  }
    87  
    88  func (b Builder) Weekly(weekDay []time.Weekday, hour, minute []uint8) Builder {
    89  	return b.clearTime().WeekDay(weekDay...).Hour(hour...).Minute(minute...)
    90  }
    91  
    92  func (b Builder) Daily(hour, minute []uint8) Builder {
    93  	return b.clearTime().Hour(hour...).Minute(minute...)
    94  }
    95  
    96  func (b Builder) Hourly(minute []uint8) Builder {
    97  	return b.clearTime().Minute(minute...)
    98  }
    99  
   100  // Reboot clears all time value fields.
   101  // This will make row as a once task.
   102  func (b Builder) Reboot() Builder {
   103  	return b.clearTime()
   104  }
   105  
   106  // Build builds Row.
   107  // No validation happens.
   108  func (b Builder) Build() Row {
   109  	return b.cron
   110  }