feat: restructure for better reuse

This commit is contained in:
Eugen Eisler 2024-10-12 22:37:35 +03:00
parent 17bde814cc
commit 56f995afb4
21 changed files with 63 additions and 62 deletions

View File

@ -2,6 +2,7 @@ package cli
import ( import (
"fmt" "fmt"
"github.com/danielmiessler/fabric/db/fs"
"github.com/danielmiessler/fabric/plugins/tools/converter" "github.com/danielmiessler/fabric/plugins/tools/converter"
"github.com/danielmiessler/fabric/restapi" "github.com/danielmiessler/fabric/restapi"
"os" "os"
@ -10,7 +11,6 @@ import (
"strings" "strings"
"github.com/danielmiessler/fabric/core" "github.com/danielmiessler/fabric/core"
"github.com/danielmiessler/fabric/db"
) )
// Cli Controls the cli. It takes in the flags and runs the appropriate functions // Cli Controls the cli. It takes in the flags and runs the appropriate functions
@ -30,7 +30,7 @@ func Cli(version string) (err error) {
return return
} }
fabricDb := db.NewDb(filepath.Join(homedir, ".config/fabric")) fabricDb := fs.NewDb(filepath.Join(homedir, ".config/fabric"))
// if the setup flag is set, run the setup function // if the setup flag is set, run the setup function
if currentFlags.Setup || currentFlags.SetupSkipPatterns || currentFlags.SetupVendor != "" { if currentFlags.Setup || currentFlags.SetupSkipPatterns || currentFlags.SetupVendor != "" {
@ -213,7 +213,7 @@ func Cli(version string) (err error) {
return return
} }
var session *db.Session var session *fs.Session
if session, err = chatter.Send(currentFlags.BuildChatRequest(strings.Join(os.Args[1:], " ")), currentFlags.BuildChatOptions()); err != nil { if session, err = chatter.Send(currentFlags.BuildChatRequest(strings.Join(os.Args[1:], " ")), currentFlags.BuildChatOptions()); err != nil {
return return
} }
@ -244,7 +244,7 @@ func Cli(version string) (err error) {
return return
} }
func Setup(db *db.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) { func Setup(db *fs.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) {
instance := core.NewFabricForSetup(db) instance := core.NewFabricForSetup(db)
if err = instance.Setup(); err != nil { if err = instance.Setup(); err != nil {
@ -260,7 +260,7 @@ func Setup(db *db.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) {
return return
} }
func SetupVendor(db *db.Db, vendorName string) (ret *core.Fabric, err error) { func SetupVendor(db *fs.Db, vendorName string) (ret *core.Fabric, err error) {
ret = core.NewFabricForSetup(db) ret = core.NewFabricForSetup(db)
if err = ret.SetupVendor(vendorName); err != nil { if err = ret.SetupVendor(vendorName); err != nil {
return return

View File

@ -2,10 +2,10 @@ package cli
import ( import (
"github.com/danielmiessler/fabric/core" "github.com/danielmiessler/fabric/core"
"github.com/danielmiessler/fabric/db/fs"
"os" "os"
"testing" "testing"
"github.com/danielmiessler/fabric/db"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -21,7 +21,7 @@ func TestCli(t *testing.T) {
} }
func TestSetup(t *testing.T) { func TestSetup(t *testing.T) {
mockDB := db.NewDb(os.TempDir()) mockDB := fs.NewDb(os.TempDir())
fabric, err := Setup(mockDB, false) fabric, err := Setup(mockDB, false)
assert.Error(t, err) assert.Error(t, err)

View File

@ -4,14 +4,14 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/danielmiessler/fabric/common" "github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/db/fs"
"github.com/danielmiessler/fabric/plugins/ai" "github.com/danielmiessler/fabric/plugins/ai"
goopenai "github.com/sashabaranov/go-openai" goopenai "github.com/sashabaranov/go-openai"
"strings" "strings"
) )
type Chatter struct { type Chatter struct {
db *db.Db db *fs.Db
Stream bool Stream bool
DryRun bool DryRun bool
@ -20,7 +20,7 @@ type Chatter struct {
vendor ai.Vendor vendor ai.Vendor
} }
func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (session *db.Session, err error) { func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (session *fs.Session, err error) {
if session, err = o.BuildSession(request, opts.Raw); err != nil { if session, err = o.BuildSession(request, opts.Raw); err != nil {
return return
} }
@ -62,16 +62,16 @@ func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (s
return return
} }
func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *db.Session, err error) { func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *fs.Session, err error) {
if request.SessionName != "" { if request.SessionName != "" {
var sess *db.Session var sess *fs.Session
if sess, err = o.db.Sessions.Get(request.SessionName); err != nil { if sess, err = o.db.Sessions.Get(request.SessionName); err != nil {
err = fmt.Errorf("could not find session %s: %v", request.SessionName, err) err = fmt.Errorf("could not find session %s: %v", request.SessionName, err)
return return
} }
session = sess session = sess
} else { } else {
session = &db.Session{} session = &fs.Session{}
} }
if request.Meta != "" { if request.Meta != "" {
@ -80,7 +80,7 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
var contextContent string var contextContent string
if request.ContextName != "" { if request.ContextName != "" {
var ctx *db.Context var ctx *fs.Context
if ctx, err = o.db.Contexts.Get(request.ContextName); err != nil { if ctx, err = o.db.Contexts.Get(request.ContextName); err != nil {
err = fmt.Errorf("could not find context %s: %v", request.ContextName, err) err = fmt.Errorf("could not find context %s: %v", request.ContextName, err)
return return
@ -90,7 +90,7 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
var patternContent string var patternContent string
if request.PatternName != "" { if request.PatternName != "" {
var pattern *db.Pattern var pattern *fs.Pattern
if pattern, err = o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables); err != nil { if pattern, err = o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables); err != nil {
err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err) err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err)
return return

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"github.com/atotto/clipboard" "github.com/atotto/clipboard"
"github.com/danielmiessler/fabric/common" "github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/db/fs"
"github.com/danielmiessler/fabric/plugins/ai/anthropic" "github.com/danielmiessler/fabric/plugins/ai/anthropic"
"github.com/danielmiessler/fabric/plugins/ai/azure" "github.com/danielmiessler/fabric/plugins/ai/azure"
"github.com/danielmiessler/fabric/plugins/ai/dryrun" "github.com/danielmiessler/fabric/plugins/ai/dryrun"
@ -28,20 +28,20 @@ const DefaultPatternsGitRepoFolder = "patterns"
const NoSessionPatternUserMessages = "no session, pattern or user messages provided" const NoSessionPatternUserMessages = "no session, pattern or user messages provided"
func NewFabric(db *db.Db) (ret *Fabric, err error) { func NewFabric(db *fs.Db) (ret *Fabric, err error) {
ret = NewFabricBase(db) ret = NewFabricBase(db)
err = ret.Configure() err = ret.Configure()
return return
} }
func NewFabricForSetup(db *db.Db) (ret *Fabric) { func NewFabricForSetup(db *fs.Db) (ret *Fabric) {
ret = NewFabricBase(db) ret = NewFabricBase(db)
_ = ret.Configure() _ = ret.Configure()
return return
} }
// NewFabricBase Create a new Fabric from a list of already configured VendorsController // NewFabricBase Create a new Fabric from a list of already configured VendorsController
func NewFabricBase(db *db.Db) (ret *Fabric) { func NewFabricBase(db *fs.Db) (ret *Fabric) {
ret = &Fabric{ ret = &Fabric{
VendorsManager: NewVendorsManager(), VendorsManager: NewVendorsManager(),
@ -77,7 +77,7 @@ type Fabric struct {
*youtube.YouTube *youtube.YouTube
Jina *jina.Client Jina *jina.Client
Db *db.Db Db *fs.Db
DefaultVendor *common.Setting DefaultVendor *common.Setting
DefaultModel *common.SetupQuestion DefaultModel *common.SetupQuestion

View File

@ -1,21 +1,20 @@
package core package core
import ( import (
"github.com/danielmiessler/fabric/db/fs"
"os" "os"
"testing" "testing"
"github.com/danielmiessler/fabric/db"
) )
func TestNewFabric(t *testing.T) { func TestNewFabric(t *testing.T) {
_, err := NewFabric(db.NewDb(os.TempDir())) _, err := NewFabric(fs.NewDb(os.TempDir()))
if err == nil { if err == nil {
t.Fatal("without setup error expected") t.Fatal("without setup error expected")
} }
} }
func TestSaveEnvFile(t *testing.T) { func TestSaveEnvFile(t *testing.T) {
fabric := NewFabricBase(db.NewDb(os.TempDir())) fabric := NewFabricBase(fs.NewDb(os.TempDir()))
err := fabric.SaveEnvFile() err := fabric.SaveEnvFile()
if err != nil { if err != nil {
@ -25,7 +24,7 @@ func TestSaveEnvFile(t *testing.T) {
func TestCopyToClipboard(t *testing.T) { func TestCopyToClipboard(t *testing.T) {
t.Skip("skipping test, because of docker env. in ci.") t.Skip("skipping test, because of docker env. in ci.")
fabric := NewFabricBase(db.NewDb(os.TempDir())) fabric := NewFabricBase(fs.NewDb(os.TempDir()))
message := "test message" message := "test message"
err := fabric.CopyToClipboard(message) err := fabric.CopyToClipboard(message)
@ -35,7 +34,7 @@ func TestCopyToClipboard(t *testing.T) {
} }
func TestCreateOutputFile(t *testing.T) { func TestCreateOutputFile(t *testing.T) {
mockDb := &db.Db{} mockDb := &fs.Db{}
fabric := NewFabricBase(mockDb) fabric := NewFabricBase(mockDb)
fileName := "test_output.txt" fileName := "test_output.txt"

View File

@ -2,6 +2,7 @@ package core
import ( import (
"fmt" "fmt"
"github.com/danielmiessler/fabric/db/fs"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -9,7 +10,6 @@ import (
"strings" "strings"
"github.com/danielmiessler/fabric/common" "github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
@ -17,7 +17,7 @@ import (
"github.com/otiai10/copy" "github.com/otiai10/copy"
) )
func NewPatternsLoader(patterns *db.PatternsEntity) (ret *PatternsLoader) { func NewPatternsLoader(patterns *fs.PatternsEntity) (ret *PatternsLoader) {
label := "Patterns Loader" label := "Patterns Loader"
ret = &PatternsLoader{ ret = &PatternsLoader{
Patterns: patterns, Patterns: patterns,
@ -42,7 +42,7 @@ func NewPatternsLoader(patterns *db.PatternsEntity) (ret *PatternsLoader) {
type PatternsLoader struct { type PatternsLoader struct {
*common.Configurable *common.Configurable
Patterns *db.PatternsEntity Patterns *fs.PatternsEntity
DefaultGitRepoUrl *common.SetupQuestion DefaultGitRepoUrl *common.SetupQuestion
DefaultFolder *common.SetupQuestion DefaultFolder *common.SetupQuestion
@ -156,7 +156,7 @@ func (o *PatternsLoader) gitCloneAndCopy() (err error) {
return err return err
} }
var changes []db.DirectoryChange var changes []fs.DirectoryChange
// ... iterates over the commits // ... iterates over the commits
if err = cIter.ForEach(func(c *object.Commit) (err error) { if err = cIter.ForEach(func(c *object.Commit) (err error) {
// GetApplyVariables the files changed in this commit by comparing with its parents // GetApplyVariables the files changed in this commit by comparing with its parents
@ -171,7 +171,7 @@ func (o *PatternsLoader) gitCloneAndCopy() (err error) {
for _, fileStat := range patch.Stats() { for _, fileStat := range patch.Stats() {
if strings.HasPrefix(fileStat.Name, o.pathPatternsPrefix) { if strings.HasPrefix(fileStat.Name, o.pathPatternsPrefix) {
dir := filepath.Dir(fileStat.Name) dir := filepath.Dir(fileStat.Name)
changes = append(changes, db.DirectoryChange{Dir: dir, Timestamp: c.Committer.When}) changes = append(changes, fs.DirectoryChange{Dir: dir, Timestamp: c.Committer.When})
} }
} }
return return
@ -256,7 +256,7 @@ func (o *PatternsLoader) writeBlobToFile(blob *object.Blob, path string) (err er
return return
} }
func (o *PatternsLoader) makeUniqueList(changes []db.DirectoryChange) (err error) { func (o *PatternsLoader) makeUniqueList(changes []fs.DirectoryChange) (err error) {
uniqueItems := make(map[string]bool) uniqueItems := make(map[string]bool)
for _, change := range changes { for _, change := range changes {
if strings.TrimSpace(change.Dir) != "" && !strings.Contains(change.Dir, "=>") { if strings.TrimSpace(change.Dir) != "" && !strings.Contains(change.Dir, "=>") {

View File

@ -1,4 +1,4 @@
package db package fs
import "fmt" import "fmt"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"os" "os"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"os" "os"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"fmt" "fmt"

1
db/fs/patterns_test.go Normal file
View File

@ -0,0 +1 @@
package fs

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"testing" "testing"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package db package fs
import ( import (
"testing" "testing"

View File

@ -1 +0,0 @@
package db

View File

@ -1,19 +1,19 @@
package restapi package restapi
import ( import (
"github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// ContextsHandler defines the handler for contexts-related operations // ContextsHandler defines the handler for contexts-related operations
type ContextsHandler struct { type ContextsHandler struct {
*StorageHandler[db.Context] *StorageHandler[fs.Context]
contexts *db.ContextsEntity contexts *fs.ContextsEntity
} }
// NewContextsHandler creates a new ContextsHandler // NewContextsHandler creates a new ContextsHandler
func NewContextsHandler(r *gin.Engine, contexts *db.ContextsEntity) (ret *ContextsHandler) { func NewContextsHandler(r *gin.Engine, contexts *fs.ContextsEntity) (ret *ContextsHandler) {
ret = &ContextsHandler{ ret = &ContextsHandler{
StorageHandler: NewStorageHandler[db.Context](r, "contexts", contexts), contexts: contexts} StorageHandler: NewStorageHandler[fs.Context](r, "contexts", contexts), contexts: contexts}
return return
} }

View File

@ -1,27 +1,29 @@
package restapi package restapi
import ( import (
"github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
) )
// PatternsHandler defines the handler for patterns-related operations // PatternsHandler defines the handler for patterns-related operations
type PatternsHandler struct { type PatternsHandler struct {
*StorageHandler[db.Pattern] *StorageHandler[fs.Pattern]
patterns *db.PatternsEntity patterns *fs.PatternsEntity
} }
// NewPatternsHandler creates a new PatternsHandler // NewPatternsHandler creates a new PatternsHandler
func NewPatternsHandler(r *gin.Engine, patterns *db.PatternsEntity) (ret *PatternsHandler) { func NewPatternsHandler(r *gin.Engine, patterns *fs.PatternsEntity) (ret *PatternsHandler) {
ret = &PatternsHandler{ ret = &PatternsHandler{
StorageHandler: NewStorageHandler[db.Pattern](r, "patterns", patterns), patterns: patterns} StorageHandler: NewStorageHandler[fs.Pattern](r, "patterns", patterns), patterns: patterns}
r.GET("/patterns/:name", ret.GetPattern)
// TODO: Add custom, replacement routes here
//r.GET("/patterns/:name", ret.Get)
return return
} }
// GetPattern handles the GET /patterns/:name route // Get handles the GET /patterns/:name route
func (h *PatternsHandler) GetPattern(c *gin.Context) { func (h *PatternsHandler) Get(c *gin.Context) {
name := c.Param("name") name := c.Param("name")
variables := make(map[string]string) // Assuming variables are passed somehow variables := make(map[string]string) // Assuming variables are passed somehow
pattern, err := h.patterns.GetApplyVariables(name, variables) pattern, err := h.patterns.GetApplyVariables(name, variables)

View File

@ -1,11 +1,11 @@
package restapi package restapi
import ( import (
"github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func Serve(fabricDb *db.Db, address string) (err error) { func Serve(fabricDb *fs.Db, address string) (err error) {
r := gin.Default() r := gin.Default()
// Middleware // Middleware

View File

@ -1,19 +1,19 @@
package restapi package restapi
import ( import (
"github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// SessionsHandler defines the handler for sessions-related operations // SessionsHandler defines the handler for sessions-related operations
type SessionsHandler struct { type SessionsHandler struct {
*StorageHandler[db.Session] *StorageHandler[fs.Session]
sessions *db.SessionsEntity sessions *fs.SessionsEntity
} }
// NewSessionsHandler creates a new SessionsHandler // NewSessionsHandler creates a new SessionsHandler
func NewSessionsHandler(r *gin.Engine, sessions *db.SessionsEntity) (ret *SessionsHandler) { func NewSessionsHandler(r *gin.Engine, sessions *fs.SessionsEntity) (ret *SessionsHandler) {
ret = &SessionsHandler{ ret = &SessionsHandler{
StorageHandler: NewStorageHandler[db.Session](r, "sessions", sessions), sessions: sessions} StorageHandler: NewStorageHandler[fs.Session](r, "sessions", sessions), sessions: sessions}
return ret return ret
} }