From 02a39ee97ea932f9f849fe5c553a9cb018089fa9 Mon Sep 17 00:00:00 2001 From: Jacob Marble Date: Sun, 4 Oct 2015 18:06:39 -0700 Subject: [PATCH 1/2] Make GCP job id prefix to job title optional. And off by default. Fixes #103 --- cups/cups.go | 25 ++++++++++++++---------- gcp-cups-connector-util/init.go | 4 ++++ gcp-cups-connector/gcp-cups-connector.go | 5 +++-- gcp/gcp.go | 4 +--- lib/config.go | 4 ++++ manager/printermanager.go | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cups/cups.go b/cups/cups.go index 25e8616..ab378a9 100644 --- a/cups/cups.go +++ b/cups/cups.go @@ -129,15 +129,16 @@ var ( // Interface between Go and the CUPS API. type CUPS struct { - cc *cupsCore - pc *ppdCache - infoToDisplayName bool - displayNamePrefix string - printerAttributes []string - systemTags map[string]string + cc *cupsCore + pc *ppdCache + infoToDisplayName bool + prefixJobIDToJobTitle bool + displayNamePrefix string + printerAttributes []string + systemTags map[string]string } -func NewCUPS(infoToDisplayName bool, displayNamePrefix string, printerAttributes []string, maxConnections uint, connectTimeout time.Duration) (*CUPS, error) { +func NewCUPS(infoToDisplayName, prefixJobIDToJobTitle bool, displayNamePrefix string, printerAttributes []string, maxConnections uint, connectTimeout time.Duration) (*CUPS, error) { if err := checkPrinterAttributes(printerAttributes); err != nil { return nil, err } @@ -406,12 +407,16 @@ func convertJobState(cupsState int32) cdd.PrintJobStateDiff { // Print sends a new print job to the specified printer. The job ID // is returned. -func (c *CUPS) Print(printername, filename, title, user string, ticket *cdd.CloudJobTicket) (uint32, error) { +func (c *CUPS) Print(printername, filename, title, user, gcpJobID string, ticket *cdd.CloudJobTicket) (uint32, error) { pn := C.CString(printername) defer C.free(unsafe.Pointer(pn)) fn := C.CString(filename) defer C.free(unsafe.Pointer(fn)) var t *C.char + + if c.prefixJobIDToJobTitle { + title = fmt.Sprintf("gcp:%s %s", gcpJobID, title) + } if len(title) > 255 { t = C.CString(title[:255]) } else { @@ -433,12 +438,12 @@ func (c *CUPS) Print(printername, filename, title, user string, ticket *cdd.Clou u := C.CString(user) defer C.free(unsafe.Pointer(u)) - jobID, err := c.cc.printFile(u, pn, fn, t, numOptions, o) + cupsJobID, err := c.cc.printFile(u, pn, fn, t, numOptions, o) if err != nil { return 0, err } - return uint32(jobID), nil + return uint32(cupsJobID), nil } // convertIPPDateToTime converts an RFC 2579 date to a time.Time object. diff --git a/gcp-cups-connector-util/init.go b/gcp-cups-connector-util/init.go index 5230da2..a42dfea 100644 --- a/gcp-cups-connector-util/init.go +++ b/gcp-cups-connector-util/init.go @@ -63,6 +63,9 @@ var ( copyPrinterInfoToDisplayNameFlag = flag.String( "copy-printer-info-to-display-name", "", "Whether to copy the CUPS printer's printer-info attribute to the GCP printer's defaultDisplayName") + prefixJobIDToJobTitleFlag = flag.String( + "prefix-job-id-to-job-title", "", + "Whether to add the job ID to the beginning of the job title") displayNamePrefixFlag = flag.String( "display-name-prefix", "", "Prefix to add to GCP printer's defaultDisplayName") @@ -390,6 +393,7 @@ func createConfigFile(xmppJID, robotRefreshToken, userRefreshToken, shareScope, flagToBool(cupsJobFullUsernameFlag, lib.DefaultConfig.CUPSJobFullUsername), flagToBool(cupsIgnoreRawPrintersFlag, lib.DefaultConfig.CUPSIgnoreRawPrinters), flagToBool(copyPrinterInfoToDisplayNameFlag, lib.DefaultConfig.CopyPrinterInfoToDisplayName), + flagToBool(prefixJobIDToJobTitleFlag, lib.DefaultConfig.PrefixJobIDToJobTitle), flagToString(displayNamePrefixFlag, lib.DefaultConfig.DisplayNamePrefix), flagToString(monitorSocketFilenameFlag, lib.DefaultConfig.MonitorSocketFilename), flagToString(gcpBaseURLFlag, lib.DefaultConfig.GCPBaseURL), diff --git a/gcp-cups-connector/gcp-cups-connector.go b/gcp-cups-connector/gcp-cups-connector.go index bbb8e3b..2d0e926 100644 --- a/gcp-cups-connector/gcp-cups-connector.go +++ b/gcp-cups-connector/gcp-cups-connector.go @@ -91,8 +91,9 @@ func main() { defer x.Quit() } - c, err := cups.NewCUPS(config.CopyPrinterInfoToDisplayName, config.DisplayNamePrefix, - config.CUPSPrinterAttributes, config.CUPSMaxConnections, cupsConnectTimeout) + c, err := cups.NewCUPS(config.CopyPrinterInfoToDisplayName, config.PrefixJobIDToJobTitle, + config.DisplayNamePrefix, config.CUPSPrinterAttributes, config.CUPSMaxConnections, + cupsConnectTimeout) if err != nil { glog.Fatal(err) } diff --git a/gcp/gcp.go b/gcp/gcp.go index a4320d9..df401cf 100644 --- a/gcp/gcp.go +++ b/gcp/gcp.go @@ -630,12 +630,10 @@ func (gcp *GoogleCloudPrint) processJob(job *Job, printer *lib.Printer, reportJo return } - jobTitle := fmt.Sprintf("gcp:%s %s", job.GCPJobID, job.Title) - gcp.jobs <- &lib.Job{ CUPSPrinterName: printer.Name, Filename: filename, - Title: jobTitle, + Title: job.Title, User: job.OwnerID, JobID: job.GCPJobID, Ticket: ticket, diff --git a/lib/config.go b/lib/config.go index cc0d9c7..1d732cc 100644 --- a/lib/config.go +++ b/lib/config.go @@ -85,6 +85,9 @@ type Config struct { // Whether to copy the CUPS printer's printer-info attribute to the GCP printer's defaultDisplayName. CopyPrinterInfoToDisplayName bool `json:"copy_printer_info_to_display_name"` + // Whether to add the job ID to the beginning of the job title. Useful for debugging. + PrefixJobIDToJobTitle bool `json:"prefix_job_id_to_job_title"` + // Prefix for all GCP printers hosted by this connector. DisplayNamePrefix string `json:"display_name_prefix"` @@ -172,6 +175,7 @@ var DefaultConfig = Config{ CUPSJobFullUsername: false, CUPSIgnoreRawPrinters: true, CopyPrinterInfoToDisplayName: true, + PrefixJobIDToJobTitle: false, DisplayNamePrefix: "", MonitorSocketFilename: "/tmp/cups-connector-monitor.sock", GCPBaseURL: "https://www.google.com/cloudprint/", diff --git a/manager/printermanager.go b/manager/printermanager.go index d9a8e5c..afee722 100644 --- a/manager/printermanager.go +++ b/manager/printermanager.go @@ -395,7 +395,7 @@ func (pm *PrinterManager) printJob(cupsPrinterName, filename, title, user, jobID printer.CUPSJobSemaphore.Acquire() defer printer.CUPSJobSemaphore.Release() - cupsJobID, err := pm.cups.Print(printer.Name, filename, title, user, ticket) + cupsJobID, err := pm.cups.Print(printer.Name, filename, title, user, jobID, ticket) if err != nil { pm.incrementJobsProcessed(false) glog.Errorf("Failed to send job %s to CUPS: %s", jobID, err) From 627c5341494ca1632e38aec1b3d4f5667c07cd98 Mon Sep 17 00:00:00 2001 From: Jacob Marble Date: Sun, 4 Oct 2015 18:27:55 -0700 Subject: [PATCH 2/2] Don't panic when trying to update a non-existant config file. --- gcp-cups-connector-util/gcp-cups-connector-util.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gcp-cups-connector-util/gcp-cups-connector-util.go b/gcp-cups-connector-util/gcp-cups-connector-util.go index 26982ee..b4b73c5 100644 --- a/gcp-cups-connector-util/gcp-cups-connector-util.go +++ b/gcp-cups-connector-util/gcp-cups-connector-util.go @@ -121,6 +121,10 @@ func updateConfigFile() { if err != nil { panic(err) } + if configFilename == "" { + fmt.Println("Could not find a config file to update") + return + } // Same config in []byte format. configRaw, err := ioutil.ReadFile(configFilename)