Skip to content

Commit

Permalink
let podman error if container name exists, remove unused cp func (kon…
Browse files Browse the repository at this point in the history
…veyor#106)

Signed-off-by: Emily McMullan <[email protected]>
  • Loading branch information
eemcmullan committed Nov 17, 2023
1 parent 7090446 commit d08c52d
Showing 1 changed file with 0 additions and 51 deletions.
51 changes: 0 additions & 51 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,11 @@ func NewContainer(log logr.Logger) *container {
}
}

func (c *container) Exists(ctx context.Context) (bool, error) {
cmd := exec.CommandContext(ctx,
Settings.PodmanBinary,
"ps", "-a", "--format", "{{.Names}}")
output, err := cmd.CombinedOutput()
if err != nil {
return false, fmt.Errorf("%w failed checking status of container %s", err, c.name)
}
for _, found := range strings.Split(string(output), "\n") {
if found == c.name {
return true, nil
}
}
return false, nil
}

func (c *container) Run(ctx context.Context, opts ...Option) error {
var err error
for _, opt := range opts {
opt(c)
}
exists, err := c.Exists(ctx)
if err != nil {
return fmt.Errorf("%w failed to check status of container %s", err, c.name)
}
if exists {
return fmt.Errorf("container %s already exists, must remove existing before running", c.name)
}
args := []string{"run"}
os := runtime.GOOS
if c.cleanup {
Expand All @@ -175,7 +152,6 @@ func (c *container) Run(ctx context.Context, opts ...Option) error {
}
for sourcePath, destPath := range c.volumes {
args = append(args, "-v")
// TODO: check this on windows
if os == "linux" {
args = append(args, fmt.Sprintf("%s:%s:Z",
filepath.Clean(sourcePath), path.Clean(destPath)))
Expand Down Expand Up @@ -219,34 +195,7 @@ func (c *container) Run(ctx context.Context, opts ...Option) error {
return nil
}

func (c *container) Cp(ctx context.Context, src string, dest string) error {
if src == "" || dest == "" {
return fmt.Errorf("source or dest cannot be empty")
}
exists, err := c.Exists(ctx)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("container %s does not exist, cannot copy from non-existing container", c.name)
}
cmd := exec.CommandContext(
ctx,
Settings.PodmanBinary,
"cp", fmt.Sprintf("%s:%s", c.name, src), dest)
c.log.V(1).Info("copying files from container",
"podman", Settings.PodmanBinary, "src", src, "dest", dest)
return cmd.Run()
}

func (c *container) Rm(ctx context.Context) error {
exists, err := c.Exists(ctx)
if err != nil {
return err
}
if !exists {
return nil
}
cmd := exec.CommandContext(
ctx,
Settings.PodmanBinary,
Expand Down

0 comments on commit d08c52d

Please sign in to comment.