Skip to content

Commit

Permalink
[UPD] Refactor resume generation logic and add format control via tem…
Browse files Browse the repository at this point in the history
…plate:

- Added function `generateResumeInFormat` to centralize resume generation logic for PDF and DOCX formats.
- Implemented template-based format extraction with `extractFormatFromTemplate`.
- If no format is specified in the template, the default behavior generates both PDF and DOCX.
- Improved error handling and logging for format-specific resume generation.
- Removed code duplication and streamlined format switching logic.
  • Loading branch information
atlet99 committed Oct 3, 2024
1 parent 31e1d5e commit b2d591e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
69 changes: 61 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@ package main

import (
"log"
"os"
"resume-generator/env"
"resume-generator/formats"
"resume-generator/models"
"resume-generator/utils"
"strings"
"text/template"
)

func main() {
// Загружаем .env файл
err := env.LoadEnv(".template.env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}

// Собираем данные для резюме
experience, err := env.GetExperience()
if err != nil {
log.Fatalf("Error loading experience: %v", err)
}

resume := models.Resume{
Name: env.GetEnv("NAME"),
Phone: env.GetEnv("PHONE"),
Email: env.GetEnv("EMAIL"),
LinkedIn: env.GetEnv("LINKEDIN"),
GitHub: env.GetEnv("GITHUB"),
TechnicalSkills: env.GetEnv("TECHNICAL_SKILLS"),
Experience: env.GetExperience(),
Experience: experience,
Education: env.GetEnv("EDUCATION"),
}

Expand All @@ -32,15 +38,62 @@ func main() {
log.Fatalf("Validation error: %v", err)
}

err = formats.GeneratePDF(resume, "generated_resume.pdf")
format, err := extractFormatFromTemplate("templates/resume.template")
if err != nil {
log.Fatalf("Error extracting format from template: %v", err)
}

if format == "" {
format = "BOTH"
}

switch strings.ToUpper(format) {
case "PDF":
generateResumeInFormat(resume, "pdf", "generated_resume.pdf")
case "DOC":
generateResumeInFormat(resume, "docx", "generated_resume.docx")
case "BOTH":
generateResumeInFormat(resume, "pdf", "generated_resume.pdf")
generateResumeInFormat(resume, "docx", "generated_resume.docx")
default:
log.Fatalf("Invalid format specified in template: %v", format)
}
}

func generateResumeInFormat(resume models.Resume, format string, filename string) {
var err error
switch format {
case "pdf":
err = formats.GeneratePDF(resume, filename)
case "docx":
err = formats.GenerateDOC(resume, filename)
default:
log.Fatalf("Invalid format: %v", format)
}

if err != nil {
log.Fatalf("Error generating %s: %v", format, err)
}

log.Printf("Resume generated successfully in %s format.\n", format)
}

func extractFormatFromTemplate(templateFile string) (string, error) {
tmpl, err := template.ParseFiles(templateFile)
if err != nil {
log.Fatalf("Error generating PDF: %v", err)
return "", err
}

data := struct {
Format string
}{
Format: "",
}

err = formats.GenerateDOC(resume, "generated_resume.docx")
err = tmpl.Execute(os.Stdout, &data)
if err != nil {
log.Fatalf("Error generating DOC: %v", err)
return "", err
}

log.Println("Resume generated successfully in both PDF and DOC formats.")
return data.Format, nil
}
13 changes: 9 additions & 4 deletions templates/resume.template
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{{ if .Name }}Name: {{.Name}}{{end}}
{{ if .Phone }}Phone: {{.Phone}}{{end}} | {{ if .Email }}Email: {{.Email}}{{end}} | {{ if .LinkedIn }}LinkedIn: {{.LinkedIn}}{{end}} | {{ if .GitHub }}GitHub: {{.GitHub}}{{end}}
# Resume Template

FORMAT: {{.Format}}

Name: {{.Name}}
Phone: {{.Phone}}
Email: {{.Email}}

## Technical Skills
{{ if .TechnicalSkills }}{{.TechnicalSkills}}{{end}}

## Experience
{{range .Experience}}
{{ if .Role }}**Role**: {{.Role}}{{end}} | {{ if .Company }}**Company**: {{.Company}}{{end}} | {{ if .Location }}**Location**: {{.Location}}{{end}} | {{ if .Dates }}**Dates**: {{.Dates}}{{end}}
- {{ if .Description }}{{.Description}}{{end}}
Role: {{.Role}} | Company: {{.Company}} | Location: {{.Location}} | Dates: {{.Dates}}
- {{.Description}}
{{end}}

## Education
Expand Down

0 comments on commit b2d591e

Please sign in to comment.