From 147a67d7487e47a18ce57208ff7311b550ca93ae Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Tue, 3 Dec 2024 11:04:36 +0100 Subject: [PATCH] Add support for OS args in `in` Make sure to pipe through the OS args. --- cmd/in/main.go | 2 +- internal/dtr/common.go | 27 +++++++++++++++++---------- internal/dtr/in.go | 4 ++-- internal/dtr/in_test.go | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/cmd/in/main.go b/cmd/in/main.go index f220e75..14c926a 100644 --- a/cmd/in/main.go +++ b/cmd/in/main.go @@ -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", diff --git a/internal/dtr/common.go b/internal/dtr/common.go index f352b40..0c02f53 100644 --- a/internal/dtr/common.go +++ b/internal/dtr/common.go @@ -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 @@ -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) } @@ -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) } @@ -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 diff --git a/internal/dtr/in.go b/internal/dtr/in.go index a71f21a..8044af8 100644 --- a/internal/dtr/in.go +++ b/internal/dtr/in.go @@ -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 } diff --git a/internal/dtr/in_test.go b/internal/dtr/in_test.go index 8e75fc8..a950022 100644 --- a/internal/dtr/in_test.go +++ b/internal/dtr/in_test.go @@ -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() {