From 9d0225a63984b408c5246696fd06930386a17bca Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sat, 19 Dec 2015 23:44:34 +0100
Subject: progress for telnet interface works now


diff --git a/src/helsinki.at/rhimportd/ctrlTelnet.go b/src/helsinki.at/rhimportd/ctrlTelnet.go
index 65504f4..b2bcfa1 100644
--- a/src/helsinki.at/rhimportd/ctrlTelnet.go
+++ b/src/helsinki.at/rhimportd/ctrlTelnet.go
@@ -266,6 +266,37 @@ func (c *TelnetClient) handle_cmd_show(args []string) {
 	}
 }
 
+func telnet_progress_callback(step int, step_name string, progress float64, userdata interface{}) {
+	out := userdata.(chan<- string)
+	out <- fmt.Sprintf("%s: %3.2f%%\r", step_name, progress*100)
+}
+
+func telnet_cmd_run(ctx rhimport.ImportContext, out chan<- string) {
+	out <- fmt.Sprintf("fetching file from '%s'\n", ctx.SourceUri)
+	if res, err := rhimport.FetchFile(&ctx); err != nil {
+		out <- fmt.Sprintf("fetch file error: %s\n", err)
+		return
+	} else if res.ResponseCode != http.StatusOK {
+		out <- fmt.Sprintf("fetch file error: %s\n", res.ErrorString)
+		return
+	}
+
+	out <- fmt.Sprintf("\nimporting file '%s'\n", ctx.SourceFile)
+	if res, err := rhimport.ImportFile(&ctx); err != nil {
+		out <- fmt.Sprintf("import file error: %s\n", err)
+		return
+	} else {
+		if res.ResponseCode == http.StatusOK {
+			out <- fmt.Sprintf("\nFile got succesfully imported into Cart/Cut %d/%d\n", res.Cart, res.Cut)
+			rhl.Printf("File got succesfully imported into Cart/Cut %d/%d", res.Cart, res.Cut)
+		} else {
+			out <- fmt.Sprintf("Fileimport has failed (Cart/Cut %d/%d): %s\n", res.Cart, res.Cut, res.ErrorString)
+			rhl.Printf("Fileimport has failed (Cart/Cut %d/%d): %s", res.Cart, res.Cut, res.ErrorString)
+		}
+	}
+	close(out)
+}
+
 func (c *TelnetClient) handle_cmd_run(args []string) {
 	if c.ctx == nil {
 		c.say("context is empty please set at least one option")
@@ -278,27 +309,12 @@ func (c *TelnetClient) handle_cmd_run(args []string) {
 		return
 	}
 
-	c.say("fetching file from '%s'", ctx.SourceUri)
-	if fres, err := rhimport.FetchFile(&ctx); err != nil {
-		c.say("fetch file error: %s", err)
-		return
-	} else if fres.ResponseCode != http.StatusOK {
-		c.say("fetch file error: %s", fres.ErrorString)
-		return
-	}
-
-	c.say("importing file '%s'", ctx.SourceFile)
-	if ires, err := rhimport.ImportFile(&ctx); err != nil {
-		c.say("import file error: %s", err)
-		return
-	} else {
-		if ires.ResponseCode == http.StatusOK {
-			c.say("File got succesfully imported into Cart/Cut %d/%d", ires.Cart, ires.Cut)
-			rhl.Printf("File got succesfully imported into Cart/Cut %d/%d", ires.Cart, ires.Cut)
-		} else {
-			c.say("Fileimport has failed (Cart/Cut %d/%d): %s", ires.Cart, ires.Cut, ires.ErrorString)
-			rhl.Printf("Fileimport has failed (Cart/Cut %d/%d): %s", ires.Cart, ires.Cut, ires.ErrorString)
-		}
+	stdout := make(chan string)
+	ctx.ProgressCallBack = telnet_progress_callback
+	ctx.ProgressCallBackData = (chan<- string)(stdout)
+	go telnet_cmd_run(ctx, stdout)
+	for str := range stdout {
+		c.write_string(str)
 	}
 }
 
diff --git a/src/helsinki.at/rhimportd/ctrlWebSimple.go b/src/helsinki.at/rhimportd/ctrlWebSimple.go
index c5b81ce..da240a8 100644
--- a/src/helsinki.at/rhimportd/ctrlWebSimple.go
+++ b/src/helsinki.at/rhimportd/ctrlWebSimple.go
@@ -91,10 +91,6 @@ func webSimpleResponse(w http.ResponseWriter, result *rhimport.ImportResult) {
 	encoder.Encode(respdata)
 }
 
-func webSimpleProgressCallback(step int, step_name string, progress float64, userdata interface{}) {
-	fmt.Printf("Step %d / %s: %3.2f%%\r", step, step_name, progress*100)
-}
-
 func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted bool, r *http.Request) (ctx *rhimport.ImportContext, err error) {
 
 	decoder := json.NewDecoder(r.Body)
@@ -122,7 +118,6 @@ func webSimpleParseRequest(conf *rhimport.Config, rddb *rhimport.RdDb, trusted b
 	ctx.AutotrimLevel = reqdata.AutotrimLevel
 	ctx.UseMetaData = reqdata.UseMetaData
 	ctx.SourceUri = reqdata.SourceUri
-	ctx.ProgressCallBack = webSimpleProgressCallback
 	return
 }
 
-- 
cgit v0.10.2