Implement proper "reset" that reads snapshots from database
This commit is contained in:
18
main.go
18
main.go
@@ -56,6 +56,12 @@ func main() {
|
|||||||
logger.InitFlag()
|
logger.InitFlag()
|
||||||
logger.Info("Initializing with log level: %s", logger.GetLevel().String())
|
logger.Info("Initializing with log level: %s", logger.GetLevel().String())
|
||||||
|
|
||||||
|
db, err := utils.GetDB()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to get database: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// The plan is:
|
// The plan is:
|
||||||
// Load all commands
|
// Load all commands
|
||||||
commands, err := utils.LoadCommands(args)
|
commands, err := utils.LoadCommands(args)
|
||||||
@@ -105,6 +111,12 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = utils.ResetWhereNecessary(associations, db)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to reset files where necessary: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Then for each file run all commands associated with the file
|
// Then for each file run all commands associated with the file
|
||||||
workers := make(chan struct{}, *utils.ParallelFiles)
|
workers := make(chan struct{}, *utils.ParallelFiles)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
@@ -137,12 +149,6 @@ func main() {
|
|||||||
logger.Debug("Created logger for command %q with log level %s", cmdName, cmdLogLevel.String())
|
logger.Debug("Created logger for command %q with log level %s", cmdName, cmdLogLevel.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := utils.GetDB()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Failed to get database: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for file, association := range associations {
|
for file, association := range associations {
|
||||||
workers <- struct{}{}
|
workers <- struct{}{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
13
utils/db.go
13
utils/db.go
@@ -14,6 +14,7 @@ type DB interface {
|
|||||||
DB() *gorm.DB
|
DB() *gorm.DB
|
||||||
Raw(sql string, args ...any) *gorm.DB
|
Raw(sql string, args ...any) *gorm.DB
|
||||||
SaveFile(filePath string, fileData []byte) error
|
SaveFile(filePath string, fileData []byte) error
|
||||||
|
GetFile(filePath string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileSnapshot struct {
|
type FileSnapshot struct {
|
||||||
@@ -80,3 +81,15 @@ func (db *DBWrapper) SaveFile(filePath string, fileData []byte) error {
|
|||||||
FileData: fileData,
|
FileData: fileData,
|
||||||
}).Error
|
}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DBWrapper) GetFile(filePath string) ([]byte, error) {
|
||||||
|
log := cylogger.Default.WithPrefix(fmt.Sprintf("GetFile: %q", filePath))
|
||||||
|
log.Debug("Getting file from database")
|
||||||
|
var fileSnapshot FileSnapshot
|
||||||
|
err := db.db.Model(&FileSnapshot{}).Where("file_path = ?", filePath).First(&fileSnapshot).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
log.Debug("File found in database")
|
||||||
|
return fileSnapshot.FileData, nil
|
||||||
|
}
|
||||||
|
@@ -33,3 +33,41 @@ func ToAbs(path string) string {
|
|||||||
log.Trace("Cwd: %q", cwd)
|
log.Trace("Cwd: %q", cwd)
|
||||||
return CleanPath(filepath.Join(cwd, path))
|
return CleanPath(filepath.Join(cwd, path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ResetWhereNecessary(associations map[string]FileCommandAssociation, db DB) error {
|
||||||
|
log := cylogger.Default.WithPrefix("ResetWhereNecessary")
|
||||||
|
log.Debug("Start")
|
||||||
|
dirtyFiles := make(map[string]struct{})
|
||||||
|
for _, association := range associations {
|
||||||
|
for _, command := range association.Commands {
|
||||||
|
log.Debug("Checking command %q for file %q", command.Name, association.File)
|
||||||
|
if command.Reset {
|
||||||
|
log.Debug("Command %q requires reset for file %q", command.Name, association.File)
|
||||||
|
dirtyFiles[association.File] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, command := range association.IsolateCommands {
|
||||||
|
log.Debug("Checking isolate command %q for file %q", command.Name, association.File)
|
||||||
|
if command.Reset {
|
||||||
|
log.Debug("Isolate command %q requires reset for file %q", command.Name, association.File)
|
||||||
|
dirtyFiles[association.File] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Debug("Dirty files: %v", dirtyFiles)
|
||||||
|
for file := range dirtyFiles {
|
||||||
|
log.Debug("Resetting file %q", file)
|
||||||
|
fileData, err := db.GetFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Debug("Writing file %q to disk", file)
|
||||||
|
err = os.WriteFile(file, fileData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Debug("File %q written to disk", file)
|
||||||
|
}
|
||||||
|
log.Debug("Done")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user