Compare commits

..

2 Commits

Author SHA1 Message Date
Brian Picciano
062e5bbd10 delete some old script that aren't needed anymore 2022-08-18 20:28:04 -06:00
Brian Picciano
60018dd9f5 load some test data when starting the dev shell 2022-08-18 20:27:40 -06:00
9 changed files with 224 additions and 307 deletions

View File

@ -2,6 +2,7 @@
bash, bash,
go, go,
buildGoModule, buildGoModule,
sqlite,
writeScript, writeScript,
writeText, writeText,
stdenv, stdenv,
@ -51,13 +52,12 @@
shell = stdenv.mkDerivation { shell = stdenv.mkDerivation {
name = "mediocre-blog-srv-shell"; name = "mediocre-blog-srv-shell";
buildInputs = [ go ]; buildInputs = [ go sqlite ];
shellHook = ''source ${init}''; shellHook = ''
}; source ${init}
shellWithBuild = stdenv.mkDerivation { echo "Loading test data..."
name = "mediocre-blog-srv-shell-with-build"; (cd srv/src/cmd/load-test-data && go run main.go)
buildInputs = [ go build ]; '';
shellHook = ''source ${init}'';
}; };
} }

View File

@ -1,114 +0,0 @@
package main
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
)
func importAsset(assetStore post.AssetStore, id, path string) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("opening file: %w", err)
}
defer f.Close()
if err := assetStore.Set(id, f); err != nil {
return fmt.Errorf("setting into asset store: %w", err)
}
return nil
}
func main() {
ctx := context.Background()
cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{})
var dataDir cfgpkg.DataDir
dataDir.SetupCfg(cfg)
defer dataDir.Close()
ctx = mctx.WithAnnotator(ctx, &dataDir)
id := cfg.String("id", "", "ID the asset will be stored under")
path := cfg.String("path", "", "Path the asset should be imported from")
fromStdin := cfg.Bool("from-stdin", false, "If set, ignore id and path, read space separated id/path pairs from stdin")
// initialization
err := cfg.Init(ctx)
logger := mlog.NewLogger(nil)
defer logger.Close()
if !*fromStdin && (*id == "" || *path == "") {
logger.FatalString(ctx, "-id and -path are required if -from-stdin is not given")
}
logger.Info(ctx, "process started")
defer logger.Info(ctx, "process exiting")
if err != nil {
logger.Fatal(ctx, "initializing", err)
}
postDB, err := post.NewSQLDB(dataDir)
if err != nil {
logger.Fatal(ctx, "initializing post sql db", err)
}
defer postDB.Close()
assetStore := post.NewAssetStore(postDB)
if !*fromStdin {
ctx := mctx.Annotate(ctx, "id", *id, "path", *path)
if err := importAsset(assetStore, *id, *path); err != nil {
logger.Fatal(ctx, "failed to import asset", err)
}
logger.Info(ctx, "asset stored")
return
}
for stdin := bufio.NewReader(os.Stdin); ; {
line, err := stdin.ReadString('\n')
if errors.Is(err, io.EOF) {
return
} else if err != nil {
logger.Fatal(ctx, "reading from stdin", err)
}
fields := strings.Fields(line)
if len(fields) < 2 {
ctx := mctx.Annotate(ctx, "line", line)
logger.FatalString(ctx, "cannot process line with fewer than 2 fields")
}
id, path := fields[0], fields[1]
ctx := mctx.Annotate(ctx, "id", id, "path", path)
if err := importAsset(assetStore, id, path); err != nil {
logger.Fatal(ctx, "failed to import asset", err)
}
logger.Info(ctx, "asset stored")
}
}

View File

@ -1,18 +0,0 @@
diamond-square-terrain.png ../../static/src/img/diamond-square/terrain.png
diamond-square-dsalg.png ../../static/src/img/diamond-square/dsalg.png
program-structure-diag1.jpg ../../static/src/img/program-structure/diag1.jpg
program-structure-diag2.jpg ../../static/src/img/program-structure/diag2.jpg
program-structure-diag3.jpg ../../static/src/img/program-structure/diag3.jpg
open-infra-keybase.png ../../static/src/img/open-infra/keybase.png
wedding-1.jpg ../../static/src/img/wedding/1.jpg
wedding-2.jpg ../../static/src/img/wedding/2.jpg
wedding-3.jpg ../../static/src/img/wedding/3.jpg
happy-tree-partial.png ../../static/src/img/happy-tree/partial.png
happy-tree-atmp1.png ../../static/src/img/happy-tree/happy-tree-atmp1.png
happy-tree-atmp2.png ../../static/src/img/happy-tree/happy-tree-atmp2.png
happy-tree-atmp3-pow3.png ../../static/src/img/happy-tree/happy-tree-atmp3-pow3.png
happy-tree-atmp3-pow4.png ../../static/src/img/happy-tree/happy-tree-atmp3-pow4.png
happy-tree-atmp3-pow5.png ../../static/src/img/happy-tree/happy-tree-atmp3-pow5.png
happy-tree-atmp3-pow6.png ../../static/src/img/happy-tree/happy-tree-atmp3-pow6.png
nfts-disaster-girl.jpg ../../static/src/img/nfts/disaster-girl.jpg
nfts-gleaners.jpg ../../static/src/img/nfts/gleaners.jpg

