Clean up shop a bit
This commit is contained in:
45
utils/db.go
Normal file
45
utils/db.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DB interface {
|
||||
DB() *gorm.DB
|
||||
Raw(sql string, args ...any) *gorm.DB
|
||||
}
|
||||
|
||||
type DBWrapper struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
var db *DBWrapper
|
||||
|
||||
func GetDB() (DB, error) {
|
||||
var err error
|
||||
|
||||
dbFile := filepath.Join("data.sqlite")
|
||||
db, err := gorm.Open(sqlite.Open(dbFile), &gorm.Config{
|
||||
// SkipDefaultTransaction: true,
|
||||
PrepareStmt: true,
|
||||
// Logger: gormlogger.Default.LogMode(gormlogger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DBWrapper{db: db}, nil
|
||||
}
|
||||
|
||||
|
||||
// Just a wrapper
|
||||
func (db *DBWrapper) Raw(sql string, args ...any) *gorm.DB {
|
||||
return db.db.Raw(sql, args...)
|
||||
}
|
||||
|
||||
func (db *DBWrapper) DB() *gorm.DB {
|
||||
return db.db
|
||||
}
|
@@ -1,24 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.site.quack-lab.dev/dave/cylogger"
|
||||
)
|
||||
|
||||
func CleanPath(path string) string {
|
||||
log := cylogger.Default.WithPrefix(fmt.Sprintf("CleanPath: %q", path))
|
||||
log.Trace("Start")
|
||||
path = filepath.Clean(path)
|
||||
path = strings.ReplaceAll(path, "\\", "/")
|
||||
log.Trace("Done: %q", path)
|
||||
return path
|
||||
}
|
||||
|
||||
func ToAbs(path string) string {
|
||||
log := cylogger.Default.WithPrefix(fmt.Sprintf("ToAbs: %q", path))
|
||||
log.Trace("Start")
|
||||
if filepath.IsAbs(path) {
|
||||
log.Trace("Path is already absolute: %q", path)
|
||||
return CleanPath(path)
|
||||
}
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Error("Error getting cwd: %v", err)
|
||||
return CleanPath(path)
|
||||
}
|
||||
log.Trace("Cwd: %q", cwd)
|
||||
return CleanPath(filepath.Join(cwd, path))
|
||||
}
|
||||
|
97
utils/git.go
97
utils/git.go
@@ -1,97 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
var (
|
||||
Repo *git.Repository
|
||||
Worktree *git.Worktree
|
||||
)
|
||||
|
||||
func SetupGit() error {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get current working directory: %w", err)
|
||||
}
|
||||
logger.Debug("Current working directory obtained: %s", cwd)
|
||||
|
||||
logger.Debug("Attempting to open git repository at %s", cwd)
|
||||
Repo, err = git.PlainOpen(cwd)
|
||||
if err != nil {
|
||||
logger.Debug("No existing git repository found at %s, attempting to initialize a new git repository.", cwd)
|
||||
Repo, err = git.PlainInit(cwd, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize a new git repository at %s: %w", cwd, err)
|
||||
}
|
||||
logger.Info("Successfully initialized a new git repository at %s", cwd)
|
||||
} else {
|
||||
logger.Info("Successfully opened existing git repository at %s", cwd)
|
||||
}
|
||||
|
||||
logger.Debug("Attempting to obtain worktree for repository at %s", cwd)
|
||||
Worktree, err = Repo.Worktree()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to obtain worktree for repository at %s: %w", cwd, err)
|
||||
}
|
||||
logger.Debug("Successfully obtained worktree for repository at %s", cwd)
|
||||
return nil
|
||||
}
|
||||
|
||||
func CleanupGitFiles(files []string) error {
|
||||
for _, file := range files {
|
||||
logger.Debug("Checking git status for file: %s", file)
|
||||
status, err := Worktree.Status()
|
||||
if err != nil {
|
||||
logger.Error("Error getting worktree status: %v", err)
|
||||
fmt.Fprintf(os.Stderr, "Error getting worktree status: %v\n", err)
|
||||
return fmt.Errorf("error getting worktree status: %w", err)
|
||||
}
|
||||
if status.IsUntracked(file) {
|
||||
logger.Info("Detected untracked file: %s. Adding to git index.", file)
|
||||
_, err = Worktree.Add(file)
|
||||
if err != nil {
|
||||
logger.Error("Error adding file to git: %v", err)
|
||||
fmt.Fprintf(os.Stderr, "Error adding file to git: %v\n", err)
|
||||
return fmt.Errorf("error adding file to git: %w", err)
|
||||
}
|
||||
|
||||
filename := filepath.Base(file)
|
||||
logger.Info("File %s added successfully. Committing with message: 'Track %s'", filename, filename)
|
||||
_, err = Worktree.Commit("Track "+filename, &git.CommitOptions{
|
||||
Author: &object.Signature{
|
||||
Name: "Big Chef",
|
||||
Email: "bigchef@bigchef.com",
|
||||
When: time.Now(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error committing file: %v", err)
|
||||
fmt.Fprintf(os.Stderr, "Error committing file: %v\n", err)
|
||||
return fmt.Errorf("error committing file: %w", err)
|
||||
}
|
||||
logger.Info("Successfully committed file: %s", filename)
|
||||
} else {
|
||||
logger.Info("File %s is already tracked. Restoring it to the working tree.", file)
|
||||
err := Worktree.Restore(&git.RestoreOptions{
|
||||
Files: []string{file},
|
||||
Staged: true,
|
||||
Worktree: true,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error restoring file: %v", err)
|
||||
fmt.Fprintf(os.Stderr, "Error restoring file: %v\n", err)
|
||||
return fmt.Errorf("error restoring file: %w", err)
|
||||
}
|
||||
logger.Info("File %s restored successfully", file)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user