-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
85 lines (69 loc) · 2.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"math"
"os"
"time"
"github.com/stuartleeks/wsl-clock/internal/pkg/logging"
"github.com/stuartleeks/wsl-clock/internal/pkg/wsl"
)
// TODO
// - allow clock tolerance to be specified as an arg
// - allow max log size to be specified as an arg
// Overridden via ldflags
var (
version = "99.0.1-devbuild"
commit = "unknown"
date = "unknown"
goversion = "unknown"
)
const toleratedTimeDiffInSeconds float64 = 30 // allow for time executing the commands between comparisons
func main() {
err := execute()
if err != nil {
logging.AddMessage(err.Error())
}
logging.WriteLog()
if err != nil {
os.Exit(1)
}
}
func execute() error {
logging.AddMessage("********************************")
logging.AddMessage("*** wsl-clock starting...")
logging.AddMessage("*** Version : %s", version)
logging.AddMessage("*** Commit : %s", commit)
logging.AddMessage("*** Date : %s", date)
logging.AddMessage("*** Go version: %s", goversion)
runningDistros, err := wsl.GetRunningV2Distros()
if err != nil {
return fmt.Errorf("Failed to get running distros: %s", err)
}
if len(runningDistros) == 0 {
logging.AddMessage("No running distros - quitting")
return nil
}
distroName := runningDistros[0]
logging.AddMessage("Running commands in distro %q", distroName)
originalTime, err := wsl.GetWslTime(distroName)
if err != nil {
return fmt.Errorf("Failed to get original time: %s", err)
}
currentTime := time.Now()
diff := currentTime.Sub(originalTime)
absDiffSeconds := math.Abs(diff.Seconds())
if absDiffSeconds < toleratedTimeDiffInSeconds {
logging.AddMessage("Time diff (%0.fs) within tolerance (%0.fs) - quitting", absDiffSeconds, toleratedTimeDiffInSeconds)
return nil
}
err = wsl.ResetWslClock(distroName)
if err != nil {
return err
}
newTime, err := wsl.GetWslTime(distroName)
if err != nil {
return fmt.Errorf("Failed to get new time: %s", err)
}
logging.AddMessage("Time correction (seconds): %.0f", newTime.Sub(originalTime).Seconds())
return nil
}