-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_linux.go
50 lines (45 loc) · 970 Bytes
/
print_linux.go
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
package main
import (
"bufio"
"bytes"
"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...)
return cmd.Run()
}
func checkDependencies() {
_, err := exec.LookPath("lpr")
if err != nil {
pog.Fatal("Missing dependency: could not find lpr")
}
}
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
}
}
return errPrinterNotExist
}