github.com/vulppine/fotoDen@v0.3.0/generator/generator.go (about)

     1  package generator
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"path"
     9  )
    10  
    11  // Structs for fotoDen JSON files
    12  //
    13  // Structs for usage with fotoDen JSON files.
    14  
    15  // Config represents the configuration for fotoDen's generator, and where
    16  // images will go, as well as what sizes will be generated.
    17  // ImageSizes is a map with string keys containing ImageScale structs, which dictate
    18  // how images will be resized.
    19  type Config struct {
    20  	ImageRootDirectory string // where all images are stored (default: img)
    21  	ImageSrcDirectory  string // where all source images are stored (default: ImageRootDirectory/src)
    22  	ImageMetaDirectory string // where all meta files per image are stored (default: ImageRootDirectory/meta)
    23  	ImageSizes         map[string]ImageScale
    24  	WebSourceLocation  string // where all html/css/js files are stored for fotoDen's functionality
    25  	WebBaseURL         string // what the base URL is (aka, fotoDen's location)
    26  }
    27  
    28  // some defaults in case we never have a fotoDen config file opened
    29  
    30  var userConfigDir, _ = os.UserConfigDir()
    31  
    32  // RootConfigDir is where the configuration files are stored.
    33  var RootConfigDir = path.Join(userConfigDir, "fotoDen")
    34  
    35  // DefaultConfig contains a template for fotoDen to use.
    36  // TODO: Move this to some kind of GeneratorConfig generator.
    37  var DefaultConfig Config = Config{
    38  	ImageRootDirectory: "img",
    39  	ImageMetaDirectory: "meta",
    40  	ImageSizes: map[string]ImageScale{
    41  		"small":  ImageScale{ScalePercent: 0.25},
    42  		"medium": ImageScale{ScalePercent: 0.5},
    43  		"large":  ImageScale{ScalePercent: 0.75},
    44  	},
    45  	ImageSrcDirectory: "src",
    46  	WebBaseURL:        "", // this should be set during configuration generation
    47  }
    48  
    49  // CurrentConfig represents the current generator config, and can be used as reference
    50  // for any package that calls fotoDen/generator.
    51  var CurrentConfig Config
    52  
    53  // WorkingDirectory is the current working directory that fotoDen was started in.
    54  var WorkingDirectory, _ = os.Getwd()
    55  
    56  // Verbose is used toggle verbose statements - when toggled, prints what the generator is doing to console.
    57  var Verbose bool // if this is set, everything important is printed
    58  
    59  func verbose(print string) {
    60  	if Verbose {
    61  		log.Println(print)
    62  	}
    63  }
    64  
    65  // WriteJSON writes a struct as a JSON file to a specified pathname.
    66  // Takes a filepath, a "mode", and an interface containing something that translates to valid JSON according to encoding/json.
    67  // Mode toggles between non-indented JSON, and indented JSON.
    68  // Returns an error if any occur.
    69  func WriteJSON(filePath string, mode string, iface interface{}) error {
    70  	file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	var toWrite []byte
    76  
    77  	switch mode {
    78  	case "single":
    79  		toWrite, err = json.Marshal(iface)
    80  		if err != nil {
    81  			return err
    82  		}
    83  	case "multi":
    84  		toWrite, err = json.MarshalIndent(iface, "", "\t")
    85  		if err != nil {
    86  			return err
    87  		}
    88  	}
    89  
    90  	_, err = file.Write(toWrite)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  // ReadJSON reads a JSON file from a pathname, and puts it into the specified interface.
    99  // Returns an error if any occur.
   100  func ReadJSON(filePath string, iface interface{}) error {
   101  	file, err := ioutil.ReadFile(filePath)
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	err = json.Unmarshal(file, iface)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	return nil
   112  }
   113  
   114  // OpenfotoDenConfig sets the current fotoDen generator configuration to this
   115  func OpenConfig(configLocation string) error {
   116  	err := ReadJSON(configLocation, &CurrentConfig)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  // WritefotoDenConfig attempts to write CurrentConfig to a new file at configLocation.
   125  func WriteConfig(config Config, configLocation string) error {
   126  	err := WriteJSON(configLocation, "multi", config)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  // RemoveItemFromStringArray removes an item from a string array at O(n) speed.
   135  func RemoveItemFromStringArray(array []string, item string) []string {
   136  	verbose("Attempting to remove " + item + " from an array.")
   137  	newArray := make([]string, 0)
   138  
   139  	for i := 0; i < len(array); i++ {
   140  		if array[i] != item {
   141  			newArray = append(newArray, array[i])
   142  		}
   143  	}
   144  
   145  	return newArray
   146  }