doc.dev1x.org

CUE

検証環境作成

ディレクトリ構造

$ tree -a -I .git
.
├── .gitignore
├── Dockerfile
├── README.md
├── build.sh
├── dist        # => ビルドされたファイルが保存されるディレクトリ
├── docker-compose.yaml
└── src         # => CUEファイルを保存するディレクトリ

.gitignore

dist

Dockerfile

FROM golang:1.18

WORKDIR /app

# -- Set ENV
ENV CGO_ENABLED=1 \
    GOOS=linux \
    GOARCH=amd64

# -- Install CUE CLI Tool
RUN go install cuelang.org/go/cmd/cue@latest

# -- Copy Source
COPY . /app

# -- Build
CMD ["bash", "./build.sh"]

docker-compose.yaml

version: '3.8'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    command: ["tail", "-f", "/dev/null"]
    volumes:
      - .:/app

build.sh

FILES=$(find ./src -name *.cue)

rm -rf ./dist

for FILE in ${FILES}; do
    FILE_NAME=$(basename ${FILE} .cue)
    DIR_NAME=$(dirname ${FILE})
    DIST_DIR=${DIR_NAME/src/dist}

    mkdir -p ${DIST_DIR}

    cue export ${FILE} > ${DIST_DIR}/${FILE_NAME}.json
    printf "%s => %s\n" ${FILE} ${DIST_DIR}/${FILE_NAME}.json
done

チュートリアル実行

JSONに変換

$ touch src/sample1.cue
$ cat src/sample1.cue

one: 1
two: 2

// A field using quotes.
"two-and-a-half": 2.5

list: [
    1,
    2,
    3,
]
$ bash build.sh
$ ls dist
sample1.json
$ cat dist/sample1.json
{
    "one": 1,
    "two": 2,
    "two-and-a-half": 2.5,
    "list": [
        1,
        2,
        3
    ]
}

参考資料