Skip to content

Commit

Permalink
feat(cmd): Split up parse into subcmds
Browse files Browse the repository at this point in the history
While the gedcom records are available via logging, sometimes it's
more convenient see that when it's sent directly to stdout. Adjust the
parse subcommand to specify what kind of data you want.
  • Loading branch information
rafaelespinoza committed Nov 5, 2023
1 parent e25afa6 commit 5e40de1
Showing 1 changed file with 76 additions and 10 deletions.
86 changes: 76 additions & 10 deletions internal/cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/rafaelespinoza/alf"
"github.com/rafaelespinoza/ged/internal/gedcom"
"github.com/rafaelespinoza/ged/internal/srv"
)

func makeParse(name string) alf.Directive {
out := alf.Command{
Description: "read GEDCOM data from STDIN, transform to entity types, write to STDOUT",
toEntities := alf.Command{
Description: "transform data to application entity types",
Setup: func(_ flag.FlagSet) *flag.FlagSet {
fullName := mainName + " " + name
subName := "to-entities"
fullName := mainName + " " + subName
flags := newFlagSet(fullName)

flags.Usage = func() {
fmt.Fprintf(flags.Output(), `%s < path/to/input
Description:
Pipe in some data, interpret it and print the transformed results to STDOUT as JSON.
This subcommand is for meant for inspecting the data transformations applied
to the input data.
Pipe in some data, interpret it and print the transformed results to STDOUT as JSON. The
output shapes here are application "entities", which are simplified reductions of the
original GEDCOM data. Most application functionality interfaces with these data types.
The output shape:
{
"people": []entity.Person{}.
"unions": []entity.Union{},
"people": []entity.Person{},
"unions": []entity.Union{}
}
`,
initUsageLine(name),
initUsageLine(subName),
)
printFlagDefaults(flags)
}
Expand All @@ -48,5 +50,69 @@ Description:
},
}

toRecords := alf.Command{
Description: "transform data to GEDCOM records",
Setup: func(_ flag.FlagSet) *flag.FlagSet {
subName := "to-records"
fullName := mainName + " " + subName
flags := newFlagSet(fullName)

flags.Usage = func() {
fmt.Fprintf(flags.Output(), `%s < path/to/input
Description:
Pipe in some data, interpret it and print the transformed results to STDOUT as JSON.
The output shapes are like GEDCOM record types.
The output shape:
{
"Individuals": []gedcom.IndividualRecord{},
"Families": []gedcom.FamilyRecord{},
"Sources": []gedcom.SourceRecord{}
}
`,
initUsageLine(subName),
)
printFlagDefaults(flags)
}
return flags
},
Run: func(ctx context.Context) error {
records, err := gedcom.ReadRecords(ctx, os.Stdin)
if err != nil {
return err
}

return writeJSON(os.Stdout, records)
},
}

out := alf.Delegator{
Description: "interpret GEDCOM data, transform it, write to STDOUT",
Subs: map[string]alf.Directive{
"to-entities": &toEntities,
"to-records": &toRecords,
},
Flags: newFlagSet(mainName),
}
out.Flags.Usage = func() {
fmt.Fprintf(out.Flags.Output(), `%s < path/to/input
Description:
Pipe in some data, interpret it and print the transformed results to STDOUT as JSON.
This subcommand is for meant for inspecting the data transformations applied
to the input data.
Subcommands:
These will have their own set of flags. Put them after the subcommand.
%v
`,
initUsageLine(name), strings.Join(out.DescribeSubcommands(), "\n\t"),
)
}

return &out
}

0 comments on commit 5e40de1

Please sign in to comment.