github.com/vulppine/fotoDen@v0.3.0/tool/tool.go (about) 1 // Package tool is a reference implementation for generation of fotoDen-based websites. 2 package tool 3 4 import ( 5 "io/ioutil" 6 "log" 7 "os" 8 "path/filepath" 9 10 "github.com/vulppine/fotoDen/generator" 11 ) 12 13 // WizardFlag specifies if fotoDen tool functions should have interactive input or not. 14 var WizardFlag bool 15 16 // NameFlag sets the name for a folder/album. If this is not set, fotoDen will automatically use the folder's name. 17 var NameFlag string 18 19 // Recurse toggles recursive functions on directories. This is primarily used for the update command. 20 var Recurse bool 21 22 // URLFlag sets the URL for functions that require a URL. This is mostly used in initialization. 23 var URLFlag string 24 25 // CurrentConfig represents the current site configuration file being used by fotoDen's tool. 26 var CurrentConfig *WebsiteConfig 27 28 // TODO: Find everything that uses the Genoptions global var and *refactor* 29 30 // GeneratorOptions is a set of options for the generator. 31 // 32 // Includes: 33 // - source 34 // - copy 35 // - thumb 36 // - large 37 // 38 // from the flags. 39 type GeneratorOptions struct { 40 Source string 41 Copy bool 42 Gensizes bool 43 ImageGen bool 44 Sort bool 45 Meta bool 46 Static bool 47 } 48 49 // Genoptions is a global variable for functions that use GeneratorOptions. 50 var Genoptions GeneratorOptions 51 52 func checkError(err error) bool { 53 if err != nil { 54 log.Println("An error occured during operation: ", err) 55 return true 56 } 57 58 return false 59 } 60 61 // Verbose toggles the verbosity of the command line tool. 62 var Verbose bool 63 64 func verbose(print string) { 65 if Verbose { 66 log.Println(print) 67 } 68 } 69 70 func fileCheck(filename string) bool { 71 _, err := os.Stat(filename) 72 return !os.IsNotExist(err) 73 } 74 75 type fvisitFunction func(string) error 76 77 // RecursiveVisit recursively visits folders, and performs a function 78 // inside of them. To ensure safety, this only works 79 // with fotoDen folders. 80 // 81 // It detects if a folder is a fotoDen folder in a lazy way, 82 // by seeing if a folder contains a folderInfo.json. 83 // If it does not, it terminates 84 // 85 // TODO: Replace this with the new fs library function in Go 1.16 86 func RecursiveVisit(folder string, fn fvisitFunction) error { 87 wd, err := os.Getwd() 88 if checkError(err) { 89 return err 90 } 91 92 if !fileCheck(filepath.Join(folder, "folderInfo.json")) { 93 return nil 94 } 95 96 err = fn(folder) 97 if checkError(err) { 98 return err 99 } 100 101 folders, err := ioutil.ReadDir(folder) 102 if checkError(err) { 103 return err 104 } 105 106 defer os.Chdir(wd) 107 108 err = os.Chdir(folder) 109 if checkError(err) { 110 return err 111 } 112 113 for _, f := range generator.GetArrayOfFolders(folders) { 114 err = RecursiveVisit(f, fn) 115 if checkError(err) { 116 return err 117 } 118 } 119 120 return nil 121 }