This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathcommand.go
More file actions
163 lines (132 loc) · 3.4 KB
/
Copy pathcommand.go
File metadata and controls
163 lines (132 loc) · 3.4 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package logcmd
import (
"errors"
"fmt"
"io"
"koding/klient/logfetcher"
"koding/klientctl/config"
"koding/klientctl/ctlcli"
"koding/klientctl/util"
"os"
"github.com/koding/logging"
)
// Options for the command, generally mapped 1:1 to
// CLI options for the given command.
type Options struct {
Debug bool
Lines int
KdLog bool
KlientLog bool
KdLogLocation string
KlientLogLocation string
}
// Init contains various instances required for a Command instance to be initialized.
type Init struct {
Stdout io.Writer
Log logging.Logger
// The ctlcli Helper. See the type docs for a better understanding of this.
Helper ctlcli.Helper
}
func (i Init) CheckValid() error {
if i.Stdout == nil {
return errors.New("MissingArgument: Stdout")
}
if i.Log == nil {
return errors.New("MissingArgument: Log")
}
if i.Helper == nil {
return errors.New("MissingArgument: Helper")
}
return nil
}
// Command implements the klientctl.Command interface for `kd sync`
type Command struct {
// Embedded Init gives us our Klient/etc instances.
Init
Options Options
Stdout *util.Fprint
}
func NewCommand(i Init, o Options) (*Command, error) {
if err := i.CheckValid(); err != nil {
return nil, err
}
if o.Debug {
i.Log.SetLevel(logging.DEBUG)
}
c := &Command{
Init: i,
Options: o,
// Override the init stdout writer with an Fprint writer
Stdout: util.NewFprint(i.Stdout),
}
return c, nil
}
// Help prints help to the caller.
func (c *Command) Help() {
c.Helper(c.Stdout)
}
func (c *Command) Run() (int, error) {
if err := c.handleOptions(); err != nil {
return 1, err
}
if err := c.tailLogs(); err != nil {
return 2, err
}
return 0, nil
}
// handleOptions deals with options, erroring if options are missing, etc.
func (c *Command) handleOptions() error {
if c.Options.KdLogLocation == "" {
c.Options.KdLogLocation = config.GetKdLogPath()
}
if c.Options.KlientLogLocation == "" {
c.Options.KlientLogLocation = config.GetKlientLogPath()
}
if c.Options.Lines == 0 {
c.Options.Lines = 20
}
return nil
}
func (c *Command) tailFile(path string, n int) error {
f, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
return err
}
// NOTE(leeola): On some systems, seemingly at random, the opened file is
// nil. The resulting error would come from GetOffsetLines' use of Seek,
// complaining about an invalid argument.
//
// To be a bit less obtuse, i'm returning a custom message and error here.
if f == nil {
c.Log.Warning("Nil file encountered, with no error explaining why. path:%s", path)
return fmt.Errorf("File opened without err, but file is nil. path:%s", path)
}
defer f.Close()
lines, err := logfetcher.GetOffsetLines(f, 1024, n)
if err != nil {
return err
}
for _, l := range lines {
c.Stdout.Printlnf(l)
}
return nil
}
func (c *Command) tailLogs() (err error) {
if c.Options.KdLog {
logLoc := c.Options.KdLogLocation
err = c.tailFile(logLoc, c.Options.Lines)
// Just logging here, because we want to print both logs if possible.
if err != nil {
c.Log.Error("Tailing %q returned err: %s", logLoc, err)
}
}
if c.Options.KlientLog {
logLoc := c.Options.KlientLogLocation
err = c.tailFile(logLoc, c.Options.Lines)
// Just logging here, because we want to print both logs if possible.
if err != nil {
c.Log.Error("Tailing %q returned err: %s", logLoc, err)
}
}
return err
}