View File

@ -1,168 +0,0 @@
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/adrg/frontmatter"
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
)
type postFrontmatter struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Tags string `yaml:"tags"`
Series string `yaml:"series"`
Updated string `yaml:"updated"`
}
func parseDate(str string) (time.Time, error) {
const layout = "2006-01-02"
return time.Parse(layout, str)
}
var postNameRegexp = regexp.MustCompile(`(20..-..-..)-([^.]+).md`)
func importPost(postStore post.Store, path string) (post.StoredPost, error) {
fileName := filepath.Base(path)
fileNameMatches := postNameRegexp.FindStringSubmatch(fileName)
if len(fileNameMatches) != 3 {
return post.StoredPost{}, fmt.Errorf("file name %q didn't match regex", fileName)
}
publishedAtStr := fileNameMatches[1]
publishedAt, err := parseDate(publishedAtStr)
if err != nil {
return post.StoredPost{}, fmt.Errorf("parsing publish date %q: %w", publishedAtStr, err)
}
postID := fileNameMatches[2]
f, err := os.Open(path)
if err != nil {
return post.StoredPost{}, fmt.Errorf("opening file: %w", err)
}
defer f.Close()
var matter postFrontmatter
body, err := frontmatter.Parse(f, &matter)
if err != nil {
return post.StoredPost{}, fmt.Errorf("parsing frontmatter: %w", err)
}
// if there is already a post for this ID, delete it, we're overwriting
if err := postStore.Delete(postID); err != nil {
return post.StoredPost{}, fmt.Errorf("deleting post id %q: %w", postID, err)
}
p := post.Post{
ID: postID,
Title: matter.Title,
Description: matter.Description,
Tags: strings.Fields(matter.Tags),
Series: matter.Series,
Body: string(body),
}
if _, err := postStore.Set(p, publishedAt); err != nil {
return post.StoredPost{}, fmt.Errorf("storing post id %q: %w", p.ID, err)
}
if matter.Updated != "" {
lastUpdatedAt, err := parseDate(matter.Updated)
if err != nil {
return post.StoredPost{}, fmt.Errorf("parsing updated date %q: %w", matter.Updated, err)
}
// as a hack, we store the post again with the updated date as now. This
// will update the LastUpdatedAt field in the Store.
if _, err := postStore.Set(p, lastUpdatedAt); err != nil {
return post.StoredPost{}, fmt.Errorf("updating post id %q: %w", p.ID, err)
}
}
storedPost, err := postStore.GetByID(p.ID)
if err != nil {
return post.StoredPost{}, fmt.Errorf("retrieving stored post by id %q: %w", p.ID, err)
}
return storedPost, nil
}
func main() {
ctx := context.Background()
cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{})
var dataDir cfgpkg.DataDir
dataDir.SetupCfg(cfg)
defer dataDir.Close()
ctx = mctx.WithAnnotator(ctx, &dataDir)
paths := cfg.Args("<post file paths...>")
// initialization
err := cfg.Init(ctx)
logger := mlog.NewLogger(nil)
defer logger.Close()
logger.Info(ctx, "process started")
defer logger.Info(ctx, "process exiting")
if err != nil {
logger.Fatal(ctx, "initializing", err)
}
if len(*paths) == 0 {
logger.FatalString(ctx, "no paths given")
}
postDB, err := post.NewSQLDB(dataDir)
if err != nil {
logger.Fatal(ctx, "initializing post sql db", err)
}
defer postDB.Close()
postStore := post.NewStore(postDB)
for _, path := range *paths {
ctx := mctx.Annotate(ctx, "postPath", path)
storedPost, err := importPost(postStore, path)
if err != nil {
logger.Error(ctx, "importing post", err)
}
ctx = mctx.Annotate(ctx,
"postID", storedPost.ID,
"postTitle", storedPost.Title,
"postDescription", storedPost.Description,
"postTags", storedPost.Tags,
"postSeries", storedPost.Series,
"postPublishedAt", storedPost.PublishedAt,
)
if !storedPost.LastUpdatedAt.IsZero() {
ctx = mctx.Annotate(ctx,
"postLastUpdatedAt", storedPost.LastUpdatedAt)
}
logger.Info(ctx, "post stored")
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

View File

@ -0,0 +1,129 @@
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
"gopkg.in/yaml.v3"
)
type testData struct {
PublishedPosts []post.Post `yaml:"published_posts"`
Assets map[string]string
}
func loadTestData(path string) (testData, error) {
f, err := os.Open(path)
if err != nil {
return testData{}, fmt.Errorf("opening file: %w", err)
}
defer f.Close()
var res testData
if err := yaml.NewDecoder(f).Decode(&res); err != nil {
return testData{}, fmt.Errorf("decoding file contents: %w", err)
}
return res, nil
}
func main() {
ctx := context.Background()
cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{})
var dataDir cfgpkg.DataDir
dataDir.SetupCfg(cfg)
defer dataDir.Close()
ctx = mctx.WithAnnotator(ctx, &dataDir)
testDataPath := cfg.String("test-data-path", "./test-data.yml", "File containing the data to be loaded in")
// initialization
err := cfg.Init(ctx)
logger := mlog.NewLogger(nil)
defer logger.Close()
logger.Info(ctx, "process started")
defer logger.Info(ctx, "process exiting")
if err != nil {
logger.Fatal(ctx, "initializing", err)
}
testDataDir := filepath.Dir(*testDataPath)
testData, err := loadTestData(*testDataPath)
if err != nil {
logger.Fatal(ctx, "loading test data", err)
}
postDB, err := post.NewSQLDB(dataDir)
if err != nil {
logger.Fatal(ctx, "initializing post sql db", err)
}
defer postDB.Close()
{
postStore := post.NewStore(postDB)
now := time.Now().Truncate(time.Hour)
for _, post := range testData.PublishedPosts {
ctx := mctx.Annotate(ctx,
"postID", post.ID,
"now", now)
if _, err := postStore.Set(post, now); err != nil {
logger.Fatal(ctx, "setting post", err)
}
logger.Info(ctx, "set post")
now = now.Add(1 * time.Hour)
}
}
{
assetStore := post.NewAssetStore(postDB)
setAsset := func(assetID, assetPath string) error {
assetFullPath := filepath.Join(testDataDir, assetPath)
f, err := os.Open(assetFullPath)
if err != nil {
return fmt.Errorf("opening %q for reading: %w", assetFullPath, err)
}
defer f.Close()
return assetStore.Set(assetID, f)
}
for assetID, assetPath := range testData.Assets {
ctx := mctx.Annotate(ctx,
"assetID", assetID,
"assetPath", assetPath)
if err := setAsset(assetID, assetPath); err != nil {
logger.Fatal(ctx, "setting asset", err)
}
logger.Info(ctx, "set asset")
}
}
}

