Files
jobhunt/main.go
T

134 lines
3.5 KiB
Go

// Command jobhunt turns a job posting into a "target package": a summary,
// a fit assessment against the CV, and a cover letter draft.
//
// jobhunt bot watch a Discord channel for links or pasted postings
// jobhunt assess <src> assess one posting (URL, file, or - for stdin) and print the note
// jobhunt scan read the company feeds and post new matching jobs (run by a timer)
// jobhunt scan --dry-run list what currently matches, without Claude, Discord or the vault
//
// Standard library only. Claude runs through the `claude` CLI with every tool
// disabled: it gets the CV and the posting text and returns JSON, nothing else.
package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
// Config comes from the environment (set by the NixOS module).
type Config struct {
NotesDir string // the vault's "Job Hunt" folder
CVPath string
StateDir string
ClaudeBin string
Model string // optional, empty means the CLI default
TokenFile string
ChannelID string
OwnerID string // the only Discord user the bot listens to
Poll time.Duration
ScanMax int // postings assessed per scan run
}
func env(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func envInt(key string, fallback int) int {
if n, err := strconv.Atoi(os.Getenv(key)); err == nil && n > 0 {
return n
}
return fallback
}
func loadConfig() Config {
notes := env("JOBHUNT_NOTES_DIR", ".")
return Config{
NotesDir: notes,
CVPath: env("JOBHUNT_CV", notes+"/CV.md"),
StateDir: env("JOBHUNT_STATE_DIR", os.TempDir()),
ClaudeBin: env("JOBHUNT_CLAUDE", "claude"),
Model: os.Getenv("JOBHUNT_MODEL"),
TokenFile: os.Getenv("JOBHUNT_DISCORD_TOKEN_FILE"),
ChannelID: os.Getenv("JOBHUNT_DISCORD_CHANNEL"),
OwnerID: os.Getenv("JOBHUNT_DISCORD_OWNER"),
Poll: 5 * time.Second,
ScanMax: envInt("JOBHUNT_SCAN_MAX", 10),
}
}
func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil)))
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "usage: jobhunt bot | jobhunt scan [--dry-run] | jobhunt assess <url|file|->")
os.Exit(2)
}
cfg := loadConfig()
var err error
switch os.Args[1] {
case "bot":
err = runBot(ctx, cfg)
case "scan":
err = runScan(ctx, cfg, len(os.Args) > 2 && os.Args[2] == "--dry-run")
case "assess":
if len(os.Args) != 3 {
fmt.Fprintln(os.Stderr, "usage: jobhunt assess <url|file|->")
os.Exit(2)
}
err = runAssess(ctx, cfg, os.Args[2])
default:
err = fmt.Errorf("unknown command %q", os.Args[1])
}
if err != nil && ctx.Err() == nil {
slog.Error("jobhunt failed", "err", err)
os.Exit(1)
}
}
// runAssess is the manual path: no Discord, nothing written to the vault.
func runAssess(ctx context.Context, cfg Config, src string) error {
var p Posting
switch {
case strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://"):
var err error
if p, err = fetchPosting(ctx, newHTTPClient(false), src); err != nil {
return err
}
case src == "-":
b, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
p = Posting{Text: string(b)}
default:
b, err := os.ReadFile(src)
if err != nil {
return err
}
p = Posting{Text: string(b)}
}
pkg, err := (&Assessor{Config: cfg}).Assess(ctx, p)
if err != nil {
return err
}
fmt.Print(renderNote(pkg, p.URL, time.Now()))
return nil
}