|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "text/template" |
| 11 | + |
| 12 | + "github.com/zapier/tfbuddy/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + beginMarker = "<!-- BEGIN GENERATED CONFIGURATION -->" |
| 17 | + endMarker = "<!-- END GENERATED CONFIGURATION -->" |
| 18 | +) |
| 19 | + |
| 20 | +//go:embed templates/usage_config.tmpl |
| 21 | +var usageTemplate string |
| 22 | + |
| 23 | +type templateOption struct { |
| 24 | + Env string |
| 25 | + Flag string |
| 26 | + Description string |
| 27 | + Default string |
| 28 | +} |
| 29 | + |
| 30 | +type templateData struct { |
| 31 | + Options []templateOption |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + if err := run(); err != nil { |
| 36 | + fmt.Fprintln(os.Stderr, err) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func run() error { |
| 42 | + tmpl, err := template.New("usage").Parse(usageTemplate) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("parse template: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + var rendered bytes.Buffer |
| 48 | + data := templateData{Options: make([]templateOption, 0, len(config.DocumentationOptions()))} |
| 49 | + for _, option := range config.DocumentationOptions() { |
| 50 | + data.Options = append(data.Options, templateOption{ |
| 51 | + Env: strings.Join(option.EnvVars, "<br>"), |
| 52 | + Flag: option.Flag, |
| 53 | + Description: option.Description, |
| 54 | + Default: option.DefaultValue, |
| 55 | + }) |
| 56 | + } |
| 57 | + if err := tmpl.Execute(&rendered, data); err != nil { |
| 58 | + return fmt.Errorf("render template: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + usagePath, err := findUsagePath() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + current, err := os.ReadFile(usagePath) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("read usage doc: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + updated, err := replaceGeneratedBlock(string(current), rendered.String()) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + if err := os.WriteFile(usagePath, []byte(updated), 0o644); err != nil { |
| 76 | + return fmt.Errorf("write usage doc: %w", err) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func findUsagePath() (string, error) { |
| 82 | + candidates := []string{ |
| 83 | + filepath.Join("docs", "usage.md"), |
| 84 | + filepath.Join("..", "..", "docs", "usage.md"), |
| 85 | + } |
| 86 | + for _, candidate := range candidates { |
| 87 | + path := filepath.Clean(candidate) |
| 88 | + if _, err := os.Stat(path); err == nil { |
| 89 | + return path, nil |
| 90 | + } |
| 91 | + } |
| 92 | + return "", fmt.Errorf("could not locate docs/usage.md from current working directory") |
| 93 | +} |
| 94 | + |
| 95 | +func replaceGeneratedBlock(current string, generated string) (string, error) { |
| 96 | + start := strings.Index(current, beginMarker) |
| 97 | + if start == -1 { |
| 98 | + return "", fmt.Errorf("missing begin marker %q", beginMarker) |
| 99 | + } |
| 100 | + end := strings.Index(current, endMarker) |
| 101 | + if end == -1 { |
| 102 | + return "", fmt.Errorf("missing end marker %q", endMarker) |
| 103 | + } |
| 104 | + if end < start { |
| 105 | + return "", fmt.Errorf("configuration markers out of order") |
| 106 | + } |
| 107 | + |
| 108 | + var output strings.Builder |
| 109 | + output.WriteString(current[:start+len(beginMarker)]) |
| 110 | + output.WriteString("\n") |
| 111 | + output.WriteString(strings.TrimRight(generated, "\n")) |
| 112 | + output.WriteString("\n") |
| 113 | + output.WriteString(current[end:]) |
| 114 | + return output.String(), nil |
| 115 | +} |
0 commit comments