Skip to content

Commit

Permalink
switch to shell token library
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Feb 20, 2024
1 parent 853aff3 commit d1615fb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 60 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ issues:
- 'O_CREATE contains underscore'
- 'Duplicate words.*sadhajshdka'
- 'FactorLog.*does not support error-wrapping directive'
- 'replacement are not allowed: utils'
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mod-Gearman-Worker

[![Build Status](https://github.com/ConSol/mod-gearman-worker-go/workflows/citest/badge.svg)](https://github.com/ConSol/mod-gearman-worker-go/actions?query=workflow:citest)
[![Go Report Card](https://goreportcard.com/badge/github.com/ConSol/mod-gearman-worker-go)](https://goreportcard.com/report/github.com/ConSol/mod-gearman-worker-go)
[![Build Status](https://github.com/consol-monitoring/mod-gearman-worker-go/workflows/citest/badge.svg)](https://github.com/consol-monitoring/mod-gearman-worker-go/actions?query=workflow:citest)
[![Go Report Card](https://goreportcard.com/badge/github.com/consol-monitoring/mod-gearman-worker-go)](https://goreportcard.com/report/github.com/consol-monitoring/mod-gearman-worker-go)
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)

this is a Mod-Gearman-Worker rewrite in golang. It supports all original
Expand Down Expand Up @@ -35,12 +35,12 @@ Prometheus metrics will get exported when started with the `prometheus-server` o

Either use `go install` like:

%> go install github.com/ConSol/mod-gearman-worker-go/cmd/mod_gearman_worker
%> go install github.com/ConSol/mod-gearman-worker-go/cmd/send_gearman
%> go install github.com/consol-monitoring/mod-gearman-worker-go/cmd/mod_gearman_worker
%> go install github.com/consol-monitoring/mod-gearman-worker-go/cmd/send_gearman

Or clone the repository and build it manually:

%> go get github.com/ConSol/mod-gearman-worker-go
%> go get github.com/consol-monitoring/mod-gearman-worker-go
%> cd $GOPATH/src/ConSol/mod-gearman-worker-go
%> make

Expand Down
2 changes: 1 addition & 1 deletion cmd/mod_gearman_worker/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
modgearman "github.com/ConSol/mod-gearman-worker-go"
modgearman "github.com/consol-monitoring/mod-gearman-worker-go"
)

// Build contains the current git commit id
Expand Down
2 changes: 1 addition & 1 deletion cmd/send_gearman/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
modgearman "github.com/ConSol/mod-gearman-worker-go"
modgearman "github.com/consol-monitoring/mod-gearman-worker-go"
)

// Build contains the current git commit id
Expand Down
48 changes: 7 additions & 41 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"regexp"
"strings"

"utils"
"github.com/sni/shelltoken"
)

// CommandExecType is used to set the execution path
Expand Down Expand Up @@ -60,31 +60,18 @@ func parseCommand(rawCommand string, config *configurationStruct) *command {
return parsed
}

var envs []string
var args []string
var err error
if !strings.ContainsAny(rawCommand, `'"`) {
envs, args = parseShellArgsWithoutQuotes(rawCommand)
} else {
// don't try to parse super long command lines, shellwords is pretty slow
if len(rawCommand) > 100000 {
return parsed
}

args, err = utils.TokenizeShell(rawCommand)
if err != nil {
logger.Debugf("failed to parse shell args: %w: %s", err, err.Error())
return parsed
}
envs, args = extractEnvFromArgv(args)
envs, args, err := shelltoken.Parse(rawCommand)
if err != nil {
logger.Debugf("failed to parse shell args: %w: %s", err, err.Error())
return parsed
}

parsed.Command = args[0]
parsed.Args = args[1:]
parsed.ExecType = Exec
for _, env := range envs {
splitted := strings.SplitN(env, "=", 2)
val, _ := utils.TrimQuotes(splitted[1])
parsed.Env[splitted[0]] = val
parsed.Env[splitted[0]] = splitted[1]
}

if fileUsesEmbeddedPerl(parsed.Command, config) {
Expand All @@ -110,24 +97,3 @@ func parseCommand(rawCommand string, config *configurationStruct) *command {

return parsed
}

func parseShellArgsWithoutQuotes(rawCommand string) (envs []string, args []string) {
splitted := strings.Fields(rawCommand)

return extractEnvFromArgv(splitted)
}

func extractEnvFromArgv(argv []string) (envs, args []string) {
inEnv := true
for _, s := range argv {
if inEnv {
if strings.Contains(s, "=") {
envs = append(envs, s)
continue
}
inEnv = false
}
args = append(args, s)
}
return
}
12 changes: 3 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
module github.com/ConSol/mod-gearman-worker-go
module github.com/consol-monitoring/mod-gearman-worker-go

go 1.21

require (
github.com/appscode/g2 v0.0.0-20190123131438-388ba74fd273
github.com/consol-monitoring/check_nsc_web/pkg/checknscweb v0.0.0-20231227233328-73f534d3989b
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/kdar/factorlog v0.0.0-20211012144011-6ea75a169038
github.com/prometheus/client_golang v1.18.0
github.com/sevlyar/go-daemon v0.1.6
github.com/sni/shelltoken v0.0.0-20240220170033-a140281ef06f
github.com/stretchr/testify v1.8.4
)

require (
github.com/consol-monitoring/check_nsc_web/pkg/checknscweb v0.0.0-20231227233328-73f534d3989b
utils v0.0.0-00010101000000-000000000000
)

replace utils => github.com/consol-monitoring/snclient/pkg/utils v0.0.0-20240219164943-8e08589f92a4

require (
github.com/appscode/go v0.0.0-20201105063637-5613f3b8169f // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -34,7 +29,6 @@ require (
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.47.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/robfig/cron.v2 v2.0.0-20150107220207-be2e0b0deed5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcju
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE=
github.com/consol-monitoring/check_nsc_web/pkg/checknscweb v0.0.0-20231227233328-73f534d3989b h1:YaHS/RbFnEfBVAZxp1TdKSfFK1OcTs01kKZL0SmI9Nc=
github.com/consol-monitoring/check_nsc_web/pkg/checknscweb v0.0.0-20231227233328-73f534d3989b/go.mod h1:TRUXpzEJY2vnFaQv5mLCxYSPC8oG+hHf76lQekIlIyw=
github.com/consol-monitoring/snclient/pkg/utils v0.0.0-20240219164943-8e08589f92a4 h1:d59mc5muDmucM1+7cESSeWeLtzZjMPqf62QFQd/zhtg=
github.com/consol-monitoring/snclient/pkg/utils v0.0.0-20240219164943-8e08589f92a4/go.mod h1:d5od47ql/Ua8S+Ab900Biy5j56EhmSffHGvtSAcDa+8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down Expand Up @@ -75,6 +73,8 @@ github.com/sevlyar/go-daemon v0.1.6 h1:EUh1MDjEM4BI109Jign0EaknA2izkOyi0LV3ro3QQ
github.com/sevlyar/go-daemon v0.1.6/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/sni/shelltoken v0.0.0-20240220170033-a140281ef06f h1:88WYU7vcTbjLkh6NWpFJb3LHlZtrn4uVQo1jdT7ttT0=
github.com/sni/shelltoken v0.0.0-20240220170033-a140281ef06f/go.mod h1:66YbXoN5mTer9RJRxauOJxIrDBDqNVZtDbbsylk0vW8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit d1615fb

Please sign in to comment.