Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
chore: make convert/build work with cuedb
Browse files Browse the repository at this point in the history
  • Loading branch information
rawkode committed Apr 7, 2021
1 parent 32334f9 commit 025f55c
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 399 deletions.
1 change: 0 additions & 1 deletion blox/article.cue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
share_image?: string
profile_id?: string
category_id?: string
body_raw?: string
body?: string
id?: string
}
173 changes: 3 additions & 170 deletions blox/meta.go
Original file line number Diff line number Diff line change
@@ -1,171 +1,12 @@
package blox

import (
"errors"
"fmt"
"os"
"path"

"github.com/cueblox/blox/config"
"github.com/spf13/cobra"
)

// Models is used by various commands to determine
// how to perform certain actions based on arguments
// and flags provided. All new types must be
// represented in this slice.
var Models = []Model{
{
ID: "profile",
Name: "Profile",
Folder: "profiles",
ForeignKey: "profile_id",
Cue: ProfileCue,
},
{
ID: "article",
Name: "Article",
Folder: "articles",
ForeignKey: "article_id",
Cue: ArticleCue,
},
{
ID: "category",
Name: "Category",
Folder: "categories",
ForeignKey: "category_id",
Cue: CategoryCue,
},
{
ID: "page",
Name: "Page",
Folder: "pages",
ForeignKey: "page_id",
Cue: PageCue,
},
}

// Model represents a content model
type Model struct {
ID string
Name string
Folder string
ForeignKey string
Cue string
}

// GetModel finds a Model definition and returns
// it to the caller.
func GetModel(id string) (Model, error) {
for _, m := range Models {
if m.ID == id {
return m, nil
}
}
return Model{}, errors.New("model not found")
}

// StaticContentPath returns the path where
// images and other static content path will
// be stored
func (m Model) StaticContentPath() string {
cfg, err := config.Load()
cobra.CheckErr(err)
return path.Join(cfg.Base, cfg.Static)
}

// SourceContentPath returns the path where
// content source files (markdown/yaml) are stored
func (m Model) SourceContentPath() string {
cfg, err := config.Load()
cobra.CheckErr(err)
return path.Join(cfg.Base, cfg.Source, m.Folder)
}

// DestinationContentPath returns the path where
// output YAML is stored
func (m Model) DestinationContentPath() string {
cfg, err := config.Load()
cobra.CheckErr(err)
return path.Join(cfg.Base, cfg.Destination, m.Folder)
}

// SourceFilePath returns the path where a specific
// content file should be stored, based on its slug
func (m Model) SourceFilePath(slug string) string {
cfg, err := config.Load()
cobra.CheckErr(err)
fileName := slug + cfg.DefaultExtension

return path.Join(cfg.Base, cfg.Source, m.Folder, fileName)
}

// TemplatePath returns the path where templates are stored
func (m Model) TemplatePath() string {
cfg, err := config.Load()
cobra.CheckErr(err)
return path.Join(cfg.Base, cfg.Templates, m.Folder)
}

