The dx CLI pattern

Install the following script into /usr/bin/dx to create a dx command that works in any repository. Ideally it understands the context and can be run largely argument and flag free. This pattern is paired well with CUE based systems for a software catalog and config for building and versions.

The dx command is intended to be run both locally and in CI to create exact parity between the two development phases. The underlying tool that runs is a custom CLI, used the Dagger SDK to perform most operations in containers, but can also access the host as needed. It also needs to accept a --cwd flag to understand where it was run from, but always run from the root of the repo so it has access to the full context. This makes things easier, trust me.

#!/bin/bash
set -euo pipefail


# make sure we return to the original dir this was run from
ORIG=$(pwd)
final () {
  cd $ORIG
}
trap final EXIT 1 2 3 6 15

# we have to run Dagger from the root of the repo
ROOT="$(git rev-parse --show-toplevel)"
cd $ROOT

# disbale interactive if running in CI
INTERACTIVE_FLAG="--interactive"
CI=${CI:-}
if [[ ! -z $CI ]]; then
  INTERACTIVE_FLAG=
fi

# go run | python | tsx
DAGGER_CMD=go run

# always run, not build, so we have branch local code
dagger \
  $INTERACTIVE_FLAG $DAGGER_CMD ./ci/dx \
  --cwd $ORIG \
  $@

Monorepo organization

This is the general layout for a good monorepo setup with multiple teams, products, and languages.

(root)/
  .git
  bfg/*
  ci/
    dx/...
    lib/...
    local/...
    stg/...
    prd/...
  config/...
  schemas/
    inv/...
    cfg/...
  src/
    utils/
    libs/
      go/...
      py/...
    <product>/
      <service>/
    devops/

  <lang-files|dirs>/...
  <tool-files|dirs>/...
  <temporary-stuff>

CUE subsystems

There are two primary CUE powered systems. This is paired with the monorepo pattern and git-flow branching and release concepts.

  1. BFG - branch config, things that have wide impact and are configured on a branch or PR or controls for your CI that depend on branch types.

  2. INV - the inventory or software catalog, represents the hierarchy of products and services

notes:

  • BFG is how we use CUE to capture the organizational processes tied to software delivery
  • INV is how we use CUE to capture the organization of products, services, and metadata for the software we deliver