spruce/internal/cmd/create.go
Dan Anglin 8a530551c2
fix: create separate types for work and education
Create separate types for education and employment to fix the issue
where unnecessary fields are created under both education and employment
when a new CV is created.
2023-08-19 00:29:55 +01:00

127 lines
2.6 KiB
Go

package cmd
import (
"encoding/json"
"flag"
"fmt"
"log/slog"
"os"
"time"
"codeflow.dananglin.me.uk/apollo/spruce/internal/cv"
)
type CreateCommand struct {
*flag.FlagSet
summary string
firstName string
lastName string
jobTitle string
filename string
}
func NewCreateCommand() *CreateCommand {
cc := CreateCommand{
FlagSet: flag.NewFlagSet("create", flag.ExitOnError),
summary: "Create a new CV JSON file.",
}
cc.StringVar(&cc.filename, "filepath", "cv.json", "specify the path where the CV JSON document should be created.")
cc.StringVar(&cc.firstName, "first-name", "", "specify your first name.")
cc.StringVar(&cc.jobTitle, "job-title", "", "specify your current job title.")
cc.StringVar(&cc.lastName, "last-name", "", "specify your last name.")
cc.Usage = usageFunc(cc.Name(), cc.summary, cc.FlagSet)
return &cc
}
func (c *CreateCommand) Run() error {
data := cv.NewCV(c.firstName, c.lastName, c.jobTitle)
data.Contact = map[string]string{
"Email": "",
"Phone": "",
}
data.Links = map[string]string{
"GitHub": "",
"Website": "",
}
data.Summary = make([]string, 2)
data.Skills = []cv.Skills{
{
Category: "",
Values: make([]string, 1),
},
{
Category: "",
Values: make([]string, 1),
},
}
data.Employment = []cv.Employment{
{
Company: "",
Location: "",
LocationType: "",
JobTitle: "",
Duration: cv.Duration{
Start: cv.Date{
Day: int64(time.Now().Day()),
Month: int64(time.Now().Month()),
Year: int64(time.Now().Year()),
},
End: &cv.Date{
Day: int64(time.Now().Day()),
Month: int64(time.Now().Month()),
Year: int64(time.Now().Year()),
},
Present: false,
},
Details: make([]string, 2),
},
}
data.Education = []cv.Education{
{
School: "",
Location: "",
Qualification: "",
Duration: cv.Duration{
Start: cv.Date{
Year: int64(time.Now().Year()),
Month: int64(time.Now().Month()),
Day: int64(time.Now().Day()),
},
End: &cv.Date{
Year: int64(time.Now().Year()),
Month: int64(time.Now().Month()),
Day: int64(time.Now().Day()),
},
},
},
}
data.Interests = make([]string, 2)
file, err := os.Create(c.filename)
if err != nil {
return fmt.Errorf("unable to open %s; %w", c.filename, err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(data); err != nil {
return fmt.Errorf("unable to write the data to %s; %w", c.filename, err)
}
slog.Info("CV successfully created", "filename", file.Name())
return nil
}