Skip to content

Commit

Permalink
Adds Local store.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Leduc-Hamel committed Mar 19, 2021
1 parent d19c369 commit f9e2292
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
10 changes: 1 addition & 9 deletions pkg/app/trieugene.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@ func (t *trieugene) Run(ctx context.Context) error {
}

func (t *trieugeneDev) Run(ctx context.Context) error {
store := store.NewS3(&store.S3Params{
AccessKey: t.cfg.S3AccessKey(),
SecretKey: t.cfg.S3SecretKey(),
URL: t.cfg.S3URL(),
Bucket: t.cfg.S3Bucket(),
Region: t.cfg.S3Region(),
DisableSSL: true,
S3ForcePathStyle: true,
})
store := store.NewLocal(t.cfg.LocalPrefix())

run(setupDevelopment(t.cfg))

Expand Down
5 changes: 5 additions & 0 deletions pkg/config/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -65,6 +66,10 @@ func (c *Config) HTTPPort() int {
return c.httpPort
}

func (c *Config) LocalPrefix() string {
return fmt.Sprintf("%s/.cfg/trieugene", GetEnv("HOME", "/tmp"))
}

func (c *Config) Logger() *zerolog.Logger {
return c.logger
}
Expand Down
44 changes: 44 additions & 0 deletions pkg/store/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package store

import (
"context"
"io/ioutil"
"os"
"path"
)

type Local struct {
prefix string
}

func NewLocal(prefix string) Store {
return &Local{prefix: prefix}
}

func (l *Local) Setup(ctx context.Context) error {
err := os.MkdirAll(l.prefix, os.ModePerm)
if err != nil {
return err
}
return nil
}

func (s *Local) Persist(ctx context.Context, filename string, data string) error {
dataAsByte := []byte(data)

filenameWithPrefix := path.Join(s.prefix, filename)

dirName := path.Dir(filenameWithPrefix)

err := os.MkdirAll(dirName, os.ModePerm)
if err != nil {
return err
}

err = ioutil.WriteFile(filenameWithPrefix, dataAsByte, 0644)
if err != nil {
return err
}

return nil
}

0 comments on commit f9e2292

Please sign in to comment.