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

Add support for OS args in in #110

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/in/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func main() {
result, err := dtr.In(os.Stdin)
result, err := dtr.In(os.Stdin, os.Args[1:]...)
if err != nil {
fmt.Fprint(os.Stderr, neat.ContentBox(
"Failed to run in",
Expand Down
27 changes: 17 additions & 10 deletions internal/dtr/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ func LoadConfig(in io.Reader) (*Config, error) {
return &config, nil
}

func command(ctx context.Context, envs map[string]string, run string, out io.Writer) *exec.Cmd {
cmd := exec.CommandContext(ctx, defaultCommand, "-c", run)
func command(ctx context.Context, out io.Writer, envs map[string]string, run string, optArgs ...string) *exec.Cmd {
args := []string{"-c", run}
if len(optArgs) != 0 {
args = append(args, "--")
args = append(args, optArgs...)
}

cmd := exec.CommandContext(ctx, defaultCommand, args...)
cmd.Stdin = nil
cmd.Stdout = out
cmd.Stderr = os.Stderr
Expand All @@ -100,12 +106,12 @@ func command(ctx context.Context, envs map[string]string, run string, out io.Wri
return cmd
}

func execute(entry Custom) ([]string, error) {
func execute(entry Custom, args ...string) ([]string, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if entry.Before != "" {
before := command(ctx, entry.Env, entry.Before, os.Stderr)
before := command(ctx, os.Stderr, entry.Env, entry.Before)
if err := before.Run(); err != nil {
return nil, fmt.Errorf("failure while running before command: %w", err)
}
Expand All @@ -118,7 +124,7 @@ func execute(entry Custom) ([]string, error) {
// For this scenario we would still like to allow no-op In runs
// Therefore we can skip command execution if nothing is defined
if entry.Run != "" {
run := command(ctx, entry.Env, entry.Run, &outStream)
run := command(ctx, &outStream, entry.Env, entry.Run, args...)
if err := run.Run(); err != nil {
return nil, fmt.Errorf("failure while running run command: %w", err)
}
Expand All @@ -137,11 +143,12 @@ func execute(entry Custom) ([]string, error) {
func metadata(list []string) []Metadata {
var metadata []Metadata
for _, entry := range list {
split := strings.SplitN(entry, " ", 2)
metadata = append(metadata, Metadata{
Name: split[0],
Value: split[1]},
)
if split := strings.SplitN(entry, " ", 2); len(split) == 2 {
metadata = append(metadata, Metadata{
Name: split[0],
Value: split[1]},
)
}
}

return metadata
Expand Down
4 changes: 2 additions & 2 deletions internal/dtr/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
"io"
)

func In(in io.Reader) (InOutResult, error) {
func In(in io.Reader, args ...string) (InOutResult, error) {
config, err := LoadConfig(in)
if err != nil {
return InOutResult{}, err
}

output, err := execute(config.Source.In)
output, err := execute(config.Source.In, args...)
if err != nil {
return InOutResult{}, err
}
Expand Down
19 changes: 19 additions & 0 deletions internal/dtr/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ var _ = Describe("In", func() {
{Name: "bar", Value: "foo"},
}))
})

It("should have access to the OS arguments", func() {
version := Version{"ref": "foobar"}
result, err := In(
feed(Config{
Source: Source{
In: Custom{Run: "echo foo $1"},
},
Version: version,
}),
"bar",
)

Expect(err).NotTo(HaveOccurred())
Expect(result.Version).To(Equal(version))
Expect(result.Metadata).To(Equal([]Metadata{
{Name: "foo", Value: "bar"},
}))
})
})

Context("empty/no-op configuration", func() {
Expand Down
Loading