Clean up shop a bit
This commit is contained in:
6
go.mod
6
go.mod
@@ -21,7 +21,10 @@ require (
|
|||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||||
github.com/go-git/go-billy/v5 v5.6.2 // indirect
|
github.com/go-git/go-billy/v5 v5.6.2 // indirect
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||||
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
github.com/kevinburke/ssh_config v1.2.0 // indirect
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||||
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
github.com/pjbgf/sha1cd v0.3.2 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
|
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
|
||||||
@@ -29,11 +32,14 @@ require (
|
|||||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||||
golang.org/x/crypto v0.35.0 // indirect
|
golang.org/x/crypto v0.35.0 // indirect
|
||||||
golang.org/x/sys v0.30.0 // indirect
|
golang.org/x/sys v0.30.0 // indirect
|
||||||
|
golang.org/x/text v0.22.0 // indirect
|
||||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||||
|
gorm.io/gorm v1.30.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-git/go-git/v5 v5.14.0
|
github.com/go-git/go-git/v5 v5.14.0
|
||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
||||||
golang.org/x/net v0.35.0 // indirect
|
golang.org/x/net v0.35.0 // indirect
|
||||||
|
gorm.io/driver/sqlite v1.6.0
|
||||||
)
|
)
|
||||||
|
10
go.sum
10
go.sum
@@ -40,6 +40,10 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
|
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
|
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
|
||||||
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
|
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
@@ -49,6 +53,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
|
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
|
||||||
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
|
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
|
||||||
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
|
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
|
||||||
@@ -106,3 +112,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
|
||||||
|
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
|
||||||
|
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
|
||||||
|
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
|
||||||
|
3
main.go
3
main.go
@@ -11,7 +11,6 @@ import (
|
|||||||
"cook/processor"
|
"cook/processor"
|
||||||
"cook/utils"
|
"cook/utils"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||||
@@ -26,8 +25,6 @@ type GlobalStats struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
repo *git.Repository
|
|
||||||
worktree *git.Worktree
|
|
||||||
stats GlobalStats = GlobalStats{
|
stats GlobalStats = GlobalStats{
|
||||||
ModificationsPerCommand: sync.Map{},
|
ModificationsPerCommand: sync.Map{},
|
||||||
}
|
}
|
||||||
|
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
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.site.quack-lab.dev/dave/cylogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CleanPath(path string) string {
|
func CleanPath(path string) string {
|
||||||
|
log := cylogger.Default.WithPrefix(fmt.Sprintf("CleanPath: %q", path))
|
||||||
|
log.Trace("Start")
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
path = strings.ReplaceAll(path, "\\", "/")
|
path = strings.ReplaceAll(path, "\\", "/")
|
||||||
|
log.Trace("Done: %q", path)
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToAbs(path string) string {
|
func ToAbs(path string) string {
|
||||||
|
log := cylogger.Default.WithPrefix(fmt.Sprintf("ToAbs: %q", path))
|
||||||
|
log.Trace("Start")
|
||||||
if filepath.IsAbs(path) {
|
if filepath.IsAbs(path) {
|
||||||
|
log.Trace("Path is already absolute: %q", path)
|
||||||
return CleanPath(path)
|
return CleanPath(path)
|
||||||
}
|
}
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error("Error getting cwd: %v", err)
|
||||||
return CleanPath(path)
|
return CleanPath(path)
|
||||||
}
|
}
|
||||||
|
log.Trace("Cwd: %q", cwd)
|
||||||
return CleanPath(filepath.Join(cwd, path))
|
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