Skip to content

Commit

Permalink
Accept tee stdin via string once again (#114)
Browse files Browse the repository at this point in the history
* Better errors

* Regen SDKs

* Accept stdin via string again

* Regen SDKs
  • Loading branch information
UnstoppableMango authored Aug 12, 2024
1 parent 4b77220 commit 25a4232
Show file tree
Hide file tree
Showing 26 changed files with 285 additions and 104 deletions.
2 changes: 1 addition & 1 deletion provider/pkg/provider/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CommandArgs[T Builder] struct {
CustomDelete []string `pulumi:"customDelete,optional"`
}

func (a *CommandArgs[T]) Cmd() *pb.Command {
func (a *CommandArgs[T]) Cmd() (*pb.Command, error) {
return a.Args.Cmd()
}

Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/provider/cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pb "github.com/unmango/pulumi-baremetal/gen/go/unmango/baremetal/v1alpha1

type Builder interface {
FsManipulator
Cmd() *pb.Command
Cmd() (*pb.Command, error)
}

type B struct {
Expand Down
8 changes: 7 additions & 1 deletion provider/pkg/provider/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func (s *State[T]) Create(ctx context.Context, inputs CommandArgs[T], preview bo
return nil
}

command, err := inputs.Cmd()
if err != nil {
log.Errorf("Failed constructing command: %s", err)
return err
}

p, err := provisioner.FromContext(ctx)
if err != nil {
log.Error("failed creating provisioner")
Expand All @@ -25,7 +31,7 @@ func (s *State[T]) Create(ctx context.Context, inputs CommandArgs[T], preview bo

log.InfoStatus("Sending create request to provisioner")
res, err := p.Create(ctx, &pb.CreateRequest{
Command: inputs.Cmd(),
Command: command,
ExpectCreated: inputs.ExpectCreated(),
ExpectMoved: inputs.ExpectMoved(),
})
Expand Down
19 changes: 8 additions & 11 deletions provider/pkg/provider/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ func (s *State[T]) Delete(ctx context.Context) error {
log.InfoStatus("Normal delete")
}

prev, err := s.Operation()
if err != nil {
log.Errorf("Failed generating operation from state: %s", err)
return fmt.Errorf("failed to generate operation from state: %w", err)
}

log.InfoStatus("Sending delete request to provisioner")
res, err := p.Delete(ctx, &pb.DeleteRequest{
Command: command,
Previous: &pb.Operation{
Command: s.Cmd(),
CreatedFiles: s.CreatedFiles,
MovedFiles: s.MovedFiles,
Result: &pb.Result{
ExitCode: int32(s.ExitCode),
Stdout: s.Stdout,
Stderr: s.Stderr,
},
},
Command: command,
Previous: prev,
})
if err != nil {
return fmt.Errorf("sending delete request: %w", err)
Expand Down
24 changes: 24 additions & 0 deletions provider/pkg/provider/cmd/state.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package cmd

import (
"fmt"

pb "github.com/unmango/pulumi-baremetal/gen/go/unmango/baremetal/v1alpha1"
)

type State[T Builder] struct {
CommandArgs[T]

Expand All @@ -20,3 +26,21 @@ func (s *State[T]) Copy() State[T] {
MovedFiles: s.MovedFiles,
}
}

func (s *State[T]) Operation() (*pb.Operation, error) {
command, err := s.Cmd()
if err != nil {
return nil, fmt.Errorf("failed to build command from state; %w", err)
}

return &pb.Operation{
Command: command,
CreatedFiles: s.CreatedFiles,
MovedFiles: s.MovedFiles,
Result: &pb.Result{
ExitCode: int32(s.ExitCode),
Stdout: s.Stdout,
Stderr: s.Stderr,
},
}, nil
}
25 changes: 14 additions & 11 deletions provider/pkg/provider/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,31 @@ func (s *State[T]) Update(ctx context.Context, inputs CommandArgs[T], preview bo
return s.Copy(), fmt.Errorf("parsing custom command: %w", err)
}
} else {
command = inputs.Cmd()
command, err = inputs.Cmd()
if err != nil {
log.Errorf("Failed to build command from inputs: %s", err)
return s.Copy(), fmt.Errorf("failed to build command from inputs: %w", err)
}

expectCreated = inputs.ExpectCreated()
expectMoved = inputs.ExpectMoved()
}

prev, err := s.Operation()
if err != nil {
log.Errorf("Failed generating operation from state: %s", err)
return s.Copy(), fmt.Errorf("failed to generate operation from state: %w", err)
}

log.DebugStatus("Sending update request to provisioner")
res, err := p.Update(ctx, &pb.UpdateRequest{
Command: command,
ExpectCreated: expectCreated,
ExpectMoved: expectMoved,
Previous: &pb.Operation{
Command: s.Cmd(),
CreatedFiles: s.CreatedFiles,
MovedFiles: s.MovedFiles,
Result: &pb.Result{
ExitCode: int32(s.ExitCode),
Stdout: s.Stdout,
Stderr: s.Stderr,
},
},
Previous: prev,
})
if err != nil {
log.Errorf("Failed sending update request: %s", err)
return s.Copy(), fmt.Errorf("sending update request: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/chmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type ChmodArgs struct {
}

// Cmd implements CommandArgs.
func (m ChmodArgs) Cmd() *pb.Command {
func (m ChmodArgs) Cmd() (*pb.Command, error) {
b := cmd.B{}
b.Op(m.Changes, "--changes")
b.Op(m.NoPreserveRoot, "--no-preserve-root")
Expand All @@ -50,7 +50,7 @@ func (m ChmodArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_CHMOD,
Args: b.Args,
}
}, nil
}

var _ cmd.Builder = ChmodArgs{}
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type MkdirArgs struct {
}

// Cmd implements CommandArgs.
func (m MkdirArgs) Cmd() *pb.Command {
func (m MkdirArgs) Cmd() (*pb.Command, error) {
b := cmd.B{Args: m.Directory}

b.Opv(m.Mode, "--mode")
Expand All @@ -33,7 +33,7 @@ func (m MkdirArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_MKDIR,
Args: b.Args,
}
}, nil
}

var _ cmd.Builder = MkdirArgs{}
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/mktemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type MktempArgs struct {
}

// Cmd implements CommandArgs.
func (m MktempArgs) Cmd() *pb.Command {
func (m MktempArgs) Cmd() (*pb.Command, error) {
b := cmd.B{}

b.Op(m.Directory, "--directory")
Expand All @@ -46,7 +46,7 @@ func (m MktempArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_MKTEMP,
Args: b.Args,
}
}, nil
}

var _ cmd.Builder = MktempArgs{}
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type MvArgs struct {
}

// Cmd implements CommandArgs.
func (m MvArgs) Cmd() *pb.Command {
func (m MvArgs) Cmd() (*pb.Command, error) {
b := cmd.B{Args: m.Source}

b.Opv(m.Backup, "--backup")
Expand All @@ -53,7 +53,7 @@ func (m MvArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_MV,
Args: b.Args,
}
}, nil
}

// ExpectMoved implements FileManipulator.
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type RmArgs struct {
}

// Cmd implements CommandArgs.
func (r RmArgs) Cmd() *pb.Command {
func (r RmArgs) Cmd() (*pb.Command, error) {
b := cmd.B{Args: r.Files}

b.Op(r.Dir, "--dir")
Expand All @@ -36,7 +36,7 @@ func (r RmArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_RM,
Args: b.Args,
}
}, nil
}

var _ cmd.Builder = RmArgs{}
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type TarArgs struct {
}

// Cmd implements CommandArgs.
func (t TarArgs) Cmd() *pb.Command {
func (t TarArgs) Cmd() (*pb.Command, error) {
b := cmd.B{Args: t.Args}

b.Op(t.Append, "--append")
Expand Down Expand Up @@ -126,7 +126,7 @@ func (t TarArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_TAR,
Args: b.Args,
}
}, nil
}

// ExpectCreated implements CommandArgs.
Expand Down
20 changes: 13 additions & 7 deletions provider/pkg/provider/coreutils/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@ type TeeArgs struct {
cmd.ArgsBase

Append bool `pulumi:"append,optional"`
Content asset.Asset `pulumi:"content"`
Content asset.Asset `pulumi:"content,optional"`
Files []string `pulumi:"files"`
Stdin string `pulumi:"stdin,optional"`
}

func (o TeeArgs) Cmd() *pb.Command {
func (o TeeArgs) Cmd() (*pb.Command, error) {
args := []string{}
if o.Append {
args = append(args, "--append")
}

data, err := o.Content.Bytes()
if err != nil {
panic(err)
var stdin string
if len(o.Stdin) > 0 {
stdin = o.Stdin
} else {
data, err := o.Content.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to read asset bytes: %w", err)
}
stdin = string(data)
}

stdin := string(data)
return &pb.Command{
Bin: pb.Bin_BIN_TEE,
Args: append(args, o.Files...),
Stdin: &stdin,
}
}, nil
}

// ExpectCreated implements FileManipulator.
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/coreutils/wget.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type WgetArgs struct {
}

// Cmd implements CommandArgs.
func (w WgetArgs) Cmd() *pb.Command {
func (w WgetArgs) Cmd() (*pb.Command, error) {
b := &cmd.B{Args: w.Urls}

b.Opv(w.AppendOutput, "--append-output")
Expand Down Expand Up @@ -118,7 +118,7 @@ func (w WgetArgs) Cmd() *pb.Command {
return &pb.Command{
Bin: pb.Bin_BIN_WGET,
Args: b.Args,
}
}, nil
}

// ExpectCreated implements FileManipulator.
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/kubeadm/config/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ImagesArgs struct {
Rootfs string `pulumi:"rootfs,optional"`
}

func (a ImagesArgs) Cmd() *pb.Command {
func (a ImagesArgs) Cmd() (*pb.Command, error) {
return builder(func(b *cmd.B) {
b.Arg("images")
b.Arg(string(a.Command))
Expand All @@ -48,7 +48,7 @@ func (a ImagesArgs) Cmd() *pb.Command {
b.Opv(a.KubernetesVersion, "--kubernetes-version")
b.Opv(a.Kubeconfig, "--kubeconfig")
b.Opv(a.Rootfs, "--rootfs")
})
}), nil
}

type Images struct{}
Expand Down
3 changes: 2 additions & 1 deletion provider/pkg/provider/kubeadm/config/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ var _ = Describe("Images command", func() {
It("should properly format arguments", func() {
args := config.ImagesArgs{Command: config.Pull}

cmd := args.Cmd()
cmd, err := args.Cmd()

Expect(err).NotTo(HaveOccurred())
Expect(cmd.Bin).To(Equal(pb.Bin_BIN_KUBEADM))
Expect(cmd.Args).To(Equal([]string{
"config",
Expand Down
4 changes: 2 additions & 2 deletions provider/pkg/provider/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ type KubeadmArgs struct {
Commands []string `pulumi:"commands"`
}

func (a KubeadmArgs) Cmd() *pb.Command {
func (a KubeadmArgs) Cmd() (*pb.Command, error) {
return Builder(func(b *cmd.B) {
for _, c := range a.Commands {
b.Arg(c)
}
})
}), nil
}

type Kubeadm struct{}
Expand Down
7 changes: 5 additions & 2 deletions sdk/dotnet/Coreutils/Inputs/TeeArgsArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public sealed class TeeArgsArgs : global::Pulumi.ResourceArgs
[Input("append")]
public Input<bool>? Append { get; set; }

[Input("content", required: true)]
public Input<AssetOrArchive> Content { get; set; } = null!;
[Input("content")]
public Input<AssetOrArchive>? Content { get; set; }

[Input("files", required: true)]
private InputList<string>? _files;
Expand All @@ -27,6 +27,9 @@ public InputList<string> Files
set => _files = value;
}

[Input("stdin")]
public Input<string>? Stdin { get; set; }

public TeeArgsArgs()
{
}
Expand Down
Loading

0 comments on commit 25a4232

Please sign in to comment.