ref: 20900950466bb8453e94868960262cf20b8d2bfb
./roadmap.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
package main import ( "errors" "fmt" "html" "io/fs" "log" "net/http" "os" "path/filepath" "strings" "github.com/rveen/ogdl" ) type Task struct { Path string Summary string Subtasks []Task } func main() { base := "" if len(os.Args) < 2 { // TODO error os.Exit(1) } else { base = os.Args[1] } base = filepath.Clean(base) router(base) } func router(base string) { http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { body := renderTasks(base, filepath.Clean(r.URL.Path)) rw.Write([]byte(body)) }) http.ListenAndServe(":8080", nil) } func renderTasks(base, path string) string { if path == "/" { t, err := root(base) if err != nil { log.Printf("while getting task %s: %v", path, err) return "" // TODO render error } log.Println(t) return "" // TODO render task } else { t, err := task(base, path) if err != nil { log.Printf("while getting task %s: %v", path, err) return "" // TODO render error } fmt.Println(t) return "" // TODO render task } } func root(base string) ([]Task, error) { tasks := []Task{} dirEntries, err := os.ReadDir(base) if err != nil { log.Printf("while reading dir: %v", err) os.Exit(1) } for _, dirEntry := range dirEntries { if !dirEntry.IsDir() { task, err := readTask(base, dirEntry.Name()) if err != nil { return []Task{}, fmt.Errorf("while reading dir: %v", err) } tasks = append(tasks, task) } } return tasks, nil } func task(base, path string) (Task, error) { task, err := readTask(base, path+".ogdl") if err != nil { return Task{}, fmt.Errorf("while reading task %s: %v", path, err) } dirEntries, err := os.ReadDir(filepath.Join(base, path)) if err != nil { if errors.Is(err, fs.ErrNotExist) { return task, nil } return Task{}, fmt.Errorf("while reading dir: %v", err) } for _, dirEntry := range dirEntries { if !dirEntry.IsDir() { subtask, err := readTask(base, filepath.Join(path, dirEntry.Name())) if err != nil { return Task{}, fmt.Errorf("while reading subtask %s: %v", path, err) } task.Subtasks = append(task.Subtasks, subtask) } } return task, nil } func readTask(base, path string) (Task, error) { g := ogdl.FromFile(filepath.Join(base, path)) if g == nil { return Task{}, fmt.Errorf("no such task: %s", path) } return Task{ Path: strings.ReplaceAll(path, ".ogdl", ""), Summary: g.String(), }, nil } |