Skip to content

Commit

Permalink
cheatsheet: nest parser inside a toplevel cheatsheet pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed May 11, 2024
1 parent 1116998 commit 59204f2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
19 changes: 19 additions & 0 deletions ingest/cheatsheet/cheatsheet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cheatsheet

type CheatSheet struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Examples []*Example `json:"examples,omitempty"`
Tags []string `json:"tags,omitempty"`
}

type Example struct {
Explanation string `json:"explanation,omitempty"`
Command string `json:"command,omitempty"`
}

type Provider string

const (
TLDR Provider = "tldr"
)
42 changes: 13 additions & 29 deletions ingest/parser/parser.go → ingest/cheatsheet/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,18 @@ import (
"os"
"path/filepath"
"strings"

"github.com/getsavvyinc/savvy-cli/ingest/cheatsheet"
)

type Parser interface {
Parse(path string) (*CheatSheet, error)
Provider() CheatSheetProvider
}

type CheatSheet struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Examples []*Example `json:"examples,omitempty"`
Tags []string `json:"tags,omitempty"`
}

type Example struct {
Explanation string `json:"explanation,omitempty"`
Command string `json:"command,omitempty"`
Parse(path string) (*cheatsheet.CheatSheet, error)
Provider() cheatsheet.Provider
}

type CheatSheetProvider string

const (
TLDR CheatSheetProvider = "tldr"
)

func New(provider CheatSheetProvider) Parser {
func New(provider cheatsheet.Provider) Parser {
switch provider {
case TLDR:
case cheatsheet.TLDR:
return &tldr{
provider: provider,
}
Expand All @@ -43,14 +27,14 @@ func New(provider CheatSheetProvider) Parser {
}

type tldr struct {
provider CheatSheetProvider
provider cheatsheet.Provider
}

var _ Parser = &tldr{}

var ErrRequiredMdFile = fmt.Errorf("required markdown file")

func (t *tldr) Parse(path string) (*CheatSheet, error) {
func (t *tldr) Parse(path string) (*cheatsheet.CheatSheet, error) {
if ext := filepath.Ext(path); ext != ".md" && ext != ".markdown" {
return nil, fmt.Errorf("%w: invalid file extension: %s", ErrRequiredMdFile, ext)
}
Expand All @@ -63,7 +47,7 @@ func (t *tldr) Parse(path string) (*CheatSheet, error) {

scanner := bufio.NewScanner(f)

cs := &CheatSheet{}
cs := &cheatsheet.CheatSheet{}
var description []string
var explanations []string
var commands []string
Expand Down Expand Up @@ -94,14 +78,14 @@ func (t *tldr) Parse(path string) (*CheatSheet, error) {
return cs, nil
}

func (t *tldr) Provider() CheatSheetProvider {
func (t *tldr) Provider() cheatsheet.Provider {
return t.provider
}

func zip(explanations, commands []string) []*Example {
examples := make([]*Example, 0, len(explanations))
func zip(explanations, commands []string) []*cheatsheet.Example {
examples := make([]*cheatsheet.Example, 0, len(explanations))
for i := range explanations {
examples = append(examples, &Example{
examples = append(examples, &cheatsheet.Example{
Explanation: explanations[i],
Command: commands[i],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package parser_test
import (
"testing"

"github.com/getsavvyinc/savvy-cli/ingest/parser"
"github.com/getsavvyinc/savvy-cli/ingest/cheatsheet"
"github.com/getsavvyinc/savvy-cli/ingest/cheatsheet/parser"
"github.com/stretchr/testify/assert"
)

func TestTLDRParser(t *testing.T) {
func TestTLDR(t *testing.T) {
tests := []struct {
name string
filepath string
err error
cheatSheet *parser.CheatSheet
cheatSheet *cheatsheet.CheatSheet
}{
{
name: "Wrong File",
Expand All @@ -22,10 +23,10 @@ func TestTLDRParser(t *testing.T) {
{
name: "sh.md",
filepath: "testdata/sh.md",
cheatSheet: &parser.CheatSheet{
cheatSheet: &cheatsheet.CheatSheet{
Title: "sh",
Description: "Bourne shell, the standard command language interpreter. See also `histexpand` for history expansion. More information: <https://manned.org/sh>.",
Examples: []*parser.Example{
Examples: []*cheatsheet.Example{
{
Command: "sh",
Explanation: "Start an interactive shell session",
Expand All @@ -47,7 +48,7 @@ func TestTLDRParser(t *testing.T) {
},
}

tldr := parser.New(parser.TLDR)
tldr := parser.New(cheatsheet.TLDR)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cs, err := tldr.Parse(tc.filepath)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

"golang.org/x/sync/errgroup"

"github.com/getsavvyinc/savvy-cli/ingest/parser"
"github.com/getsavvyinc/savvy-cli/ingest/cheatsheet"
"github.com/getsavvyinc/savvy-cli/ingest/cheatsheet/parser"
)

const tldrPath = "tldr/pages/"
Expand All @@ -18,7 +19,7 @@ const maxConcurrency = 500

func main() {
logger := slog.Default()
cheatsheetParser := parser.New(parser.TLDR)
cheatsheetParser := parser.New(cheatsheet.TLDR)

var g errgroup.Group
g.SetLimit(maxConcurrency)
Expand Down

0 comments on commit 59204f2

Please sign in to comment.