1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "html/template" 7 "io" 8 "os" 9 "strings" 10 11 "vimagination.zapto.org/parser" 12 ) 13 14 var ( 15 fMap = template.FuncMap{ 16 "add": func(a, b int) int { 17 return a + b 18 }, 19 "mul": func(a, b int) int { 20 return a * b 21 }, 22 "sub": func(a, b int) int { 23 return a - b 24 }, 25 "repeat": strings.Repeat, 26 "split": strings.Split, 27 } 28 config = struct { 29 ReposDir string `json:"reposDir"` 30 OutputDir string `json:"outputDir"` 31 Pinned []string `json:"pinned"` 32 GitDir string `json:"gitDir"` 33 IndexFile string `json:"indexFile"` 34 IndexTemplate string `json:"indexTemplate"` 35 IndexTemplateFile string `json:"indexTemplateFile"` 36 RepoTemplate string `json:"repoTemplate"` 37 RepoTemplateFile string `json:"repoTemplateFile"` 38 PrettyPrint []string `json:"prettyPrint"` 39 PrettyTemplate string `json:"prettyTemplate"` 40 PrettyTemplateFile string `json:"prettyTemplateFile"` 41 indexTemplate, repoTemplate, prettyTemplate *template.Template 42 prettyMap map[string]parser.TokenFunc 43 }{ 44 ReposDir: "./", 45 OutputDir: ".", 46 GitDir: ".git", 47 IndexFile: "index.html", 48 prettyMap: make(map[string]parser.TokenFunc), 49 } 50 prettyPrinters = map[string]parser.TokenFunc{ 51 ".go": commentsPlain, 52 } 53 ) 54 55 func readConfig(configFile string) error { 56 f, err := os.Open(configFile) 57 if err != nil { 58 if os.IsNotExist(err) { 59 return nil 60 } 61 return fmt.Errorf("error while opening config file: %w", err) 62 } 63 defer f.Close() 64 if err := json.NewDecoder(f).Decode(&config); err != nil { 65 return fmt.Errorf("error parsing config file: %w", err) 66 } 67 if config.IndexTemplateFile != "" { 68 f, err := os.Open(config.IndexTemplateFile) 69 if err != nil { 70 return fmt.Errorf("error opening index template file: %w", err) 71 } 72 b, err := io.ReadAll(f) 73 f.Close() 74 if err != nil { 75 return fmt.Errorf("error reading index template file: %w", err) 76 } 77 config.IndexTemplate = string(b) 78 } 79 if config.RepoTemplateFile != "" { 80 f, err := os.Open(config.RepoTemplateFile) 81 if err != nil { 82 return fmt.Errorf("error opening repo template file: %w", err) 83 } 84 b, err := io.ReadAll(f) 85 f.Close() 86 if err != nil { 87 return fmt.Errorf("error reading repo template file: %w", err) 88 } 89 config.RepoTemplate = string(b) 90 } 91 if config.PrettyTemplateFile != "" { 92 f, err := os.Open(config.PrettyTemplateFile) 93 if err != nil { 94 return fmt.Errorf("error opening pretty template file: %w", err) 95 } 96 b, err := io.ReadAll(f) 97 f.Close() 98 if err != nil { 99 return fmt.Errorf("error reading pretty template file: %w", err) 100 } 101 config.PrettyTemplate = string(b) 102 } 103 if config.indexTemplate, err = template.New("index").Funcs(fMap).Parse(config.IndexTemplate); err != nil { 104 return fmt.Errorf("error parsing index template: %w", err) 105 } 106 if config.repoTemplate, err = template.New("repo").Funcs(fMap).Parse(config.RepoTemplate); err != nil { 107 return fmt.Errorf("error parsing repo template: %w", err) 108 } 109 if config.prettyTemplate, err = template.New("pretty").Funcs(fMap).Parse(config.PrettyTemplate); err != nil { 110 return fmt.Errorf("error parsing pretty template: %w", err) 111 } 112 for _, printer := range config.PrettyPrint { 113 if p, ok := prettyPrinters[printer]; ok { 114 config.prettyMap[printer] = p 115 } 116 } 117 return nil 118 } 119