// TemplateFilePath returns the path for a template file
func (m Model) TemplateFilePath(slug string) string {
cfg, err := config.Load()
cobra.CheckErr(err)
fileName := slug + cfg.DefaultExtension
func defaultTemplate(typ string) ([]byte, error) {

return path.Join(cfg.Base, cfg.Templates, m.Folder, fileName)
}

// DestinationFilePath returns the path where a converted
// model will be stored as YAML.
func (m Model) DestinationFilePath(slug string) string {
cfg, err := config.Load()
cobra.CheckErr(err)
fileName := slug + ".yaml"

return path.Join(cfg.Base, cfg.Destination, m.Folder, fileName)
}

// New creates a new content model
func (m Model) New(slug string, destination string) error {
err := os.MkdirAll(destination, 0744)
if err != nil {
return err
}

// check for user installed templates first
cfg, err := config.Load()
cobra.CheckErr(err)

templatePath := path.Join(cfg.Base, cfg.Templates, m.Folder, m.ID+cfg.DefaultExtension)

joined := path.Join(destination, slug)
// check to see if we're creating the templates
// from the `init` command
var bb []byte
if templatePath == joined {
bb, err = m.defaultTemplate()
cobra.CheckErr(err)
} else {
bb, err = os.ReadFile(templatePath)
cobra.CheckErr(err)
}

// create the destination file
f, err := os.Create(joined)
if err != nil {
return err
}
defer f.Close()

f.Write(bb)

return nil
}

func (m Model) defaultTemplate() ([]byte, error) {

switch m.ID {
switch typ {
case "article":
return []byte(ArticleTemplate), nil
case "category":
Expand All @@ -175,14 +16,6 @@ func (m Model) defaultTemplate() ([]byte, error) {
case "page":
return []byte(PageTemplate), nil
default:
return []byte{}, fmt.Errorf("generator doesn't support %s yet", m.ID)
return []byte{}, fmt.Errorf("generator doesn't support %s yet", typ)
}
}

// BaseModel defines fields used by all drb
// models
type BaseModel struct {
ID string `json:"id"`
Body string `json:"body"`
BodyRaw string `json:"body_raw"`
}
1 change: 0 additions & 1 deletion blox/page.cue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
title: string
draft?: bool | *false
body_raw?: string
body?: string
id?: string
}
1 change: 1 addition & 0 deletions blox/profile.cue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

url: string
profile_id?: string
body?: string
}

#Profile: {
Expand Down
42 changes: 0 additions & 42 deletions blox/schema.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package blox

import (
// import go:embed
_ "embed"
"io/ioutil"
"path/filepath"
"strings"

"cuelang.org/go/cuego"
"github.com/cueblox/blox/cueutils"
"github.com/goccy/go-yaml"
)

// ArticleCue is the cue for an Article
Expand Down Expand Up @@ -43,37 +35,3 @@ var PageCue string
// PageTemplate is the page template
//go:embed page.md
var PageTemplate string

// FromYAML converts converted YAML content into output-ready map
func FromYAML(path string, modelName string, cue string) (map[string]interface{}, error) {
var model = make(map[string]interface{})

cuego.DefaultContext = &cuego.Context{}

err := cuego.Constrain(&model, cue)
if err != nil {
return nil, cueutils.UsefulError(err)
}

bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, cueutils.UsefulError(err)
}

err = yaml.Unmarshal(bytes, &model)
if err != nil {
return nil, cueutils.UsefulError(err)
}

err = cuego.Complete(&model)
if err != nil {
return nil, cueutils.UsefulError(err)
}

ext := filepath.Ext(path)
slug := strings.Replace(filepath.Base(path), ext, "", -1)

model["id"] = slug

return model, nil
}
21 changes: 8 additions & 13 deletions cmd/blox_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -23,24 +22,19 @@ var (

var buildCmd = &cobra.Command{
Use: "build",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Convert, Validate, & Build Your JSON Blox",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
cobra.CheckErr(err)

database, err := cuedb.NewDatabase()
database, err := cuedb.NewDatabase(cfg)
cobra.CheckErr(err)

// Load Schemas!
cobra.CheckErr(database.RegisterTables(blox.ProfileCue))

cobra.CheckErr(buildModels(cfg, &database))
cobra.CheckErr(convertModels(&database))
cobra.CheckErr(buildModels(&database))

if referentialIntegrity {
pterm.Info.Println("Checking Referential Integrity")
Expand All @@ -61,13 +55,13 @@ to quickly create a Cobra application.`,
},
}

func buildModels(cfg *config.BloxConfig, db *cuedb.Database) error {
func buildModels(db *cuedb.Database) error {
var errors error

pterm.Info.Println("Validating ...")

for _, table := range db.GetTables() {
err := filepath.Walk(path.Join(cfg.Base, cfg.Destination, table.Directory()),
err := filepath.Walk(db.DestinationPath(table),
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -110,7 +104,8 @@ func buildModels(cfg *config.BloxConfig, db *cuedb.Database) error {

return err

})
},
)

if err != nil {
errors = multierror.Append(err)
Expand Down
Loading

0 comments on commit 025f55c

Please sign in to comment.