Big refactor

This commit is contained in:
2024-09-23 14:51:58 +02:00
parent aef8f745eb
commit 16196ffe19
3 changed files with 40 additions and 53 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
main.log
main.exe
gitvc.exe

2
go.mod
View File

@@ -1,3 +1,3 @@
module main
module gitvc
go 1.22.2

88
main.go
View File

@@ -1,60 +1,37 @@
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
var timeUnits = map[string]int64{
"ms": 1,
"s": 1000,
"m": 1000 * 60,
"h": 1000 * 60 * 60,
"d": 1000 * 60 * 60 * 24,
"M": 1000 * 60 * 60 * 24 * 30,
"y": 1000 * 60 * 60 * 24 * 365,
}
var valueRegex, _ = regexp.Compile(`\d+`)
var unitRegex, _ = regexp.Compile(`[a-zA-Z]+`)
func parseDuration(date string) int64 {
var milliseconds int64 = 0
date = strings.TrimSpace(date)
var parts = strings.Split(date, "_")
for _, part := range parts {
part = strings.TrimSpace(part)
log.Printf("Parsing date part: %s\n", part)
var value = valueRegex.FindString(part)
var unit = unitRegex.FindString(part)
if value == "" || unit == "" {
log.Println("Invalid date part: " + part)
continue
}
if _, ok := timeUnits[unit]; !ok {
log.Println("Invalid date unit: " + unit)
continue
}
log.Printf("Part %s parsed as: Value: %s, Unit: %s\n", part, value, unit)
var valueMs, _ = strconv.ParseInt(value, 10, 16)
valueMs = valueMs * timeUnits[unit]
milliseconds += valueMs
log.Printf("Adding %dms to duration, now: %d\n", valueMs, milliseconds)
var Error *log.Logger
var Warning *log.Logger
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logFile, err := os.Create("main.log")
if err != nil {
log.Printf("Error creating log file: %v", err)
os.Exit(1)
}
logger := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logger)
return milliseconds
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
Warning = log.New(io.MultiWriter(logFile, os.Stdout),
fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
log.Lmicroseconds|log.Lshortfile)
}
func getEnv(key, def string) string {
var value, exists = os.LookupEnv(key)
if exists {
@@ -70,11 +47,15 @@ func main() {
ROOT = filepath.ToSlash(strings.TrimSpace(getEnv("ROOT", "/tmp")))
ROOT = path.Clean(ROOT)
var SCAN_INTERVAL = time.Duration(parseDuration(getEnv("SCAN_INTERVAL", "1m")) * 1e6)
scanInterval, err := time.ParseDuration(getEnv("SCAN_INTERVAL", "1m"))
if err != nil {
Error.Printf("error parsing SCAN_INTERVAL: %v", err)
return
}
log.Println("Input args parsed as:")
log.Printf("ROOT: %s\n", ROOT)
log.Printf("SCAN_INTERVAL: %.0fs\n", SCAN_INTERVAL.Seconds())
log.Printf("Input args parsed as:")
log.Printf("ROOT: %s", ROOT)
log.Printf("SCAN_INTERVAL: %.0fs", scanInterval.Seconds())
// This shit DOES NOT run correctly on windows
// It absolutely butchers CRLF -> LF
@@ -86,14 +67,19 @@ func main() {
for {
log.Printf("Running at %s", time.Now().Format(time.RFC3339))
doRun()
time.Sleep(SCAN_INTERVAL)
time.Sleep(scanInterval)
}
}
func doRun() {
err := os.Remove(filepath.Join(ROOT, ".git/index.lock"))
if err != nil {
Error.Printf("Error removing index.lock: %v", err)
}
status, err := doStatus()
if err != nil {
log.Printf("Error executing doStatus code %v with out: %v", err, status)
Error.Printf("Error executing doStatus code %v with out: %v", err, status)
return
}
if status == "" {
@@ -104,19 +90,19 @@ func doRun() {
res, err := doAddAll()
if err != nil {
log.Printf("Error executing doAddAll with code %v and out: %v", err, res)
Error.Printf("Error executing doAddAll with code %v and out: %v", err, res)
return
}
log.Printf("Changes added to index: %v", res)
res, err = setIdentity()
if err != nil {
log.Printf("Error setting identity with code %v and out: %v", err, res)
Error.Printf("Error setting identity with code %v and out: %v", err, res)
}
res, err = doCommit()
if err != nil {
log.Printf("Error executing doCommitAll with code %v and out: %v", err, res)
Error.Printf("Error executing doCommitAll with code %v and out: %v", err, res)
return
}
log.Printf("Changes committed: %v", res)