From 3ffaad46068d3cf37d9309d4439165a080ffab02 Mon Sep 17 00:00:00 2001 From: Fabian Weber <> Date: Sun, 17 Mar 2019 13:40:08 +0100 Subject: [PATCH] fixed add printer to class and added function for error handling --- constants.go | 9 +++++++++ cups-client.go | 30 ++++++++++++++++-------------- error.go | 9 +++++++++ ipp-client.go | 2 +- 4 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 error.go diff --git a/constants.go b/constants.go index 20c4f0b..47044f3 100644 --- a/constants.go +++ b/constants.go @@ -275,6 +275,15 @@ const ( JobStateFilterCompleted = "completed" ) +type ErrorPolicy string + +const ( + ErrorPolicyRetryJob ErrorPolicy = "retry-job" + ErrorPolicyAbortJob = "abort-job" + ErrorPolicyRetryCurrentJob = "retry-current-job" + ErrorPolicyStopPrinter = "stop-printer" +) + const ( CharsetLanguage = "en-US" Charset = "utf-8" diff --git a/cups-client.go b/cups-client.go index 81f57c9..9f00946 100644 --- a/cups-client.go +++ b/cups-client.go @@ -63,7 +63,7 @@ func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) { return ppdNameMap, nil } -func (c *CUPSClient) AcceptJobs(printer, destPrinter string) error { +func (c *CUPSClient) AcceptJobs(printer string) error { req := NewRequest(OperationCupsAcceptJobs, 1) req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) @@ -71,7 +71,7 @@ func (c *CUPSClient) AcceptJobs(printer, destPrinter string) error { return err } -func (c *CUPSClient) RejectJobs(printer, destPrinter string) error { +func (c *CUPSClient) RejectJobs(printer string) error { req := NewRequest(OperationCupsRejectJobs, 1) req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) @@ -81,21 +81,23 @@ func (c *CUPSClient) RejectJobs(printer, destPrinter string) error { func (c *CUPSClient) AddPrinterToClass(class, printer string) error { attributes, err := c.GetPrinterAttributes(class, []string{"member-uris"}) - if err != nil { + if err != nil && !IsNotExistsError(err) { return err } memberURIList := make([]string, 0) - for _, member := range attributes["member-uris"] { - memberString := strings.Split(member.Value.(string), "/") - printerName := memberString[len(memberString)-1] + if !IsNotExistsError(err) { + for _, member := range attributes["member-uris"] { + memberString := strings.Split(member.Value.(string), "/") + printerName := memberString[len(memberString)-1] - if printerName == printer { - return nil + if printerName == printer { + return nil + } + + memberURIList = append(memberURIList, member.Value.(string)) } - - memberURIList = append(memberURIList, member.Value.(string)) } memberURIList = append(memberURIList, c.getPrinterUri(printer)) @@ -145,7 +147,7 @@ func (c *CUPSClient) DeleteClass(class string) error { return err } -func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, errorPolicy string, information, location string) error { +func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, errorPolicy ErrorPolicy, information, location string) error { req := NewRequest(OperationCupsAddModifyPrinter, 1) req.OperationAttributes["printer-uri"] = c.getPrinterUri(name) req.OperationAttributes["ppd-name"] = ppd @@ -154,7 +156,7 @@ func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, err req.PrinterAttributes["device-uri"] = deviceURI req.PrinterAttributes["printer-info"] = information req.PrinterAttributes["printer-location"] = location - req.PrinterAttributes["printer-error-policy"] = errorPolicy + req.PrinterAttributes["printer-error-policy"] = string(errorPolicy) _, err := c.SendRequest(c.getHttpUri("admin", ""), req) return err @@ -187,10 +189,10 @@ func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error { return err } -func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy string) error { +func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy ErrorPolicy) error { req := NewRequest(OperationCupsAddModifyPrinter, 1) req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) - req.PrinterAttributes["printer-error-policy"] = errorPolicy + req.PrinterAttributes["printer-error-policy"] = string(errorPolicy) _, err := c.SendRequest(c.getHttpUri("admin", ""), req) return err diff --git a/error.go b/error.go new file mode 100644 index 0000000..3ffd833 --- /dev/null +++ b/error.go @@ -0,0 +1,9 @@ +package ipp + +func IsNotExistsError(err error) bool { + if err == nil { + return false + } + + return err.Error() == "The printer or class does not exist." +} diff --git a/ipp-client.go b/ipp-client.go index d50624b..70e731c 100644 --- a/ipp-client.go +++ b/ipp-client.go @@ -289,7 +289,7 @@ func (c *IPPClient) CancelJob(jobID int, purge bool) error { } func (c *IPPClient) CancelAllJob(printer string, purge bool) error { - req := NewRequest(OperationCancelJob, 1) + req := NewRequest(OperationCancelJobs, 1) req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes["purge-jobs"] = purge