-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (59 loc) · 1.94 KB
/
main.go
File metadata and controls
74 lines (59 loc) · 1.94 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
// Copyright (c) Alex Ellis 2023. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"log"
"os"
"github.com/alexellis/fstail/pkg/fstail"
"github.com/spf13/cobra"
)
func main() {
var prefixStyle string
var disablePrefix bool
var rootCmd = &cobra.Command{
Use: "fstail",
Short: "Tail files with optional string matching",
Long: `Tail files in a directory with optional string matching and prefix styles.
fstail uses fsnotify to detect both new files, and existing files that are changed.
It then starts concatenating their contents to the terminal.`,
Example: ` # Work in current directory
fstail
# Work in /var/log/actuated
fstail /var/log/actuated
# Work in /var/log/actuated and match strings with "server error"
fstail /var/log/actuated "server error"
# Disable prefix printing
fstail --prefix none /var/log/actuated
# Print a prefix with the calculated Pod/container name
fstail --prefix k8s /var/log/containers "queue-worker"`,
RunE: func(cmd *cobra.Command, args []string) error {
var opts fstail.RunOptions
if len(args) == 1 {
opts.WorkDir = args[0]
} else if len(args) == 2 {
opts.WorkDir = args[0]
opts.Match = args[1]
} else {
cwd, err := os.Getwd()
if err != nil {
return err
}
opts.WorkDir = cwd
}
opts.DisableLogPrefix = disablePrefix
if disablePrefix {
opts.PrefixStyle = fstail.PrefixStyleNone
} else if prefixStyle == "k8s" {
opts.PrefixStyle = fstail.PrefixStyleK8s
} else {
opts.PrefixStyle = fstail.PrefixStyleFilename
}
return fstail.Run(opts)
},
}
rootCmd.Flags().StringVar(&prefixStyle, "prefix", "filename", "Prefix style: filename, k8s, or none")
rootCmd.Flags().BoolVar(&disablePrefix, "no-prefix", false, "Disable prefix printing (equivalent to --prefix none)")
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}