2022-06-06 10:55:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-06-07 14:06:36 +00:00
|
|
|
"path/filepath"
|
2022-06-06 10:55:39 +00:00
|
|
|
"strconv"
|
2022-06-07 14:06:36 +00:00
|
|
|
"time"
|
2022-06-06 10:55:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
TeamsDir string
|
|
|
|
skipInitialSync bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
2022-06-07 14:06:36 +00:00
|
|
|
//var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
|
2022-06-06 10:55:39 +00:00
|
|
|
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
|
2022-06-07 14:06:36 +00:00
|
|
|
//watcher := flag.Bool("watch", false, "Enable daemon mode by watching the directory")
|
|
|
|
tspath := flag.String("timestamp-file", "./REMOTE/timestamp", "Path to the file storing the last timestamp")
|
|
|
|
exercicespath := flag.String("exercices-file", "./REMOTE/exercices-bindings.json", "Path to the file containing the ID bindings")
|
|
|
|
coeff := flag.Float64("global-coeff", 10.0, "Coefficient to use to multiply all scores before passing them to the other platform")
|
2022-06-06 10:55:39 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
api := AirbusAPI{
|
|
|
|
BaseURL: "https://portal.european-cybercup.lan/api/v1",
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
|
|
|
|
api.BaseURL = v
|
|
|
|
}
|
|
|
|
if v, exists := os.LookupEnv("AIRBUS_TOKEN"); exists {
|
|
|
|
api.Token = v
|
|
|
|
}
|
|
|
|
if v, exists := os.LookupEnv("AIRBUS_SESSIONID"); exists {
|
|
|
|
var err error
|
2022-06-07 14:06:36 +00:00
|
|
|
api.SessionID, err = strconv.ParseInt(v, 10, 64)
|
2022-06-06 10:55:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetPrefix("[challenge-sync-airbus] ")
|
|
|
|
|
|
|
|
TeamsDir = path.Clean(TeamsDir)
|
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
// Load the timestamp
|
|
|
|
ts, err := loadTS(*tspath)
|
2022-06-06 10:55:39 +00:00
|
|
|
if err != nil {
|
2022-06-07 14:06:36 +00:00
|
|
|
log.Fatal("Unable to open timestamp file: ", err.Error())
|
2022-06-06 10:55:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
// Load exercices bindings
|
|
|
|
exbindings, err := ReadExercicesBindings(*exercicespath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to open exercices bindings file: ", err.Error())
|
2022-06-06 10:55:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
// Load teams.json
|
|
|
|
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to open teams bindings file: ", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
w := Walker{
|
|
|
|
LastSync: *ts,
|
|
|
|
Exercices: exbindings,
|
|
|
|
Teams: teamsbindings,
|
|
|
|
API: api,
|
|
|
|
Coeff: *coeff,
|
2022-06-06 10:55:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
// Iterate over teams scores
|
|
|
|
err = filepath.WalkDir(TeamsDir, w.WalkScore)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Something goes wrong during walking")
|
|
|
|
}
|
2022-06-06 10:55:39 +00:00
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
// Update timestamp for the next time
|
|
|
|
w.LastSync = time.Now()
|
2022-06-06 10:55:39 +00:00
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
err = saveTS(*tspath, &w.LastSync)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to save timestamp file: ", err.Error())
|
2022-06-06 10:55:39 +00:00
|
|
|
}
|
|
|
|
}
|