|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/bard-cli/bard-chat/pkg/chat" |
| 9 | + "github.com/bard-cli/bard-chat/pkg/man7" |
| 10 | + "github.com/bard-cli/bard-chat/pkg/recipe" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + outDirectory = "./out" |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + app := &cli.App{ |
| 20 | + Name: "BardChat", |
| 21 | + Usage: "Create content from man7.org manual pages", |
| 22 | + Flags: []cli.Flag{ |
| 23 | + &cli.StringFlag{ |
| 24 | + Name: "file", |
| 25 | + Aliases: []string{"f"}, |
| 26 | + Usage: "Path to the YAML file", |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | + Action: BardChat, |
| 31 | + } |
| 32 | + |
| 33 | + err := app.Run(os.Args) |
| 34 | + if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func BardChat(c *cli.Context) error { |
| 40 | + // Sanity Check for BARD_COOKIE |
| 41 | + if os.Getenv("BARD_COOKIE") == "" { |
| 42 | + fmt.Fprintf(os.Stderr, "BARD_COOKIE is not set\n") |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + |
| 46 | + // Read the recipe YAML file |
| 47 | + recipe, err := recipe.ReadYAML(c.String("file")) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("failed to read the YAML file: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Create man7.org manual pages metadata |
| 53 | + manPages, err := man7.CreateManPages() |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to create man pages: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + // TODO: thread pool for parallel processing |
| 59 | + |
| 60 | + for _, manPageWanted := range recipe.GetManPages() { // for each man page I want to ask questions... |
| 61 | + // Create a Bard Chat to each man page |
| 62 | + manPageChat, err := chat.New(manPages.Get(manPageWanted), recipe.GetTitlesAndQuestions()) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to create chat: %v", err) |
| 65 | + } |
| 66 | + // Ask all questions to each man page, one by one |
| 67 | + manPageAnswers, err := manPageChat.AskQuestions() |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to ask questions: %v", err) |
| 70 | + } |
| 71 | + // Append the answers to a file |
| 72 | + for title, answer := range manPageAnswers { |
| 73 | + writeFile(manPageWanted, title, answer) |
| 74 | + } |
| 75 | + break // only a single entry for now |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func writeFile(manpage, title, content string) error { |
| 82 | + filePath := outDirectory + "/" + manpage + ".md" |
| 83 | + |
| 84 | + _, err := os.Stat(filePath) |
| 85 | + if os.IsNotExist(err) { |
| 86 | + file, err := os.Create(filePath) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to create file: %v", err) |
| 89 | + } |
| 90 | + _, err = file.WriteString(fmt.Sprintf("# Event: %s\n\n", manpage)) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to write header to file: %v", err) |
| 93 | + } |
| 94 | + file.Close() |
| 95 | + } else if err != nil { |
| 96 | + return fmt.Errorf("failed to check file: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("failed to open file: %v", err) |
| 102 | + } |
| 103 | + defer file.Close() |
| 104 | + |
| 105 | + what := fmt.Sprintf("## %s\n%s", title, content) |
| 106 | + _, err = file.WriteString(what) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("failed to append content to file: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
0 commit comments