golift.io/starr@v1.0.0/starrcmd/config.go (about)

     1  package starrcmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"golift.io/starr"
     8  )
     9  
    10  /* Notes to future developers of this module:
    11  - Counters should be int.
    12  - IDs should be int64.
    13  - Sizes should be int64 (bytes).
    14  - Avoid uint* int8, int16, int32, float32, or add parsers for them. See golift.io/cnfg.
    15  - Avoid external modules for env parsing; those require custom types.
    16  - Some slices are allowed, add more when needed. See parseSlices().
    17    - Slices must have a split character. ,, or ,| (usually).
    18    - Missing the split character will cause a panic() during parsing.
    19    - Add tests for all methods and data types to catch panics before release.
    20  - The time.Time format is hard coded (twice). If new formats arise, find a way to fix it.
    21  - No time.Duration types exist, but we can write a parser for those if they arise.
    22  */
    23  
    24  var (
    25  	// ErrInvalidEvent is returned if you invoke a procedure for the wrong event.
    26  	ErrInvalidEvent = fmt.Errorf("incorrect event type requested")
    27  	// ErrNoEventFound is returned if an event type is not found.
    28  	// This should only happen when testing and you forget a variable.
    29  	ErrNoEventFound = fmt.Errorf("no eventType environment variable found")
    30  )
    31  
    32  // DateFormat matches the date output from most apps.
    33  const DateFormat = "1/2/2006 3:04:05 PM"
    34  
    35  // DateFormat2 matches the date output from Readarr.
    36  const DateFormat2 = "01/02/2006 15:04:05"
    37  
    38  // Event is a custom type to hold our EventType.
    39  type Event string
    40  
    41  // This list of constants represents all available and existing Event Types for all five Starr apps.
    42  // Lidarr is complete; 1/30/2022.
    43  // Prowlarr is complete; 1/30/2022.
    44  // Radarr is complete; 1/30/2022.
    45  // Readarr is complete; 1/30/2022.
    46  // Sonarr is complete; 1/30/2022.
    47  const (
    48  	EventTest              Event = "Test"              // All Apps, useless
    49  	EventHealthIssue       Event = "HealthIssue"       // All Apps
    50  	EventApplicationUpdate Event = "ApplicationUpdate" // All Apps
    51  	EventGrab              Event = "Grab"              // All Apps except Prowlarr
    52  	EventRename            Event = "Rename"            // All Apps except Prowlarr
    53  	EventDownload          Event = "Download"          // All Apps except Prowlarr/Lidarr
    54  	EventTrackRetag        Event = "TrackRetag"        // Lidarr & Readarr
    55  	EventAlbumDownload     Event = "AlbumDownload"     // Lidarr
    56  	EventMovieFileDelete   Event = "MovieFileDelete"   // Radarr
    57  	EventMovieDelete       Event = "MovieDelete"       // Radarr
    58  	EventBookDelete        Event = "BookDelete"        // Readarr
    59  	EventAuthorDelete      Event = "AuthorDelete"      // Readarr
    60  	EventBookFileDelete    Event = "BookFileDelete"    // Readarr
    61  	EventSeriesDelete      Event = "SeriesDelete"      // Sonarr
    62  	EventEpisodeFileDelete Event = "EpisodeFileDelete" // Sonarr
    63  )
    64  
    65  // CmdEvent holds the current event type and the app that triggered it.
    66  // Get one of these by calling New().
    67  type CmdEvent struct {
    68  	App  starr.App
    69  	Type Event
    70  }
    71  
    72  // New returns the current Event and Application it's from, or an error if the type doesn't exist.
    73  // When running from a Starr App Custom Script this should not return an error.
    74  func New() (*CmdEvent, error) {
    75  	for _, cmdEvent := range []*CmdEvent{
    76  		{starr.Radarr, Event(os.Getenv("radarr_eventtype"))},
    77  		{starr.Sonarr, Event(os.Getenv("sonarr_eventtype"))},
    78  		{starr.Lidarr, Event(os.Getenv("lidarr_eventtype"))},
    79  		{starr.Readarr, Event(os.Getenv("readarr_eventtype"))},
    80  		{starr.Prowlarr, Event(os.Getenv("prowlarr_eventtype"))},
    81  	} {
    82  		if cmdEvent.Type != "" {
    83  			return cmdEvent, nil
    84  		}
    85  	}
    86  
    87  	return nil, ErrNoEventFound
    88  }
    89  
    90  // NewMust returns a command event without returning an error. It will panic if the event does not exist.
    91  // When running from a Starr App Custom Script this should not panic.
    92  func NewMust() *CmdEvent {
    93  	cmdEvent, _ := New()
    94  	if cmdEvent == nil {
    95  		panic(ErrNoEventFound)
    96  	}
    97  
    98  	return cmdEvent
    99  }
   100  
   101  // NewMustNoPanic returns a command event and allows your code to handle the problem.
   102  // When running from a Starr App Custom Script this should always return a proper event.
   103  func NewMustNoPanic() *CmdEvent {
   104  	cmdEvent, _ := New()
   105  	if cmdEvent == nil {
   106  		return &CmdEvent{}
   107  	}
   108  
   109  	return cmdEvent
   110  }