123 lines
3.9 KiB
Go
123 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var reUnsafeName = regexp.MustCompile(`[\\/:*?"<>|#^\[\]\x00-\x1f]+`)
|
|
|
|
// noteName builds the note title: a plain phrase, as the vault wants, safe as
|
|
// a file name and as a [[wikilink]] target.
|
|
func noteName(pkg *Package) string {
|
|
name := strings.TrimSpace(pkg.Title)
|
|
if c := strings.TrimSpace(pkg.Company); c != "" {
|
|
name = c + " - " + name
|
|
}
|
|
name = reUnsafeName.ReplaceAllString(name, " ")
|
|
name = strings.Join(strings.Fields(name), " ")
|
|
name = strings.Trim(name, ". ")
|
|
if r := []rune(name); len(r) > 100 {
|
|
name = strings.TrimSpace(string(r[:100]))
|
|
}
|
|
if name == "" {
|
|
name = "Job posting"
|
|
}
|
|
return name
|
|
}
|
|
|
|
func bullets(b *strings.Builder, heading string, items []string) {
|
|
if len(items) == 0 {
|
|
return
|
|
}
|
|
fmt.Fprintf(b, "## %s\n", heading)
|
|
for _, it := range items {
|
|
fmt.Fprintf(b, "- %s\n", strings.TrimSpace(it))
|
|
}
|
|
b.WriteString("\n")
|
|
}
|
|
|
|
// yamlString quotes a value for the frontmatter. A JSON string is also a
|
|
// valid YAML double-quoted string, whatever the posting put in it.
|
|
func yamlString(s string) string {
|
|
b, _ := json.Marshal(strings.Join(strings.Fields(s), " "))
|
|
return string(b)
|
|
}
|
|
|
|
// renderNote writes the package as a vault note: frontmatter, plain title, a
|
|
// Related section at the end. The properties after `created` are the columns
|
|
// of the vault's "Job Hunt" base (the table of all postings). `status` starts
|
|
// as "new"; the user changes it by hand (applied, interview, ...).
|
|
func renderNote(pkg *Package, url string, now time.Time) string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "---\nmoc: \"[[00-Job Hunt]]\"\ncreated: %s\n", now.Format("2006-01-02"))
|
|
fmt.Fprintf(&b, "company: %s\nrole: %s\nfit: %d\nverdict: %s\nlocation: %s\nposting: %s\nstatus: \"new\"\n---\n\n",
|
|
yamlString(pkg.Company), yamlString(pkg.Title), pkg.FitScore, yamlString(strings.ToLower(pkg.Verdict)),
|
|
yamlString(pkg.Location), yamlString(url))
|
|
fmt.Fprintf(&b, "# %s\n\n", noteName(pkg))
|
|
fmt.Fprintf(&b, "- [ ] Review and decide: **%s**, fit %d/100\n\n", strings.ToLower(pkg.Verdict), pkg.FitScore)
|
|
if pkg.VerdictReason != "" {
|
|
fmt.Fprintf(&b, "%s\n\n", pkg.VerdictReason)
|
|
}
|
|
|
|
b.WriteString("## Facts\n")
|
|
for _, f := range []struct{ label, value string }{
|
|
{"Posting", url}, {"Company", pkg.Company}, {"Location", pkg.Location}, {"Work mode", pkg.WorkMode},
|
|
{"Seniority", pkg.Seniority}, {"Salary", pkg.Salary}, {"Posting language", pkg.PostingLanguage},
|
|
{"Language requirement", pkg.DutchRequirement},
|
|
} {
|
|
if v := strings.TrimSpace(f.value); v != "" {
|
|
fmt.Fprintf(&b, "- **%s:** %s\n", f.label, v)
|
|
}
|
|
}
|
|
b.WriteString("\n")
|
|
|
|
if pkg.Summary != "" {
|
|
fmt.Fprintf(&b, "## Summary\n%s\n\n", strings.TrimSpace(pkg.Summary))
|
|
}
|
|
bullets(&b, "Requirements", pkg.Requirements)
|
|
bullets(&b, "Strengths", pkg.Strengths)
|
|
bullets(&b, "Gaps", pkg.Gaps)
|
|
bullets(&b, "CV tweaks", pkg.CVTweaks)
|
|
bullets(&b, "Questions to ask", pkg.Questions)
|
|
if pkg.CoverLetter != "" {
|
|
fmt.Fprintf(&b, "## Cover letter draft\n%s\n\n", strings.TrimSpace(pkg.CoverLetter))
|
|
}
|
|
b.WriteString("## Related\n- [[00-Job Hunt|Job Hunt]]\n- [[CV]]\n")
|
|
return b.String()
|
|
}
|
|
|
|
// writeNote saves the note without ever replacing an existing file: a second
|
|
// assessment of the same role gets a numbered name. The bot only creates
|
|
// files in the vault; it never edits or deletes one (deletions would sync to
|
|
// every device).
|
|
func writeNote(dir, name, content string) (string, error) {
|
|
for i := 1; i <= 50; i++ {
|
|
file := name + ".md"
|
|
if i > 1 {
|
|
file = fmt.Sprintf("%s (%d).md", name, i)
|
|
}
|
|
path := filepath.Join(dir, file)
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
|
if errors.Is(err, fs.ErrExist) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
_, werr := f.WriteString(content)
|
|
if cerr := f.Close(); werr == nil {
|
|
werr = cerr
|
|
}
|
|
return path, werr
|
|
}
|
|
return "", fmt.Errorf("too many notes named %q", name)
|
|
}
|