Skip to content

Commit

Permalink
refactor: Skip and log if parsing fails (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikelVeen authored May 14, 2024
1 parent bc9d2ca commit 37592c7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
20 changes: 18 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"path"
"strings"
"time"

"github.com/glass-cms/glasscms/item"
"github.com/glass-cms/glasscms/parser"
"github.com/glass-cms/glasscms/sourcer"
"github.com/lmittmann/tint"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
Expand All @@ -34,10 +37,20 @@ var (

type ConvertCommand struct {
*cobra.Command

logger *slog.Logger
}

func NewConvertCommand() *ConvertCommand {
c := &ConvertCommand{}
c := &ConvertCommand{
logger: slog.New(
tint.NewHandler(os.Stdout, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.TimeOnly,
}),
),
}

c.Command = &cobra.Command{
Use: "convert <source>",
Short: "Convert source files",
Expand Down Expand Up @@ -71,6 +84,8 @@ func NewConvertCommand() *ConvertCommand {
flagset.StringP(ArgFormat, ArgFormatShorthand, "json", "Output format (json, yaml)")
_ = viper.BindPFlag(ArgFormat, flagset.Lookup(ArgFormat))

// TODO: Add a flag to pretty print the output.

return c
}

Expand Down Expand Up @@ -104,7 +119,8 @@ func (c *ConvertCommand) Execute(_ *cobra.Command, args []string) error {
var i *item.Item
i, err = parser.Parse(src)
if err != nil {
return err
c.logger.Warn(fmt.Sprintf("Failed to parse %s: %s", src.Name(), err))
continue
}

items = append(items, i)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/djherbis/times v1.6.0
github.com/lmittmann/tint v1.0.4
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
8 changes: 7 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"gopkg.in/yaml.v3"
)

var (
ErrInvalidFrontMatter = errors.New("invalid front matter yaml")
)

const (
numParts = 3
)
Expand All @@ -22,10 +26,12 @@ func Parse(src sourcer.Source) (*item.Item, error) {
}
defer src.Close()

// FIXME: This is a naive implementation that breaks if the content contains "---\n".

// Split the content into front matter and markdown
parts := bytes.SplitN(c, []byte("---\n"), numParts)
if len(parts) < numParts {
return nil, errors.New("invalid content")
return nil, ErrInvalidFrontMatter
}

// Parse the YAML front matter
Expand Down

0 comments on commit 37592c7

Please sign in to comment.