-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_linux.go
More file actions
70 lines (63 loc) · 1.51 KB
/
print_linux.go
File metadata and controls
70 lines (63 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os/exec"
"github.com/FurqanSoftware/pog"
)
func printPDF(cfg Config, name string) error {
args := []string{}
if cfg.Printer.Name != "" {
args = append(args, "-P", cfg.Printer.Name)
}
args = append(args, name)
cmd := exec.Command("lpr", args...)
_, err := cmd.Output()
if err != nil {
var exiterr *exec.ExitError
if errors.As(err, &exiterr) {
if exiterr.ExitCode() == 1 && bytes.HasPrefix(exiterr.Stderr, []byte("lpr: Error - No default destination.")) {
return printDispatchError{error: errNoDefaultPrinter}
}
return printDispatchError{fmt.Errorf("%w: %s", err, ellipsize(string(exiterr.Stderr), 50, "..."))}
}
return printDispatchError{err}
}
return nil
}
func checkDependencies(cfg Config) {
_, err := exec.LookPath("lpr")
if err != nil {
pog.Fatal("Missing dependency: could not find lpr")
}
}
func resolvePrintHelper(cfg *Config) {}
func logPlatformConfigSummary(cfg Config) {}
func checkPrinter(cfg Config) error {
cmd := exec.Command("lpstat", "-p")
out, err := cmd.Output()
if err != nil {
return err
}
sc := bufio.NewScanner(bytes.NewReader(out))
sc.Split(bufio.ScanLines)
for sc.Scan() {
line := sc.Bytes()
fields := bytes.Fields(line)
if len(fields) < 2 || !bytes.Equal(fields[0], []byte("printer")) {
continue
}
if cfg.Printer.Name == "" {
return nil
}
if bytes.Equal(fields[1], []byte(cfg.Printer.Name)) {
return nil
}
}
if err := sc.Err(); err != nil {
return err
}
return errPrinterNotExist
}