diff --git a/.gitignore b/.gitignore index 3a86718..9161861 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ blas +!blas/ !cmd/blas/ Session.vim coverage.txt diff --git a/pkg/blas/blas.go b/pkg/blas/blas.go new file mode 100644 index 0000000..45e13b3 --- /dev/null +++ b/pkg/blas/blas.go @@ -0,0 +1,60 @@ +package blas + +import ( + "context" + "os" + "path" + "strings" + + "dynatron.me/x/blasphem/internal/common" + "dynatron.me/x/blasphem/pkg/bus" + "dynatron.me/x/blasphem/pkg/config" + "dynatron.me/x/blasphem/pkg/storage" +) + +type Blas struct { + *bus.Bus + storage.Store + Config *config.Config +} + +func (b *Blas) Shutdown(ctx context.Context) error { + b.Bus.Shutdown() + return ctx.Err() +} + +func (b *Blas) ConfigDir() (cd string) { + if b.Config.DataDir != nil { + cd = *b.Config.DataDir + } + + home, err := os.UserHomeDir() + if err != nil { + panic(err) + } + + switch { + case cd == "": + return path.Join(home, "."+common.AppName) + case strings.HasPrefix(cd, "~/"): + return path.Join(home, cd[2:]) + default: + return cd + } +} + +func (b *Blas) openStore() error { + stor, err := storage.Open(os.DirFS(b.ConfigDir())) + b.Store = stor + return err +} + +func New(cfg *config.Config) (*Blas, error) { + b := &Blas{ + Bus: bus.New(), + Config: cfg, + } + + err := b.openStore() + return b, err +}