generated from blakewilliamson1980/cloud-print-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
62 lines (50 loc) · 1.43 KB
/
monitor.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
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// +build linux darwin freebsd
package main
import (
"fmt"
"io/ioutil"
"net"
"os"
"time"
"github.com/google/cloud-print-connector/lib"
"github.com/urfave/cli"
)
func monitorConnector(context *cli.Context) error {
config, filename, err := lib.GetConfig(context)
if err != nil {
return fmt.Errorf("Failed to read config file: %s", err)
}
if filename == "" {
fmt.Println("No config file was found, so using defaults")
}
if _, err := os.Stat(config.MonitorSocketFilename); err != nil {
if !os.IsNotExist(err) {
return err
}
return fmt.Errorf(
"No connector is running, or the monitoring socket %s is mis-configured",
config.MonitorSocketFilename)
}
timer := time.AfterFunc(context.Duration("monitor-timeout"), func() {
fmt.Fprintf(os.Stderr, "Monitor check timed out after %s", context.Duration("monitor-timeout").String())
os.Exit(1)
})
conn, err := net.DialTimeout("unix", config.MonitorSocketFilename, time.Second)
if err != nil {
return fmt.Errorf(
"No connector is running, or it is not listening to socket %s",
config.MonitorSocketFilename)
}
defer conn.Close()
buf, err := ioutil.ReadAll(conn)
if err != nil {
return err
}
timer.Stop()
fmt.Printf(string(buf))
return nil
}