Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit 29cea39

Browse files
committed
Merge pull request #104 from jacobmarble/job-title
Make GCP job id prefix to job title optional.
2 parents 149c605 + 627c534 commit 29cea39

File tree

7 files changed

+32
-16
lines changed

7 files changed

+32
-16
lines changed

cups/cups.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,16 @@ var (
129129

130130
// Interface between Go and the CUPS API.
131131
type CUPS struct {
132-
cc *cupsCore
133-
pc *ppdCache
134-
infoToDisplayName bool
135-
displayNamePrefix string
136-
printerAttributes []string
137-
systemTags map[string]string
132+
cc *cupsCore
133+
pc *ppdCache
134+
infoToDisplayName bool
135+
prefixJobIDToJobTitle bool
136+
displayNamePrefix string
137+
printerAttributes []string
138+
systemTags map[string]string
138139
}
139140

140-
func NewCUPS(infoToDisplayName bool, displayNamePrefix string, printerAttributes []string, maxConnections uint, connectTimeout time.Duration) (*CUPS, error) {
141+
func NewCUPS(infoToDisplayName, prefixJobIDToJobTitle bool, displayNamePrefix string, printerAttributes []string, maxConnections uint, connectTimeout time.Duration) (*CUPS, error) {
141142
if err := checkPrinterAttributes(printerAttributes); err != nil {
142143
return nil, err
143144
}
@@ -406,12 +407,16 @@ func convertJobState(cupsState int32) cdd.PrintJobStateDiff {
406407

407408
// Print sends a new print job to the specified printer. The job ID
408409
// is returned.
409-
func (c *CUPS) Print(printername, filename, title, user string, ticket *cdd.CloudJobTicket) (uint32, error) {
410+
func (c *CUPS) Print(printername, filename, title, user, gcpJobID string, ticket *cdd.CloudJobTicket) (uint32, error) {
410411
pn := C.CString(printername)
411412
defer C.free(unsafe.Pointer(pn))
412413
fn := C.CString(filename)
413414
defer C.free(unsafe.Pointer(fn))
414415
var t *C.char
416+
417+
if c.prefixJobIDToJobTitle {
418+
title = fmt.Sprintf("gcp:%s %s", gcpJobID, title)
419+
}
415420
if len(title) > 255 {
416421
t = C.CString(title[:255])
417422
} else {
@@ -433,12 +438,12 @@ func (c *CUPS) Print(printername, filename, title, user string, ticket *cdd.Clou
433438
u := C.CString(user)
434439
defer C.free(unsafe.Pointer(u))
435440

436-
jobID, err := c.cc.printFile(u, pn, fn, t, numOptions, o)
441+
cupsJobID, err := c.cc.printFile(u, pn, fn, t, numOptions, o)
437442
if err != nil {
438443
return 0, err
439444
}
440445

441-
return uint32(jobID), nil
446+
return uint32(cupsJobID), nil
442447
}
443448

444449
// convertIPPDateToTime converts an RFC 2579 date to a time.Time object.

gcp-cups-connector-util/gcp-cups-connector-util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func updateConfigFile() {
121121
if err != nil {
122122
panic(err)
123123
}
124+
if configFilename == "" {
125+
fmt.Println("Could not find a config file to update")
126+
return
127+
}
124128

125129
// Same config in []byte format.
126130
configRaw, err := ioutil.ReadFile(configFilename)

gcp-cups-connector-util/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ var (
6363
copyPrinterInfoToDisplayNameFlag = flag.String(
6464
"copy-printer-info-to-display-name", "",
6565
"Whether to copy the CUPS printer's printer-info attribute to the GCP printer's defaultDisplayName")
66+
prefixJobIDToJobTitleFlag = flag.String(
67+
"prefix-job-id-to-job-title", "",
68+
"Whether to add the job ID to the beginning of the job title")
6669
displayNamePrefixFlag = flag.String(
6770
"display-name-prefix", "",
6871
"Prefix to add to GCP printer's defaultDisplayName")
@@ -390,6 +393,7 @@ func createConfigFile(xmppJID, robotRefreshToken, userRefreshToken, shareScope,
390393
flagToBool(cupsJobFullUsernameFlag, lib.DefaultConfig.CUPSJobFullUsername),
391394
flagToBool(cupsIgnoreRawPrintersFlag, lib.DefaultConfig.CUPSIgnoreRawPrinters),
392395
flagToBool(copyPrinterInfoToDisplayNameFlag, lib.DefaultConfig.CopyPrinterInfoToDisplayName),
396+
flagToBool(prefixJobIDToJobTitleFlag, lib.DefaultConfig.PrefixJobIDToJobTitle),
393397
flagToString(displayNamePrefixFlag, lib.DefaultConfig.DisplayNamePrefix),
394398
flagToString(monitorSocketFilenameFlag, lib.DefaultConfig.MonitorSocketFilename),
395399
flagToString(gcpBaseURLFlag, lib.DefaultConfig.GCPBaseURL),

gcp-cups-connector/gcp-cups-connector.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ func main() {
9191
defer x.Quit()
9292
}
9393

94-
c, err := cups.NewCUPS(config.CopyPrinterInfoToDisplayName, config.DisplayNamePrefix,
95-
config.CUPSPrinterAttributes, config.CUPSMaxConnections, cupsConnectTimeout)
94+
c, err := cups.NewCUPS(config.CopyPrinterInfoToDisplayName, config.PrefixJobIDToJobTitle,
95+
config.DisplayNamePrefix, config.CUPSPrinterAttributes, config.CUPSMaxConnections,
96+
cupsConnectTimeout)
9697
if err != nil {
9798
glog.Fatal(err)
9899
}

gcp/gcp.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,10 @@ func (gcp *GoogleCloudPrint) processJob(job *Job, printer *lib.Printer, reportJo
630630
return
631631
}
632632

633-
jobTitle := fmt.Sprintf("gcp:%s %s", job.GCPJobID, job.Title)
634-
635633
gcp.jobs <- &lib.Job{
636634
CUPSPrinterName: printer.Name,
637635
Filename: filename,
638-
Title: jobTitle,
636+
Title: job.Title,
639637
User: job.OwnerID,
640638
JobID: job.GCPJobID,
641639
Ticket: ticket,

lib/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ type Config struct {
8585
// Whether to copy the CUPS printer's printer-info attribute to the GCP printer's defaultDisplayName.
8686
CopyPrinterInfoToDisplayName bool `json:"copy_printer_info_to_display_name"`
8787

88+
// Whether to add the job ID to the beginning of the job title. Useful for debugging.
89+
PrefixJobIDToJobTitle bool `json:"prefix_job_id_to_job_title"`
90+
8891
// Prefix for all GCP printers hosted by this connector.
8992
DisplayNamePrefix string `json:"display_name_prefix"`
9093

@@ -172,6 +175,7 @@ var DefaultConfig = Config{
172175
CUPSJobFullUsername: false,
173176
CUPSIgnoreRawPrinters: true,
174177
CopyPrinterInfoToDisplayName: true,
178+
PrefixJobIDToJobTitle: false,
175179
DisplayNamePrefix: "",
176180
MonitorSocketFilename: "/tmp/cups-connector-monitor.sock",
177181
GCPBaseURL: "https://www.google.com/cloudprint/",

manager/printermanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func (pm *PrinterManager) printJob(cupsPrinterName, filename, title, user, jobID
395395
printer.CUPSJobSemaphore.Acquire()
396396
defer printer.CUPSJobSemaphore.Release()
397397

398-
cupsJobID, err := pm.cups.Print(printer.Name, filename, title, user, ticket)
398+
cupsJobID, err := pm.cups.Print(printer.Name, filename, title, user, jobID, ticket)
399399
if err != nil {
400400
pm.incrementJobsProcessed(false)
401401
glog.Errorf("Failed to send job %s to CUPS: %s", jobID, err)

0 commit comments

Comments
 (0)