Fork me on GitHub

plash b


Usage

plash b PLASHCMD *BUILDARGS [-- *CMDARGS]

Description

Build then run utility. Builds the given build commands and calls the given
plash command with the builded container.

Example

$ plash b run -A
$ plash b run --eval-file ./plashfile -- ls

Tested Behaviour

#!/bin/bash

set -eux

• b builds flag arguments and passes it to command

test "$(plash b nodepath -f 1)" = "$(plash nodepath 1)"

• no build build flags fail

[[ "$(plash b nodepath 1)" = *"no build arguments"*]]
! plash b nodepath 1
[[ "$(plash b nodepath 1 2 3 4)" = *"no build arguments"*]]
! plash b nodepath 1 2 3 4

• no arguments fail

[[ "$(plash b nodepath)" = "usage: plash b"*]]
! plash b nodepath

• build arguments can be in the beginning or end

plash b with-mount true -A
plash b with-mount -A -- true

Source Code


#define USAGE "usage: plash b PLASHCMD *BUILDARGS [-- *CMDARGS]\n"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <plash.h>

int b_main(int argc, char *argv[]) {
  if (argc < 2) {
    fputs(USAGE, stderr);
    return EXIT_FAILURE;
  }

  char **buildargs = argv;
  char **cmdargs = (char *[]){NULL};
  char *cmd = strdup(argv[1]);
  if (cmd == NULL)
    pl_fatal("strdup");

  // reuse argv to build command to build the image
  *argv = "/proc/self/exe";
  argv++;
  *argv = "build";
  argv++;

  while (*(++argv)) {
    if ((*argv)[0] == '-' && (*argv)[1] == '-' && (*argv)[2] == '\0') {
      // "--" found, cut the array here
      *argv = NULL;
      cmdargs = argv + 1;
    }
  }

  // Run the requested command with the builded image
  pl_exec_add("/proc/self/exe");
  pl_exec_add(cmd);
  pl_exec_add(pl_firstline(pl_check_output(buildargs)));
  while (*(cmdargs))
    pl_exec_add(*cmdargs++);
  pl_exec_add(NULL);
  return EXIT_FAILURE;
}