github.com/go-spatial/go-wfs@v0.1.4-0.20190401000911-c9fba2bb5188/util/util.go (about)

     1  ///////////////////////////////////////////////////////////////////////////////
     2  //
     3  // The MIT License (MIT)
     4  // Copyright (c) 2018 Jivan Amara
     5  // Copyright (c) 2018 Tom Kralidis
     6  // Copyright (c) 2018 James Lucktaylor
     7  //
     8  // Permission is hereby granted, free of charge, to any person obtaining a copy
     9  // of this software and associated documentation files (the "Software"), to
    10  // deal in the Software without restriction, including without limitation the
    11  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    12  // sell copies of the Software, and to permit persons to whom the Software is
    13  // furnished to do so, subject to the following conditions:
    14  //
    15  // The above copyright notice and this permission notice shall be included in
    16  // all copies or substantial portions of the Software.
    17  //
    18  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    19  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21  // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    22  // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    23  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
    24  // USE OR OTHER DEALINGS IN THE SOFTWARE.
    25  //
    26  ///////////////////////////////////////////////////////////////////////////////
    27  
    28  package util
    29  
    30  import (
    31  	"bytes"
    32  	"html/template"
    33  	"log"
    34  	"os"
    35  	"path"
    36  	"path/filepath"
    37  	"sort"
    38  )
    39  
    40  // DefaultGpkg is used when a data source isn't provided.
    41  // Scans for files w/ '.gpkg' extension following logic described below, and chooses the first alphabetically.
    42  // Returns the absolute path as a string.
    43  //
    44  // 1) scan the current working directory
    45  // 2) if none found in the working directory and it exists, scan ./data/
    46  // 3) if none found in either of the previous and it exists, scan ./test_data/
    47  // 4) if none found, return an empty string
    48  func DefaultGpkg() string {
    49  	// Directories to check in decreasing order of priority
    50  	dirs := []string{"", "data", "test_data"}
    51  	gpkgPath := ""
    52  	wd, err := os.Getwd()
    53  	if err != nil {
    54  		log.Printf("Unable to get working directory: %v", err)
    55  		return ""
    56  	}
    57  
    58  	for _, dir := range dirs {
    59  		searchGlob := path.Join(dir, "*.gpkg")
    60  		gpkgFiles, err := filepath.Glob(searchGlob)
    61  		if err != nil {
    62  			panic("Invalid glob pattern hardcoded")
    63  		}
    64  		if len(gpkgFiles) == 0 {
    65  			continue
    66  		}
    67  		sort.Strings(gpkgFiles)
    68  		gpkgFilename := gpkgFiles[0]
    69  		gpkgPath = path.Clean(path.Join(wd, gpkgFilename))
    70  		break
    71  	}
    72  
    73  	if len(gpkgPath) == 0 {
    74  		return ""
    75  	} else {
    76  		return filepath.Clean(gpkgPath)
    77  	}
    78  }
    79  
    80  func RenderTemplate(templateString string, data map[string]interface{}) ([]byte, error) {
    81  	var tpl bytes.Buffer
    82  	t := template.New("template")
    83  	t, _ = t.Parse(templateString)
    84  
    85  	if err := t.Execute(&tpl, data); err != nil {
    86  		return tpl.Bytes(), err
    87  	}
    88  
    89  	return tpl.Bytes(), nil
    90  }