From 9be91accb3b47a7501cfa871d5de95b6b07612d6 Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 26 Aug 2021 17:31:20 +0200 Subject: [PATCH] delete aux file if new package got installed better error message if no latex compiler available use latexmk as default compiler - if not available use pdflatex --- Main.go | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/Main.go b/Main.go index 9513b86..6b9110d 100644 --- a/Main.go +++ b/Main.go @@ -3,36 +3,46 @@ package main import ( "bufio" "fmt" - "io" "log" + "os" "os/exec" "regexp" "runtime" "strconv" ) +const ( + ErrNoCompiler = "none of the following latex compilers available: [latexmk, pdflatex]" +) + func main() { fmt.Printf("Pdflatex command exists: %t\n", commandExists("pdflatex")) - fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH) + fmt.Printf("LatexMk command exists: %t\n", commandExists("latexmk")) + fmt.Printf("Operation System: %s/%s\n", runtime.GOOS, runtime.GOARCH) compileAndInstall() } func compileAndInstall() { out, err := compileLatex("main.tex") - //fmt.Println(*out) + if err != nil { fmt.Println("An error occured while compiling the document!") - filename := parseMissingFile(out) - if filename != "" { + if err.Error() == ErrNoCompiler { + log.Fatal(err.Error()) + } + + if filename := parseMissingFile(out); filename != "" { fmt.Printf("We need to download: %s\n", filename) - // now we neet to perform a root check + // now we need to perform a root check if rootCheck() { log.Println("Awesome! You are now running this program with root permissions!") if installFile(filename) { + // we remove the main aux file to really trigger a rebuild! + os.Remove("main.aux") // if successfully installed we try to compile again compileAndInstall() } @@ -91,7 +101,14 @@ func commandExists(cmd string) bool { // parse the thumbail picture from video file func compileLatex(filename string) (*string, error) { - app := "pdflatex" + app := "" + if commandExists("latexmk") { + app = "latexmk" + } else if commandExists("pdflatex") { + app = "pdflatex" + } else { + return nil, fmt.Errorf(ErrNoCompiler) + } cmd := exec.Command(app, "-file-line-error", @@ -124,14 +141,11 @@ func rootCheck() bool { i, err := strconv.Atoi(string(output[:len(output)-1])) if err != nil { + // maybe no unix system? log.Fatal(err) } - if i == 0 { - return true - } else { - return false - } + return i == 0 } func installFile(filename string) bool { @@ -147,17 +161,25 @@ func installFile(filename string) bool { stderr, _ := cmd.StderrPipe() fmt.Println("running dnf install now!") + cmd.Start() go func() { - merged := io.MultiReader(stderr, stdout) - scanner := bufio.NewScanner(merged) + scanner := bufio.NewScanner(stdout) for scanner.Scan() { m := scanner.Text() fmt.Println(m) } }() - err := cmd.Run() + go func() { + scanner := bufio.NewScanner(stderr) + for scanner.Scan() { + m := scanner.Text() + fmt.Println(m) + } + }() + + err := cmd.Wait() if err != nil { fmt.Println(err.Error()) return false