(templatePath, destPath string)
| 124 | } |
| 125 | |
| 126 | func generateFile(templatePath, destPath string) bool { |
| 127 | templateMap := template.FuncMap{ |
| 128 | "contains": contains, |
| 129 | "exists": exists, |
| 130 | "split": strings.Split, |
| 131 | "replace": strings.Replace, |
| 132 | "default": defaultValue, |
| 133 | "parseUrl": parseUrl, |
| 134 | "atoi": strconv.Atoi, |
| 135 | "add": add, |
| 136 | "isTrue": isTrue, |
| 137 | "lower": strings.ToLower, |
| 138 | "upper": strings.ToUpper, |
| 139 | "jsonQuery": jsonQuery, |
| 140 | "loop": loop, |
| 141 | } |
| 142 | |
| 143 | combinedFuncMap := sprig.TxtFuncMap() |
| 144 | for k, v := range templateMap { |
| 145 | combinedFuncMap[k] = v |
| 146 | } |
| 147 | tmpl := template.New(filepath.Base(templatePath)).Funcs(combinedFuncMap) |
| 148 | |
| 149 | if len(delims) > 0 { |
| 150 | tmpl = tmpl.Delims(delims[0], delims[1]) |
| 151 | } |
| 152 | tmpl, err := tmpl.ParseFiles(templatePath) |
| 153 | if err != nil { |
| 154 | log.Fatalf("unable to parse template: %s", err) |
| 155 | } |
| 156 | |
| 157 | // Don't overwrite destination file if it exists and no-overwrite flag passed |
| 158 | if _, err := os.Stat(destPath); err == nil && noOverwriteFlag { |
| 159 | return false |
| 160 | } |
| 161 | |
| 162 | dest := os.Stdout |
| 163 | if destPath != "" { |
| 164 | dest, err = os.Create(destPath) |
| 165 | if err != nil { |
| 166 | log.Fatalf("unable to create %s", err) |
| 167 | } |
| 168 | defer dest.Close() |
| 169 | } |
| 170 | |
| 171 | err = tmpl.ExecuteTemplate(dest, filepath.Base(templatePath), &Context{}) |
| 172 | if err != nil { |
| 173 | log.Fatalf("template error: %s\n", err) |
| 174 | } |
| 175 | |
| 176 | if fi, err := os.Stat(destPath); err == nil { |
| 177 | if err := dest.Chmod(fi.Mode()); err != nil { |
| 178 | log.Fatalf("unable to chmod temp file: %s\n", err) |
| 179 | } |
| 180 | if err := dest.Chown(int(fi.Sys().(*syscall.Stat_t).Uid), int(fi.Sys().(*syscall.Stat_t).Gid)); err != nil { |
| 181 | log.Fatalf("unable to chown temp file: %s\n", err) |
| 182 | } |
| 183 | } |
no outgoing calls
no test coverage detected