~mariusor/motley

f9aa53d510c39838f695b450ea9c0b2571ed15d3 — Marius Orcsik 4 months ago a261692
Improve fs storage to allow for oni storage
4 files changed, 41 insertions(+), 18 deletions(-)

M cmd/motley/main.go
M go.mod
M internal/cmd/storage_all.go
M internal/config/config.go
M cmd/motley/main.go => cmd/motley/main.go +19 -8
@@ 1,19 1,21 @@
package main

import (
	xerrors "errors"
	"fmt"
	"git.sr.ht/~marius/motley/internal/cmd"
	"git.sr.ht/~marius/motley/internal/config"
	"git.sr.ht/~marius/motley/internal/env"
	"github.com/alecthomas/kong"
	"github.com/go-ap/fedbox"
	"github.com/sirupsen/logrus"
	"io"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"

	"git.sr.ht/~marius/motley/internal/cmd"
	"git.sr.ht/~marius/motley/internal/config"
	"git.sr.ht/~marius/motley/internal/env"
	"github.com/alecthomas/kong"
	"github.com/go-ap/fedbox"
	"github.com/sirupsen/logrus"
)

var version = "HEAD"


@@ 53,6 55,7 @@ func main() {
	stores, err := loadArguments(&conf)
	if err != nil {
		l.Errorf("%s", err)
		fmt.Fprintln(os.Stderr, err)
		ktx.Exit(1)
	}



@@ 86,7 89,7 @@ func loadArguments(conf *config.Options) ([]fedbox.FullStorage, error) {
		return nil, fmt.Errorf("invalid flags: when passing storage DSN you need an associated URL for each of them")
	}

	m := make(multiError, 0)
	m := make([]error, 0)
	stores := make([]fedbox.FullStorage, 0)
	for _, dsnConfig := range Motley.Config {
		if dsnConfig == "" {


@@ 112,6 115,14 @@ func loadArguments(conf *config.Options) ([]fedbox.FullStorage, error) {
			m = append(m, fmt.Errorf("unable to access storage: %s", err))
			continue
		}

		o, err := url.ParseRequestURI(c.BaseURL)
		if err != nil {
			m = append(m, fmt.Errorf("invalid url passed: %s", err))
			continue
		}
		conf.Host = o.Host

		*conf = c
		stores = append(stores, db)
	}


@@ 155,7 166,7 @@ func loadArguments(conf *config.Options) ([]fedbox.FullStorage, error) {
		stores = append(stores, db)
	}
	if len(m) > 0 {
		return nil, m
		return nil, xerrors.Join(m...)
	}
	return stores, nil
}

M go.mod => go.mod +1 -1
@@ 1,6 1,6 @@
module git.sr.ht/~marius/motley

go 1.19
go 1.21

require (
	github.com/alecthomas/kong v0.8.0

M internal/cmd/storage_all.go => internal/cmd/storage_all.go +10 -5
@@ 1,15 1,16 @@
package cmd

import (
	badger "github.com/go-ap/storage-badger"
	boltdb "github.com/go-ap/storage-boltdb"
	fs "github.com/go-ap/storage-fs"
	sqlite "github.com/go-ap/storage-sqlite"
	iofs "io/fs"
	"path/filepath"

	"git.sr.ht/~marius/motley/internal/config"
	"github.com/go-ap/errors"
	"github.com/go-ap/fedbox"
	badger "github.com/go-ap/storage-badger"
	boltdb "github.com/go-ap/storage-boltdb"
	fs "github.com/go-ap/storage-fs"
	sqlite "github.com/go-ap/storage-sqlite"
	"github.com/sirupsen/logrus"
)



@@ 84,7 85,11 @@ func getFsStorageAtPath(dir, url string, l logrus.FieldLogger) (fedbox.FullStora
func getFsStorage(c config.Options, l logrus.FieldLogger) (fedbox.FullStorage, error) {
	path, err := c.BaseStoragePath()
	if err != nil {
		return nil, err
		var pathError *iofs.PathError
		if !errors.As(err, &pathError) {
			return nil, err
		}
		path = c.StoragePath
	}
	l.Debugf("Initializing fs storage at %s", path)
	db, err := getFsStorageAtPath(filepath.Dir(path), c.BaseURL, l)

M internal/config/config.go => internal/config/config.go +11 -4
@@ 2,6 2,7 @@ package config

import (
	"fmt"
	"net/url"
	"os"
	"os/user"
	"path/filepath"


@@ 198,11 199,17 @@ func LoadFromEnv(base string, e env.Type, timeOut time.Duration) (Options, error
		conf.TimeOut = to
	}
	if conf.Host != "" {
		conf.Secure, _ = strconv.ParseBool(loadKeyFromEnv(KeyHTTPS, "false"))
		if conf.Secure {
			conf.BaseURL = fmt.Sprintf("https://%s", conf.Host)
		if u, err := url.ParseRequestURI(conf.Host); err != nil {
			conf.Secure, _ = strconv.ParseBool(loadKeyFromEnv(KeyHTTPS, "true"))
			proto := "http"
			if conf.Secure {
				proto = "https"
			}
			conf.BaseURL = fmt.Sprintf("%s://%s", proto, conf.Host)
		} else {
			conf.BaseURL = fmt.Sprintf("http://%s", conf.Host)
			conf.Secure = u.Scheme == "https"
			conf.Host = u.Host
			conf.BaseURL = u.String()
		}
	}
	conf.KeyPath = loadKeyFromEnv(KeyKeyPath, "")