View File

@ -0,0 +1,85 @@
---
published_posts:
- id: markdown-test
title: Markdown Test
description: A little post containing different kinds of markdown elements.
tags:
- foo
series: testing
body: |
This here's a test post containing various markdown elements in its body.
It's useful for making sure that posts will look good (generally).
## Let's Begin
There's various things worth testing. _Emphasized_ and **bold** text are
great starting points. Also `little bits of code`.
One might consider making a list of them.
* A bit normal.
* _A bit emphasized_
* **A bit bold**
* `A bit of code.`
So many!
### A Subsection
Crazy. Another way to delineate a subsection is with a horizontal rule.
-----
And it only gets crazier from here!
Check out this code block.
```
// It's like actually being in the matrix
for !dead {
if awake {
work()
} else {
continue
}
}
```
Edgy.
#### Side-note
Did you know that the terms "cyberspace" and "matrix" are attributable to a book from 1984 called _Neuromancer_?
> The 1999 cyberpunk science fiction film The Matrix particularly draws from Neuromancer both eponym and usage of the term "matrix".
> - Wikipedia
Here's a real picture of cyberspace.
![not a sound stage]({{ AssetURL "galaxy.jpg" }})
This has been a great post.
- id: empty-test
title: Empty Test
description: A post with no content. Might as well test it.
tags:
- foo
- bar
series: testing
body: ""
- id: little-markdown-test
title: Little Markdown Test
description: A post with almost no content.
tags:
- bar
series: testing
body: |
This page is almost empty.
assets:
galaxy.jpg: ./galaxy.jpg

View File

@ -22,4 +22,5 @@ require (
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

View File

@ -256,4 +256,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=