github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/internal/job/parseQuery.go (about)

     1  package job
     2  
     3  import (
     4  	"net/url"
     5  	"errors"
     6  	"text/template"
     7  	"strconv"
     8  )
     9  
    10  // ParseQuery takes url.Values and loads the template and resources referenced in q into the root directory.
    11  func (j *Job) ParseQuery(q url.Values, cache *TemplateCache) error {
    12  	cOpts := j.Opts
    13  
    14  	// Check if a registered template is being requested in the URL, if so make sure its available on the local disk
    15  	if tmplID := q.Get("tmpl"); j.Template == nil && tmplID != "" {
    16  		tmplID = tmplID + cOpts.Delims.Left + cOpts.Delims.Right
    17  		cache.Lock()
    18  
    19  		ti, exists := cache.Get(tmplID)
    20  		if !exists {
    21  			// Look for the requested template in the source chain and parse it
    22  			if err := j.GetTemplate(q.Get("tmpl")); err != nil {
    23  				cache.Unlock()
    24  				return err
    25  			}
    26  
    27  			cache.Add(tmplID, j.Template)
    28  		} else {
    29  			j.Template = ti.(*template.Template)
    30  		}
    31  		cache.Unlock()
    32  	} else if j.Template == nil {
    33  		return errors.New("no template provided")
    34  	}
    35  	// Finish setting up the template
    36  	if omk := q.Get("onMissingKey"); omk != "" && cOpts.OnMissingKey == "" {
    37  		cOpts.OnMissingKey = MissingKeyOpt(omk)
    38  		if omk := cOpts.OnMissingKey; !omk.IsValid() {
    39  			return errors.New("invalid onMissingKey field found in JSON body")
    40  		}
    41  	}
    42  
    43  	// handle linking resources into the working directory, downloading those that aren't in the root directory
    44  	rscsIDs := q["rsc"]
    45  	j.AddResource(rscsIDs...)
    46  
    47  	// Load and parse details json from local disk, downloading it from the db if not found on local disk
    48  	if dtID := q.Get("dtls"); len(j.Details) == 0 && dtID != "" {
    49  		if err := j.GetDetails(dtID); err != nil {
    50  			return err
    51  		}
    52  	}
    53  
    54  	// finish configuring compilation options
    55  	if cOpts.CC == "" {
    56  		cOpts.CC = Compiler(q.Get("compiler"))
    57  	}
    58  	if cOpts.N < 2 {
    59  		if n, err := strconv.Atoi(q.Get("count")); err == nil {
    60  			cOpts.N = uint(n)
    61  		}
    62  	}
    63  
    64  	// Set the job options
    65  	j.Opts = cOpts
    66  
    67  	return nil
    68  }