This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsed.go
More file actions
110 lines (94 loc) · 2.54 KB
/
sed.go
File metadata and controls
110 lines (94 loc) · 2.54 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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/ylacancellera/galera-log-explainer/regex"
"github.com/ylacancellera/galera-log-explainer/types"
)
type sed struct {
Paths []string `arg:"" name:"paths" help:"paths of the log to use"`
ByIP bool `help:"Replace by IP instead of name"`
}
func (s *sed) Help() string {
return `sed translates a log, replacing node UUID, IPS, names with either name or IP everywhere. By default it replaces by name.
Use like so:
cat node1.log | galera-log-explainer sed *.log | less
galera-log-explainer sed *.log < node1.log | less
You can also simply call the command to get a generated sed command to review and apply yourself
galera-log-explainer sed *.log`
}
func (s *sed) Run() error {
toCheck := regex.AllRegexes()
timeline, err := timelineFromPaths(s.Paths, toCheck)
if err != nil {
return errors.Wrap(err, "Found nothing worth replacing")
}
ctxs := timeline.GetLatestUpdatedContextsByNodes()
args := []string{}
for key, ctx := range ctxs {
tosearchs := []string{key}
tosearchs = append(tosearchs, ctx.OwnHashes...)
tosearchs = append(tosearchs, ctx.OwnIPs...)
tosearchs = append(tosearchs, ctx.OwnNames...)
for _, tosearch := range tosearchs {
ni := whoIs(ctxs, tosearch)
fmt.Println(ni)
switch {
case CLI.Sed.ByIP:
args = append(args, sedByIP(ni)...)
default:
args = append(args, sedByName(ni)...)
}
}
}
if len(args) == 0 {
return errors.New("Could not find informations to replace")
}
fstat, err := os.Stdin.Stat()
if err != nil {
return err
}
if fstat.Size() == 0 {
fmt.Println("No files found in stdin, returning the sed command instead:")
fmt.Println("sed", strings.Join(args, " "))
return nil
}
cmd := exec.Command("sed", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}
func sedByName(ni types.NodeInfo) []string {
if len(ni.NodeNames) == 0 {
return nil
}
elem := ni.NodeNames[0]
args := sedSliceWith(ni.NodeUUIDs, elem)
args = append(args, sedSliceWith(ni.IPs, elem)...)
return args
}
func sedByIP(ni types.NodeInfo) []string {
if len(ni.IPs) == 0 {
return nil
}
elem := ni.IPs[0]
args := sedSliceWith(ni.NodeUUIDs, elem)
args = append(args, sedSliceWith(ni.NodeNames, elem)...)
return args
}
func sedSliceWith(elems []string, replace string) []string {
args := []string{}
for _, elem := range elems {
args = append(args, "-e")
args = append(args, "s/"+elem+"/"+replace+"/g")
}
return args
}