make store a struct and add tests

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-25 15:17:35 -07:00 committed by Solomon Hykes
parent 524f77df65
commit 9fec69f3a0
8 changed files with 85 additions and 49 deletions

View File

@ -22,11 +22,11 @@ var downCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
// nolint:staticcheck
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(lg, cmd) routeName := getRouteName(lg, cmd)
route, err := dagger.LookupRoute(ctx, routeName, nil) route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil { if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route") lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route")
} }

View File

@ -23,8 +23,9 @@ var listCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routes, err := dagger.ListRoutes(ctx) routes, err := store.ListRoutes(ctx)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("cannot list routes") lg.Fatal().Err(err).Msg("cannot list routes")
} }

View File

@ -22,18 +22,17 @@ var newCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
// nolint:staticcheck
upRouteFlag, err := cmd.Flags().GetBool("up") upRouteFlag, err := cmd.Flags().GetBool("up")
if err != nil { if err != nil {
lg.Fatal().Err(err).Str("flag", "up").Msg("unable to resolve flag") lg.Fatal().Err(err).Str("flag", "up").Msg("unable to resolve flag")
} }
// nolint:staticcheck
routeName := getRouteName(lg, cmd) routeName := getRouteName(lg, cmd)
// TODO: Implement options: --layout-*, --setup // TODO: Implement options: --layout-*, --setup
route, err := dagger.CreateRoute(ctx, routeName, nil) route, err := store.CreateRoute(ctx, routeName, nil)
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to create route") lg.Fatal().Err(err).Msg("failed to create route")
} }

View File

@ -23,11 +23,11 @@ var queryCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
// nolint:staticcheck
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(lg, cmd) routeName := getRouteName(lg, cmd)
route, err := dagger.LookupRoute(ctx, routeName, nil) route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil { if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route") lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route")
} }

View File

@ -22,11 +22,11 @@ var upCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
lg := logger.New() lg := logger.New()
// nolint:staticcheck
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(lg, cmd) routeName := getRouteName(lg, cmd)
route, err := dagger.LookupRoute(ctx, routeName, nil) route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil { if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route") lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route")
} }

View File

