shindex.go (4022B)
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "strings" 9 10 "shroomgit/config" 11 ) 12 13 func main() { 14 args := os.Args[1:] 15 16 if len(args) == 0 { 17 fmt.Println("ERROR not enough arguments") 18 printHelp() 19 os.Exit(1) 20 } 21 22 basicPart() 23 gitTable(args) 24 fmt.Println("\n</body>") 25 fmt.Print("</html>") 26 } 27 28 func gitTable(args []string) { 29 fmt.Println("<div style=\"display:flex; flex-wrap:wrap; gap;9px;\">") 30 31 for _, repo := range args { 32 repoName := getRepoName(repo) 33 cmd := exec.Command( 34 "git", 35 "-C", repo, 36 "log", "-1", 37 "--pretty=format:%cd", 38 "--date=format:%Y-%m-%d", 39 ) 40 descFile := filepath.Join(repo, ".git", "description") 41 desc := "" 42 if data, err := os.ReadFile(descFile); err == nil { 43 desc = string(data) 44 } 45 lastCommit, _ := cmd.Output() 46 license := detectLicense(repo) 47 48 fmt.Println("\n<div style=\"width:400px;\">") 49 fmt.Println("<div style=\"display:flex;padding:5px;\">") 50 fmt.Println("<div style=\"width:32px;height:32px;margin-right:8px;\">") 51 52 // the line below assumes the directory path is the same as the repo's name, not arguments 53 // change the 'repoName' to 'repo' if you want the latter option 54 fmt.Printf("<a href=\"%s/log.html\" style=\"color:inherit\">", repoName) 55 56 fmt.Println("<svg width=\"32\" height=\"32\">") 57 fmt.Println("<rect x=\"6\" y=\"10\" width=\"20\" height=\"20\"") 58 fmt.Println("fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" transform=\"rotate(45 20 20)\"/></svg></a></div><div>") 59 60 // other instance of possible path argument issue 61 fmt.Printf("<a href=\"%s/log.html\" style=\"line-height:1.5;\">", repoName) 62 63 fmt.Printf("<b>%s", repoName) 64 fmt.Printf("</b></a><br>%s", desc) 65 fmt.Printf("<br><br>%s", string(lastCommit)) 66 if license != "0" { 67 fmt.Printf(" - %s", license) 68 } 69 70 fmt.Println("</div></div></div>") 71 } 72 73 fmt.Println("</div>") 74 } 75 76 func basicPart() { 77 config := config.Shindex 78 fmt.Println("<!DOCTYPE html>") 79 fmt.Println("<html>") 80 fmt.Println("<head>") 81 fmt.Printf("<title>%s</title>\n", config.Title) 82 fmt.Println("<link rel=\"stylesheet\" href=\"style.css\" \\>") 83 fmt.Println("<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\" \\>") 84 fmt.Println("</head>\n") 85 fmt.Println("<body>") 86 fmt.Println("<table>") 87 fmt.Printf("<tr><td><a href=\"%s\">", config.LogoHref) 88 fmt.Printf("<img src=\"%s\" ", config.Logo) 89 fmt.Printf("alt=\"\" width=%d ", config.LogoWidth) 90 fmt.Printf("height=%d ", config.LogoHeight) 91 fmt.Print("/></a></td>\n") 92 fmt.Printf("<td>%s", config.Title) 93 fmt.Println("</td></tr>") 94 fmt.Println("</table>") 95 fmt.Println(config.Desc) 96 fmt.Println("<hr>") 97 } 98 99 func getRepoName(repo string) string { 100 gitConfig := filepath.Join(repo, ".git", "config") 101 data, err := os.ReadFile(gitConfig) 102 if err != nil { 103 return filepath.Base(repo) 104 } 105 106 content := string(data) 107 108 for _, line := range strings.Split(content, "\n") { 109 line = strings.TrimSpace(line) 110 if strings.HasPrefix(line, "url =") { 111 parts := strings.Fields(line) 112 url := parts[2] 113 url = strings.TrimSuffix(url, ".git") 114 segments := strings.Split(url, "/") 115 return segments[len(segments)-1] 116 } 117 } 118 119 return filepath.Base(repo) 120 } 121 122 // Not very reliable, will fix later 123 func detectLicense(repo string) string { 124 files := []string{"LICENSE", "LICENSE.txt", "LICENSE.md", "license"} 125 126 for _, f := range files { 127 path := filepath.Join(repo, f) 128 data, _ := os.ReadFile(path) 129 130 text := string(data) 131 132 switch { 133 case strings.Contains(text, "MIT License"): 134 return "MIT" 135 case strings.Contains(text, "Apache"): 136 return "Apache" 137 case strings.Contains(text, "GNU General"): 138 return "GPL" 139 case strings.Contains(text, "unlicense"): 140 return "unlicense" 141 case strings.Contains(text, "Mozilla"): 142 return "MPL" 143 case strings.Contains(text, "Creative Commons"): 144 return "CC" 145 case strings.Contains(text, "0BSD"), strings.Contains(text, "BSD-"): 146 return "BSD" 147 case strings.Contains(text, "license"): 148 return "other" 149 } 150 } 151 152 return "0" 153 } 154 155 func printHelp() { 156 fmt.Println("read source code for now") 157 }