This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cronner.go
87 lines (69 loc) · 1.68 KB
/
cronner.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2015 PagerDuty, Inc, et al. All rights reserved.
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
// Package main is the main thing, man.
package main
import (
"fmt"
"os"
"os/exec"
"github.com/PagerDuty/godspeed"
"github.com/codeskyblue/go-uuid"
"github.com/tideland/golib/logger"
)
// Version is the program's version string
const Version = "0.2.6"
type cmdHandler struct {
gs *godspeed.Godspeed
opts *binArgs
cmd *exec.Cmd
uuid string
hostname string
}
func main() {
logger.SetLogger(logger.NewStandardLogger(os.Stderr))
// get and parse the command line options
opts := &binArgs{}
output, err := opts.parse(nil)
// make sure parsing didn't bomb
if err != nil {
logger.Errorf("error: %v\n", err)
os.Exit(1)
}
// if parsing had output, print it and exit 0
if len(output) > 0 {
fmt.Print(output)
os.Exit(0)
}
// build a Godspeed client
var gs *godspeed.Godspeed
if opts.StatsdHost == "" {
gs, err = godspeed.NewDefault()
} else {
gs, err = godspeed.New(opts.StatsdHost, godspeed.DefaultPort, false)
}
// make sure nothing went wrong with Godspeed
if err != nil {
logger.Errorf("error: %v\n", err)
os.Exit(1)
}
gs.SetNamespace(opts.Namespace)
// get the hostname and validate nothing happened
hostname, err := os.Hostname()
if err != nil {
logger.Errorf("error: %v\n", err)
os.Exit(1)
}
handler := &cmdHandler{
opts: opts,
hostname: hostname,
gs: gs,
uuid: uuid.New(),
cmd: exec.Command(opts.Cmd, opts.CmdArgs...),
}
ret, _, _, err := handleCommand(handler)
if err != nil {
logger.Errorf(err.Error())
}
os.Exit(ret)
}