From f20e88405d2313ab8760a2fe9b38195ac0b9ddbb Mon Sep 17 00:00:00 2001 From: Etienne Bruines Date: Mon, 17 May 2021 09:05:31 +0200 Subject: [PATCH] Refactor gofaxsend into a package Using the idiomatic Go convention of writing code in a package, and then handle the CLI in a smaller main-package. This makes the code importable from other parts of the code and more maintainable. In the spirit of portability, also split SendQfile into a separate function that uses the Qfile reference without requiring the use of the filesystem. --- gofaxsend/{ => cmd}/main.go | 10 +++--- gofaxsend/error.go | 2 +- gofaxsend/faxfile.go | 2 +- gofaxsend/faxjob.go | 2 +- gofaxsend/qfile.go | 2 +- gofaxsend/qfile_test.go | 2 +- gofaxsend/sendqfile.go | 64 ++++++++++++++++++++----------------- gofaxsend/transmission.go | 2 +- 8 files changed, 46 insertions(+), 40 deletions(-) rename gofaxsend/{ => cmd}/main.go (91%) diff --git a/gofaxsend/main.go b/gofaxsend/cmd/main.go similarity index 91% rename from gofaxsend/main.go rename to gofaxsend/cmd/main.go index 0ccbac2..0f7a48c 100644 --- a/gofaxsend/main.go +++ b/gofaxsend/cmd/main.go @@ -26,6 +26,7 @@ import ( "github.com/gonicus/gofaxip/gofaxlib" "github.com/gonicus/gofaxip/gofaxlib/logger" + "github.com/gonicus/gofaxip/gofaxsend" ) const ( @@ -73,18 +74,17 @@ func main() { qfilename := flag.Arg(0) if qfilename == "" { - logger.Logger.Println("No qfile provided on command line") - os.Exit(sendFailed) + logger.Logger.Fatalln("No qfile provided on command line") } gofaxlib.LoadConfig(*configFile) devicefifo := filepath.Join(gofaxlib.Config.Hylafax.Spooldir, fifoPrefix+*deviceID) gofaxlib.SendFIFO(devicefifo, "SB") - returned, err := SendQfile(qfilename) + returned, err := gofaxsend.SendQfileFromDisk(qfilename, *deviceID) if err != nil { logger.Logger.Printf("Error processing qfile %v: %v", qfilename, err) - returned = sendFailed + returned = gofaxsend.SendFailed } gofaxlib.SendFIFO(devicefifo, "SR") @@ -94,5 +94,5 @@ func main() { } logger.Logger.Print("Exiting with status ", returned) - os.Exit(returned) + os.Exit(int(returned)) } diff --git a/gofaxsend/error.go b/gofaxsend/error.go index c49efb8..2484398 100644 --- a/gofaxsend/error.go +++ b/gofaxsend/error.go @@ -15,7 +15,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -package main +package gofaxsend // FaxError is a Error including information if a fax shoud be retried type FaxError interface { diff --git a/gofaxsend/faxfile.go b/gofaxsend/faxfile.go index f8065c0..b878083 100644 --- a/gofaxsend/faxfile.go +++ b/gofaxsend/faxfile.go @@ -15,7 +15,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -package main +package gofaxsend import ( "errors" diff --git a/gofaxsend/faxjob.go b/gofaxsend/faxjob.go index 6a7a701..5b7b5f4 100644 --- a/gofaxsend/faxjob.go +++ b/gofaxsend/faxjob.go @@ -15,7 +15,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -package main +package gofaxsend import ( "github.com/google/uuid" diff --git a/gofaxsend/qfile.go b/gofaxsend/qfile.go index 2a29a58..acf47e1 100644 --- a/gofaxsend/qfile.go +++ b/gofaxsend/qfile.go @@ -15,7 +15,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -package main +package gofaxsend import ( "bufio" diff --git a/gofaxsend/qfile_test.go b/gofaxsend/qfile_test.go index 0389c44..a667c2a 100644 --- a/gofaxsend/qfile_test.go +++ b/gofaxsend/qfile_test.go @@ -1,4 +1,4 @@ -package main +package gofaxsend import ( "testing" diff --git a/gofaxsend/sendqfile.go b/gofaxsend/sendqfile.go index 48aaa7e..7cbaee4 100644 --- a/gofaxsend/sendqfile.go +++ b/gofaxsend/sendqfile.go @@ -15,7 +15,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -package main +package gofaxsend import ( "fmt" @@ -30,28 +30,34 @@ import ( const ( // Return codes for Hylafax. - sendRetry = iota - sendFailed - sendDone - sendReformat - sendV34fail - sendV17fail - sendBatchfail - sendNobatch + SendRetry SendResult = iota + SendFailed + SendDone + SendReformat + SendV34fail + SendV17fail + SendBatchfail + SendNobatch ) -// SendQfile immediately tries to send the given qfile using FreeSWITCH -func SendQfile(qfilename string) (returned int, err error) { - returned = sendFailed +type SendResult int +// SendQfileFromDisk reads the qfile from disk and then immediately tries to send the given qfile using FreeSWITCH +func SendQfileFromDisk(filename, deviceID string) (SendResult, error) { // Open qfile - qf, err := OpenQfile(qfilename) + qf, err := OpenQfile(filename) if err != nil { - err = fmt.Errorf("Cannot open qfile %v: %v", qfilename, err) - return + return SendFailed, fmt.Errorf("cannot open qfile %v: %w", filename, err) } defer qf.Close() + return SendQfile(qf, deviceID) +} + +// SendQfile immediately tries to send the given qfile using FreeSWITCH +func SendQfile(qf *Qfile, deviceID string) (returned SendResult, err error) { + returned = SendFailed + var jobid uint if jobidstr := qf.GetString("jobid"); jobidstr != "" { if i, err := strconv.Atoi(jobidstr); err == nil { @@ -114,29 +120,29 @@ func SendQfile(qfilename string) (returned int, err error) { // Query DynamicConfig if dcCmd := gofaxlib.Config.Gofaxsend.DynamicConfig; dcCmd != "" { sessionlog.Log("Calling DynamicConfig script", dcCmd) - dc, err := gofaxlib.DynamicConfig(dcCmd, *deviceID, qf.GetString("owner"), qf.GetString("number"), fmt.Sprint(jobid)) + dc, err := gofaxlib.DynamicConfig(dcCmd, deviceID, qf.GetString("owner"), qf.GetString("number"), fmt.Sprint(jobid)) if err != nil { errmsg := fmt.Sprintln("Error calling DynamicConfig:", err) sessionlog.Log(errmsg) - qf.Set("returned", strconv.Itoa(sendRetry)) + qf.Set("returned", strconv.Itoa(int(SendRetry))) qf.Set("status", errmsg) if err = qf.Write(); err != nil { sessionlog.Logf("Error updating qfile:", err) } // Retry, as this is an internal error executing the DynamicConfig script which could recover later - return sendRetry, nil + return SendRetry, nil } // Check if call should be rejected if gofaxlib.DynamicConfigBool(dc.GetString("RejectCall")) { errmsg := "Transmission rejected by DynamicConfig" sessionlog.Log(errmsg) - qf.Set("returned", strconv.Itoa(sendFailed)) + qf.Set("returned", strconv.Itoa(int(SendFailed))) qf.Set("status", errmsg) if err = qf.Write(); err != nil { sessionlog.Logf("Error updating qfile:", err) } - return sendFailed, nil + return SendFailed, nil } // Check if a custom identifier should be set @@ -204,10 +210,10 @@ func SendQfile(qfilename string) (returned int, err error) { qf.Set("totdials", strconv.Itoa(totdials)) if err = qf.Write(); err != nil { sessionlog.Log("Error updating qfile:", err) - return sendFailed, nil + return SendFailed, nil } // Default: Retry when transmission fails - returned = sendRetry + returned = SendRetry // Start transmission goroutine transmitTs := time.Now() @@ -235,7 +241,7 @@ StatusLoop: // Fax Finished status = result.ResultText if result.Success { - returned = sendDone + returned = SendDone } break StatusLoop } @@ -260,23 +266,23 @@ StatusLoop: qf.Set("ndials", strconv.Itoa(ndials)) status = faxerr.Error() if faxerr.Retry() { - returned = sendRetry + returned = SendRetry } else { - returned = sendFailed + returned = SendFailed } break StatusLoop } } qf.Set("status", status) - qf.Set("returned", strconv.Itoa(returned)) + qf.Set("returned", strconv.Itoa(int(returned))) if err = qf.Write(); err != nil { sessionlog.Log("Error updating qfile:", err) } xfl := &gofaxlib.XFRecord{} xfl.Commid = sessionlog.CommID() - xfl.Modem = *deviceID + xfl.Modem = deviceID xfl.Jobid = uint(jobid) xfl.Jobtag = qf.GetString("jobtag") xfl.Sender = qf.GetString("mailaddr") @@ -287,11 +293,11 @@ StatusLoop: if result.Success { sessionlog.Logf("Fax sent successfully. Hangup Cause: %v. Result: %v", result.Hangupcause, status) } else { - sessionlog.Logf("Fax failed. Retry: %v. Hangup Cause: %v. Result: %v", returned == sendRetry, result.Hangupcause, status) + sessionlog.Logf("Fax failed. Retry: %v. Hangup Cause: %v. Result: %v", returned == SendRetry, result.Hangupcause, status) } xfl.SetResult(result) } else { - sessionlog.Logf("Call failed. Retry: %v. Result: %v", returned == sendRetry, status) + sessionlog.Logf("Call failed. Retry: %v. Result: %v", returned == SendRetry, status) xfl.Reason = status xfl.Ts = transmitTs xfl.Jobtime = time.Now().Sub(transmitTs) diff --git a/gofaxsend/transmission.go b/gofaxsend/transmission.go index 89111fa..9bd5e18 100644 --- a/gofaxsend/transmission.go +++ b/gofaxsend/transmission.go @@ -15,7 +15,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -package main +package gofaxsend import ( "bytes"