-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: cmd arg mode #41
Changes from all commits
311993a
25fa801
5a98c3b
39a1e68
133e83b
158d83f
dfb349e
f399be6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
linters-settings: | ||
funlen: | ||
lines: 100 | ||
lines: 150 | ||
statements: 50 | ||
gocyclo: | ||
min-complexity: 15 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Package cmdutils contain helper methods for cmd executors | ||
package cmdutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/config" | ||
"github.com/FMotalleb/crontab-go/core/utils" | ||
"github.com/FMotalleb/crontab-go/ctxutils" | ||
) | ||
|
||
type Ctx struct { | ||
context.Context | ||
logger *logrus.Entry | ||
} | ||
|
||
func NewCtx( | ||
ctx context.Context, | ||
taskEnviron map[string]string, | ||
logger *logrus.Entry, | ||
) *Ctx { | ||
result := &Ctx{ | ||
Context: ctx, | ||
logger: logger, | ||
} | ||
result.init(taskEnviron) | ||
return result | ||
} | ||
|
||
func (ctx *Ctx) init(taskEnviron map[string]string) { | ||
osEnviron := os.Environ() | ||
ctx.logger.Trace("Initial environment variables: ", osEnviron) | ||
ctx.Context = context.WithValue( | ||
ctx, | ||
ctxutils.Environments, | ||
map[string]string{}, | ||
) | ||
for _, pair := range osEnviron { | ||
parts := strings.SplitN(pair, "=", 2) | ||
if len(parts) == 2 { | ||
ctx.envAdd(parts[0], parts[1]) | ||
} | ||
} | ||
for key, val := range taskEnviron { | ||
ctx.envAdd(key, val) | ||
switch strings.ToLower(key) { | ||
case "shell": | ||
ctx.logger.Info("you've used `SHELL` env variable in command environments, overriding the global shell with:", val) | ||
case "shell_args": | ||
ctx.logger.Info("you've used `SHELL_ARGS` env variable in command environments, overriding the global shell_args with: ", val) | ||
case "shell_arg_compatibility": | ||
ctx.logger.Info("you've used `SHELL_ARG_COMPATIBILITY` env variable in command environments, overriding the global shell_arg_compatibility with: ", val) | ||
} | ||
} | ||
} | ||
|
||
func (ctx *Ctx) envGetAll() map[string]string { | ||
if env := ctx.Value(ctxutils.Environments); env != nil { | ||
return env.(map[string]string) | ||
} | ||
return map[string]string{} | ||
} | ||
|
||
func (ctx *Ctx) envGet(key string) string { | ||
return ctx.envGetAll()[key] | ||
} | ||
|
||
func (ctx *Ctx) envAdd(key string, value string) { | ||
oldEnv := ctx.envGetAll() | ||
key = strings.ToUpper(key) | ||
oldEnv[key] = value | ||
ctx.Context = context.WithValue( | ||
ctx, | ||
ctxutils.Environments, | ||
oldEnv, | ||
) | ||
} | ||
|
||
func (ctx *Ctx) envReshape() []string { | ||
env := ctx.envGetAll() | ||
var result []string | ||
for key, val := range env { | ||
result = append(result, fmt.Sprintf("%s=%s", strings.ToUpper(key), val)) | ||
} | ||
return result | ||
} | ||
|
||
func (ctx *Ctx) getShell() string { | ||
return ctx.envGet("SHELL") | ||
} | ||
|
||
func (ctx *Ctx) getShellArg() string { | ||
return ctx.envGet("SHELL_ARGS") | ||
} | ||
|
||
func (ctx *Ctx) getShellArgCompatibility() config.ShellArgCompatibilityMode { | ||
result := config.ShellArgCompatibilityMode(ctx.envGet("SHELL_ARG_COMPATIBILITY")) | ||
switch result { | ||
case "": | ||
return config.DefaultShellArgCompatibility | ||
default: | ||
return result | ||
} | ||
} | ||
|
||
func (ctx *Ctx) BuildExecuteParams(command string, eventData []string) (shell string, cmd []string, env []string) { | ||
environments := ctx.envReshape() | ||
shell = ctx.getShell() | ||
shellArgs := utils.EscapedSplit(ctx.getShellArg(), ':') | ||
shellArgs = append(shellArgs, command) | ||
switch ctx.getShellArgCompatibility() { | ||
case config.EventArgOmit: | ||
ctx.logger.Debug("event arguments will not be passed to the command") | ||
case config.EventArgPassingAsArgs: | ||
shellArgs = append(shellArgs, eventData...) | ||
case config.EventArgPassingAsEnviron: | ||
environments = append( | ||
environments, | ||
fmt.Sprintf("CRONTAB_GO_EVENT_ARGUMENTS=%s", | ||
collectEventForEnv(eventData), | ||
), | ||
) | ||
default: | ||
ctx.logger.Warn("event argument passing mode is not supported, using default mode (omitting)") | ||
} | ||
return shell, shellArgs, environments | ||
} | ||
|
||
func collectEventForEnv(eventData []string) string { | ||
builder := &strings.Builder{} | ||
for i, part := range eventData { | ||
builder.WriteString(strings.ReplaceAll(part, ":", "\\:")) | ||
if i < len(eventData)-1 { | ||
builder.WriteRune(':') | ||
} | ||
} | ||
|
||
return builder.String() | ||
} | ||
Comment on lines
+1
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM, but add tests. The However, many lines in the file are not covered by tests according to the codecov report. Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/config" | ||
cmdutils "github.com/FMotalleb/crontab-go/core/cmd_connection/cmd_utils" | ||
"github.com/FMotalleb/crontab-go/ctxutils" | ||
) | ||
|
||
|
@@ -50,7 +51,7 @@ func NewDockerAttachConnection(log *logrus.Entry, conn *config.TaskConnection) a | |
// Returns: | ||
// - An error if the preparation fails, otherwise nil. | ||
func (d *DockerAttachConnection) Prepare(ctx context.Context, task *config.Task) error { | ||
shell, shellArgs, env := reshapeEnviron(task.Env, d.log) | ||
cmdCtx := cmdutils.NewCtx(ctx, task.Env, d.log) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM, but add tests. The code change to use the However, the added line is not covered by tests according to the codecov report. Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
d.ctx = ctx | ||
// Specify the container ID or name | ||
d.containerID = d.conn.ContainerName | ||
|
@@ -59,22 +60,17 @@ func (d *DockerAttachConnection) Prepare(ctx context.Context, task *config.Task) | |
d.conn.DockerConnection = "unix:///var/run/docker.sock" | ||
} | ||
params := ctx.Value(ctxutils.EventData).([]string) | ||
shell, shellArgs, environments := cmdCtx.BuildExecuteParams(task.Command, params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM, but add tests. The code change to use the However, the added line is not covered by tests according to the codecov report. Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
cmd := append( | ||
[]string{shell}, | ||
append( | ||
shellArgs, | ||
append( | ||
[]string{task.Command}, | ||
params..., | ||
)..., | ||
)..., | ||
shellArgs..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM, but add tests. The code change to append the However, the added line is not covered by tests according to the codecov report. Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
) | ||
// Create an exec configuration | ||
d.execCFG = &container.ExecOptions{ | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Privileged: true, | ||
Env: env, | ||
Env: environments, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM, but add tests. The code change to set the However, the added line is not covered by tests according to the codecov report. Do you want me to generate the unit testing code or open a GitHub issue to track this task? ToolsGitHub Check: codecov/patch
|
||
WorkingDir: task.WorkingDirectory, | ||
User: task.UserName, | ||
Cmd: cmd, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"bytes" | ||
"context" | ||
"io" | ||
"strings" | ||
|
||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/api/types/network" | ||
|
@@ -13,6 +12,8 @@ import ( | |
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/config" | ||
cmdutils "github.com/FMotalleb/crontab-go/core/cmd_connection/cmd_utils" | ||
"github.com/FMotalleb/crontab-go/core/utils" | ||
"github.com/FMotalleb/crontab-go/ctxutils" | ||
"github.com/FMotalleb/crontab-go/helpers" | ||
) | ||
|
@@ -53,34 +54,29 @@ func NewDockerCreateConnection(log *logrus.Entry, conn *config.TaskConnection) a | |
// Returns: | ||
// - An error if the preparation fails, otherwise nil. | ||
func (d *DockerCreateConnection) Prepare(ctx context.Context, task *config.Task) error { | ||
shell, shellArgs, env := reshapeEnviron(task.Env, d.log) | ||
cmdCtx := cmdutils.NewCtx(ctx, task.Env, d.log) | ||
d.ctx = ctx | ||
if d.conn.DockerConnection == "" { | ||
d.log.Debug("No explicit docker connection specified, using default: `unix:///var/run/docker.sock`") | ||
d.conn.DockerConnection = "unix:///var/run/docker.sock" | ||
} | ||
|
||
params := ctx.Value(ctxutils.EventData).([]string) | ||
shell, shellArgs, environments := cmdCtx.BuildExecuteParams(task.Command, params) | ||
cmd := append( | ||
[]string{shell}, | ||
append( | ||
shellArgs, | ||
append( | ||
[]string{task.Command}, | ||
params..., | ||
)..., | ||
)..., | ||
shellArgs..., | ||
) | ||
volumes := make(map[string]struct{}) | ||
for _, volume := range d.conn.Volumes { | ||
inContainer := strings.Split(volume, ":")[1] | ||
inContainer := utils.EscapedSplit(volume, ':')[1] | ||
volumes[inContainer] = struct{}{} | ||
} | ||
// Create an exec configuration | ||
d.containerConfig = &container.Config{ | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Env: env, | ||
Env: environments, | ||
Comment on lines
+57
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The The search results did not reveal any unit tests specifically targeting the
Analysis chainLGTM, but ensure the changes are thoroughly tested. The changes to the Run the following script to verify the changes: The static analysis tool indicates that the changes are not covered by tests. Do you want me to add unit tests for the Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify the `Prepare` method changes are thoroughly tested.
# Test: Search for the `Prepare` method. Expect: Corresponding unit tests.
ast-grep --lang go --pattern $'func (d *DockerCreateConnection) Prepare($_, $_) error {
$$$
}'
Length of output: 3328 Script: #!/bin/bash
# Description: Search for test files or functions that reference the `Prepare` method or the `DockerCreateConnection` struct.
# Find all Go test files.
fd --extension go --exec rg 'DockerCreateConnection|Prepare' {}
Length of output: 1360 ToolsGitHub Check: codecov/patch
|
||
WorkingDir: task.WorkingDirectory, | ||
User: task.UserName, | ||
Cmd: cmd, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but add tests.
The code changes to add a new binding for the
shell_arg_compatibility
environment variable are approved.However, the added lines are not covered by tests according to the codecov report.
Do you want me to generate the unit testing code or open a GitHub issue to track this task?
Tools
GitHub Check: codecov/patch