Dan Anglin
4f90a4c8bb
Use the default FlagSet to parse all the arguments and to set the default help message for spruce. Arguments set after the subcommand are still parsed by the subcommand's FlagSet. The summaries for all subcommand are defined in one place in the main function for consistency. The internal/cmd.SpruceUsage function is replaced with the spruceUsageFunc function in the main package which returns the usage function which is set as the default usage function. The format of the help message for spruce and the subcommands have been updated with the inspiration of the help message from gopass.
127 lines
2.6 KiB
Go
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(name, summary string) *CreateCommand {
|
|
command := CreateCommand{
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
summary: summary,
|
|
}
|
|
|
|
command.StringVar(&command.filename, "filepath", "cv.json", "specify the output path of the CV JSON file.")
|
|
command.StringVar(&command.firstName, "first-name", "", "specify your first name.")
|
|
command.StringVar(&command.jobTitle, "job-title", "", "specify your current job title.")
|
|
command.StringVar(&command.lastName, "last-name", "", "specify your last name.")
|
|
|
|
command.Usage = usageFunc(command.Name(), command.summary, command.FlagSet)
|
|
|
|
return &command
|
|
}
|
|
|
|
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
|
|
}
|