Add update flag for init cmd
Some checks failed
Build and push Probo image on the registry / login (push) Failing after 2m41s

This commit is contained in:
Andrea Fazzi 2025-05-23 12:09:04 +02:00
commit bf41b6dece
6 changed files with 55 additions and 36 deletions

View file

@ -20,7 +20,7 @@ func init() {
rootCmd.AddCommand(importCmd)
importCmd.Flags().StringP("type", "t", "quizzes", "Import a file containing entities (participants or quizzes)")
importCmd.Flags().StringP("delim", "d", "----", "Set the quizzes separator.")
importCmd.Flags().StringP("delim", "d", "<hr/>", "Set the quizzes separator.")
}
func importEntity(cmd *cobra.Command, args []string) {

View file

@ -22,12 +22,19 @@ var initCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().BoolP("update", "u", false, "Updates files that are older than the ones in the current embedded fs.")
}
func runInit(cmd *cobra.Command, args []string) {
info, err := embed.CopyToWorkingDirectory(embed.Init, "init")
update, err := cmd.Flags().GetBool("update")
if err != nil {
panic(err)
log.Fatal(err)
}
info, err := embed.CopyToWorkingDirectory(embed.Init, "init", update)
if err != nil {
log.Fatal(err)
}
log.Info("Filesystem init", "total", info.Total, "copied", info.Copied)

View file

@ -120,7 +120,7 @@ func createSession(cmd *cobra.Command, args []string) {
vm.Set("session", session)
vm.Set("quizzes", quizzes.ReadAll())
vm.Set("participants", participants.ReadAll())
vm.Set("log", func(text string) { log.Info(text) })
vm.Set("log", func(msg any, keyvals ...any) { log.Info(msg, keyvals...) })
_, err = vm.RunScript(filepath.Base(programPath), string(program))
if err != nil {

View file

@ -5,6 +5,8 @@ import (
"io/fs"
"os"
"path/filepath"
"github.com/charmbracelet/log"
)
var (
@ -21,7 +23,7 @@ type CopyInfo struct {
Updated int
}
func CopyToWorkingDirectory(src embed.FS, root string) (*CopyInfo, error) {
func CopyToWorkingDirectory(src embed.FS, root string, update bool) (*CopyInfo, error) {
currentDir, err := os.Getwd()
if err != nil {
return nil, err
@ -42,14 +44,15 @@ func CopyToWorkingDirectory(src embed.FS, root string) (*CopyInfo, error) {
destPath := filepath.Join(currentDir, relPath)
if d.IsDir() {
return os.MkdirAll(destPath, 0o700)
return os.MkdirAll(destPath, 0700)
}
info.Total++
exists := false
destFileInfo, err := os.Stat(destPath)
if _, err := os.Stat(destPath); !os.IsNotExist(err) {
if !os.IsNotExist(err) {
exists = true
}
@ -60,7 +63,16 @@ func CopyToWorkingDirectory(src embed.FS, root string) (*CopyInfo, error) {
if !exists {
info.Copied++
err = os.WriteFile(destPath, data, 0o644)
err = os.WriteFile(destPath, data, 0644)
}
if exists && update {
info.Updated++
err = os.WriteFile(destPath, data, 0644)
if err != nil {
return err
}
}
}
return err

View file

@ -1,28 +1,3 @@
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {String} The first item in the shuffled array
*/
var shuffle = function (array) {
var currentIndex = array.length;
var temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
let tags = vars.quiz.tags;
let participantClass = vars.participant.attributes.class;
let num = 12;
@ -51,3 +26,28 @@ for (let i = 0; i < filteredParticipants.length; i++) {
Quizzes: shuffle(shuffledAnswers.slice()).slice(0, num),
};
}
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {String} The first item in the shuffled array
*/
var shuffle = function (array) {
var currentIndex = array.length;
var temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};

View file

@ -4,7 +4,7 @@ import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"io"
"os"
)
@ -38,7 +38,7 @@ func main() {
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
byteValue, _ := io.ReadAll(xmlFile)
var questionnaire Questionnaire
xml.Unmarshal(byteValue, &questionnaire)
@ -51,7 +51,7 @@ func main() {
}
mdFileName := fmt.Sprintf("question_%d_%d.md", i+1, j+1)
ioutil.WriteFile(mdFileName, []byte(mdContent), 0644)
os.WriteFile(mdFileName, []byte(mdContent), 0644)
}
}
}