@ -4,22 +4,35 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil"
"os" "os"
"path" "path"
"strings" "path/filepath"
"github.com/google/uuid" "github.com/google/uuid"
) )
const ( const (
storeLocation = "$HOME/.config/dagger/routes" defaultStoreRoot = "$HOME/.config/dagger/routes"
) )
type Store struct {
root string
}
func NewStore(root string) *Store {
return &Store{
root: root,
}
}
func DefaultStore() *Store {
return NewStore(os.ExpandEnv(defaultStoreRoot))
}
type CreateOpts struct{} type CreateOpts struct{}
func CreateRoute(ctx context.Context, name string, o *CreateOpts) (*Route, error) { func (s *Store) CreateRoute(ctx context.Context, name string, o *CreateOpts) (*Route, error) {
r, err := LookupRoute(ctx, name, &LookupOpts{}) r, err := s.LookupRoute(ctx, name, &LookupOpts{})
if err != nil && !errors.Is(err, os.ErrNotExist) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err return nil, err
} }
@ -36,50 +49,53 @@ func CreateRoute(ctx context.Context, name string, o *CreateOpts) (*Route, error
return nil, err return nil, err
} }
return r, syncRoute(r) return r, s.syncRoute(r)
} }
type UpdateOpts struct{} type UpdateOpts struct{}
func UpdateRoute(ctx context.Context, r *Route, o *UpdateOpts) error { func (s *Store) UpdateRoute(ctx context.Context, r *Route, o *UpdateOpts) error {
return syncRoute(r) return s.syncRoute(r)
} }
type DeleteOpts struct{} type DeleteOpts struct{}
func DeleteRoute(ctx context.Context, r *Route, o *DeleteOpts) error { func (s *Store) DeleteRoute(ctx context.Context, r *Route, o *DeleteOpts) error {
return deleteRoute(r) return os.Remove(s.routePath(r.st.Name))
} }
type LookupOpts struct{} type LookupOpts struct{}
func LookupRoute(ctx context.Context, name string, o *LookupOpts) (*Route, error) { func (s *Store) LookupRoute(ctx context.Context, name string, o *LookupOpts) (*Route, error) {
st, err := loadRoute(name) data, err := os.ReadFile(s.routePath(name))
if err != nil { if err != nil {
return nil, err return nil, err
} }
var st RouteState
if err := json.Unmarshal(data, &st); err != nil {
return nil, err
}
return &Route{ return &Route{
st: st, st: &st,
}, nil }, nil
} }
type LoadOpts struct{} type LoadOpts struct{}
func LoadRoute(ctx context.Context, id string, o *LoadOpts) (*Route, error) { func (s *Store) LoadRoute(ctx context.Context, id string, o *LoadOpts) (*Route, error) {
panic("NOT IMPLEMENTED") panic("NOT IMPLEMENTED")
} }
func ListRoutes(ctx context.Context) ([]string, error) { func (s *Store) ListRoutes(ctx context.Context) ([]string, error) {
routes := []string{} routes := []string{}
rootDir := os.ExpandEnv(storeLocation) files, err := os.ReadDir(s.root)
files, err := ioutil.ReadDir(rootDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, f := range files { for _, f := range files {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") { if f.IsDir() || filepath.Ext(f.Name()) != ".json" {
// There is extra data in the directory, ignore // There is extra data in the directory, ignore
continue continue
} }
@ -89,12 +105,12 @@ func ListRoutes(ctx context.Context) ([]string, error) {
return routes, nil return routes, nil
} }
func routePath(name string) string { func (s *Store) routePath(name string) string {
return path.Join(os.ExpandEnv(storeLocation), name+".json") return path.Join(s.root, name+".json")
} }
func syncRoute(r *Route) error { func (s *Store) syncRoute(r *Route) error {
p := routePath(r.st.Name) p := s.routePath(r.st.Name)
if err := os.MkdirAll(path.Dir(p), 0755); err != nil { if err := os.MkdirAll(path.Dir(p), 0755); err != nil {
return err return err
@ -107,19 +123,3 @@ func syncRoute(r *Route) error {
return os.WriteFile(p, data, 0644) return os.WriteFile(p, data, 0644)
} }
func deleteRoute(r *Route) error {
return os.Remove(routePath(r.st.Name))
}
func loadRoute(name string) (*RouteState, error) {
data, err := os.ReadFile(routePath(name))
if err != nil {
return nil, err
}
var st *RouteState
if err := json.Unmarshal(data, st); err != nil {
return nil, err
}
return st, nil
}

37
dagger/store_test.go Normal file
View File

@ -0,0 +1,37 @@
package dagger
import (
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestStore(t *testing.T) {
ctx := context.TODO()
root, err := os.MkdirTemp(os.TempDir(), "dagger-*")
require.NoError(t, err)
store := NewStore(root)
_, err = store.LookupRoute(ctx, "notexist", nil)
require.Error(t, err)
require.True(t, errors.Is(err, os.ErrNotExist))
r, err := store.CreateRoute(ctx, "test", nil)
require.NoError(t, err)
require.NotNil(t, r)
require.Equal(t, "test", r.Name())
r, err = store.LookupRoute(ctx, "test", nil)
require.NoError(t, err)
require.NotNil(t, r)
require.Equal(t, "test", r.Name())
routes, err := store.ListRoutes(ctx)
require.NoError(t, err)
require.Len(t, routes, 1)
require.Equal(t, "test", routes[0])
}

View File

@ -25,6 +25,5 @@ todoApp: netlify.#Site & {
contents: yarn.#Script & { contents: yarn.#Script & {
source: repository source: repository
run: "build" run: "build"
env: "xx" :"bar"
} }
} }