summaryrefslogtreecommitdiff
path: root/srv/src/cmd/import-asset/main.go
blob: 611bb1c3b3e39f2ee19fb0359137d331ae43e37b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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")
	}
}