Slight lmcli package refactor
- Moved utility functions from lmcli.go to tools.go This is preparing for `lmcli` package API refactoring
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package lmcli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -45,32 +43,6 @@ func NewContext() (*Context, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
store, err := getConversationService()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chroma := tty.NewChromaHighlighter("markdown", *config.Chroma.Formatter, *config.Chroma.Style)
|
||||
return &Context{*config, store, *chroma}, nil
|
||||
}
|
||||
|
||||
func createOrOpenAppend(path string) (*os.File, error) {
|
||||
var file *os.File
|
||||
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||
file, err = os.Create(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
file, err = os.OpenFile(path, os.O_APPEND, fs.ModeAppend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func getConversationService() (conversation.Repo, error) {
|
||||
databaseFile := filepath.Join(dataDir(), "conversations.db")
|
||||
gormLogFile, err := createOrOpenAppend(filepath.Join(dataDir(), "database.log"))
|
||||
if err != nil {
|
||||
@@ -87,13 +59,18 @@ func getConversationService() (conversation.Repo, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error establishing connection to store: %v", err)
|
||||
}
|
||||
|
||||
repo, err := conversation.NewRepo(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repo, nil
|
||||
|
||||
// Initialize chroma
|
||||
chroma := tty.NewChromaHighlighter("markdown", *config.Chroma.Formatter, *config.Chroma.Style)
|
||||
return &Context{*config, repo, *chroma}, nil
|
||||
}
|
||||
|
||||
|
||||
func (c *Context) GetModels() (models []string) {
|
||||
modelCounts := make(map[string]int)
|
||||
for _, p := range c.Config.Providers {
|
||||
@@ -228,36 +205,6 @@ func (c *Context) GetModelProvider(model string, provider string) (string, strin
|
||||
return "", "", nil, fmt.Errorf("unknown model: %s", model)
|
||||
}
|
||||
|
||||
func configDir() string {
|
||||
var configDir string
|
||||
|
||||
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if xdgConfigHome != "" {
|
||||
configDir = filepath.Join(xdgConfigHome, "lmcli")
|
||||
} else {
|
||||
userHomeDir, _ := os.UserHomeDir()
|
||||
configDir = filepath.Join(userHomeDir, ".config/lmcli")
|
||||
}
|
||||
|
||||
os.MkdirAll(configDir, 0755)
|
||||
return configDir
|
||||
}
|
||||
|
||||
func dataDir() string {
|
||||
var dataDir string
|
||||
|
||||
xdgDataHome := os.Getenv("XDG_DATA_HOME")
|
||||
if xdgDataHome != "" {
|
||||
dataDir = filepath.Join(xdgDataHome, "lmcli")
|
||||
} else {
|
||||
userHomeDir, _ := os.UserHomeDir()
|
||||
dataDir = filepath.Join(userHomeDir, ".local/share/lmcli")
|
||||
}
|
||||
|
||||
os.MkdirAll(dataDir, 0755)
|
||||
return dataDir
|
||||
}
|
||||
|
||||
func Fatal(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
os.Exit(1)
|
||||
|
||||
55
pkg/lmcli/tools.go
Normal file
55
pkg/lmcli/tools.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package lmcli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func createOrOpenAppend(path string) (*os.File, error) {
|
||||
var file *os.File
|
||||
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||
file, err = os.Create(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
file, err = os.OpenFile(path, os.O_APPEND, fs.ModeAppend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func configDir() string {
|
||||
var configDir string
|
||||
|
||||
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if xdgConfigHome != "" {
|
||||
configDir = filepath.Join(xdgConfigHome, "lmcli")
|
||||
} else {
|
||||
userHomeDir, _ := os.UserHomeDir()
|
||||
configDir = filepath.Join(userHomeDir, ".config/lmcli")
|
||||
}
|
||||
|
||||
os.MkdirAll(configDir, 0755)
|
||||
return configDir
|
||||
}
|
||||
|
||||
func dataDir() string {
|
||||
var dataDir string
|
||||
|
||||
xdgDataHome := os.Getenv("XDG_DATA_HOME")
|
||||
if xdgDataHome != "" {
|
||||
dataDir = filepath.Join(xdgDataHome, "lmcli")
|
||||
} else {
|
||||
userHomeDir, _ := os.UserHomeDir()
|
||||
dataDir = filepath.Join(userHomeDir, ".local/share/lmcli")
|
||||
}
|
||||
|
||||
os.MkdirAll(dataDir, 0755)
|
||||
return dataDir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user