Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse connection #2

Draft
wants to merge 12 commits into
base: netlink
Choose a base branch
from
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.5.0 h1:HuArIo48skDwlrvM3sEdHXElYslAMsf3KwRkkW4MC4s=
Expand All @@ -211,18 +211,18 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
48 changes: 37 additions & 11 deletions pkg/clients/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Cmder interface {
GetCommand() string
GetCommand() (*Command, error)
ExtractResult(string) (map[string]string, error)
}

Expand All @@ -21,9 +21,12 @@ type Cmd struct {
cmd string
outputProcessor func(string) (string, error)
regex *regexp.Regexp
shellRegex *regexp.Regexp
fullCmd string
}

var removeCarrageReturns *regexp.Regexp = regexp.MustCompile(`\r*\n`)

func NewCmd(key, cmd string) (*Cmd, error) {
cmdInstance := Cmd{
key: key,
Expand All @@ -39,35 +42,48 @@ func NewCmd(key, cmd string) (*Cmd, error) {
}
cmdInstance.fullCmd += fmt.Sprintf("%s;", cmdInstance.suffix)

compiledRegex, err := regexp.Compile(`(?s)<` + key + `>\n` + `(.*)` + `\n</` + key + `>`)
compiledValueRegex, err := regexp.Compile(`(?s)<` + key + `>\r*\n` + `(.*?)` + `\r*\n</` + key + `>`)
if err != nil {
return nil, fmt.Errorf("failed to compile regex for key %s: %w", key, err)
}
cmdInstance.regex = compiledRegex
cmdInstance.regex = compiledValueRegex
compiledShellRegex, err := regexp.Compile(`(?s)(<` + key + `>\r*\n.*?\r*\n</` + key + `>)`)
if err != nil {
return nil, fmt.Errorf("failed to compile sehll regex for key %s: %w", key, err)
}
cmdInstance.shellRegex = compiledShellRegex
return &cmdInstance, nil
}

func (c *Cmd) SetOutputProcessor(f func(string) (string, error)) {
c.outputProcessor = f
}

func (c *Cmd) GetCommand() string {
func (c *Cmd) GetCommandString() string {
return c.fullCmd
}

func (c *Cmd) GetCommand() (*Command, error) {
cmd := Command{
Stdin: c.GetCommandString(),
regex: c.shellRegex,
}
return &cmd, nil
}

func (c *Cmd) ExtractResult(s string) (map[string]string, error) {
result := make(map[string]string)
log.Debugf("extract %s from %s", c.key, s)
match := c.regex.FindStringSubmatch(s)
log.Debugf("match %#v", match)

if len(match) > 0 {
value := match[1]
value := removeCarrageReturns.ReplaceAllString(match[1], "\n")

if c.outputProcessor != nil {
cleanValue, err := c.outputProcessor(match[1])
cleanValue, err := c.outputProcessor(value)
if err != nil {
return result, fmt.Errorf("failed to cleanup value %s of key %s", match[1], c.key)
return result, fmt.Errorf("failed to cleanup value %s of key %s", value, c.key)
}
value = cleanValue
}
Expand All @@ -86,12 +102,22 @@ func (cgrp *CmdGroup) AddCommand(c *Cmd) {
cgrp.cmds = append(cgrp.cmds, c)
}

func (cgrp *CmdGroup) GetCommand() string {
res := ""
func (cgrp *CmdGroup) GetCommand() (*Command, error) {
grpCmdStr := ""
for _, c := range cgrp.cmds {
res += c.GetCommand()
grpCmdStr += c.GetCommandString()
}
fKey := cgrp.cmds[0].key
lKey := cgrp.cmds[len(cgrp.cmds)-1].key
grpRegex, err := regexp.Compile(`(?s:(<` + fKey + `>\r*\n.*\r*\n</` + lKey + `>))`)
if err != nil {
return &Command{}, fmt.Errorf("failed to compile regex for command group %w", err)
}
res := &Command{
Stdin: grpCmdStr,
regex: grpRegex,
}
return res
return res, nil
}

func (cgrp *CmdGroup) ExtractResult(s string) (map[string]string, error) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/clients/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ var _ = Describe("CmdGrp", func() {
Expect(err).ToNot(HaveOccurred())
cmdGrp := &clients.CmdGroup{}
cmdGrp.AddCommand(cmd)
cmdString := cmdGrp.GetCommand()
Expect(cmdString).To(Equal(cmd.GetCommand()))
command, err := cmdGrp.GetCommand()
Expect(err).NotTo(HaveOccurred())
Expect(command.Stdin).To(Equal(cmd.GetCommandString()))

cmd2, err := clients.NewCmd("TestKey2", "This is another test goodbye.")
Expect(err).ToNot(HaveOccurred())
cmdGrp.AddCommand(cmd2)
cmdString2 := cmdGrp.GetCommand()
Expect(cmdString2).To(Equal(cmd.GetCommand() + cmd2.GetCommand()))
command2, err := cmdGrp.GetCommand()
Expect(err).NotTo(HaveOccurred())
Expect(command2.Stdin).To(Equal(cmd.GetCommandString() + cmd2.GetCommandString()))
})
})
When("passed a valid result", func() {
Expand Down
Loading