Implement import-asset script
This commit is contained in:
parent
4d23325823
commit
f9d1f664f0
114
srv/src/cmd/import-asset/main.go
Normal file
114
srv/src/cmd/import-asset/main.go
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
18
srv/src/cmd/import-asset/to-import.txt
Normal file
18
srv/src/cmd/import-asset/to-import.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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
|
@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/adrg/frontmatter"
|
"github.com/adrg/frontmatter"
|
||||||
"github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
|
||||||
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
|
||||||
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
||||||
@ -107,7 +106,7 @@ func main() {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
cfg := cfg.NewBlogCfg(cfg.Params{})
|
cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{})
|
||||||
|
|
||||||
var dataDir cfgpkg.DataDir
|
var dataDir cfgpkg.DataDir
|
||||||
dataDir.SetupCfg(cfg)
|
dataDir.SetupCfg(cfg)
|
||||||
|
Loading…
Reference in New Issue
Block a user