Skip to content

Commit

Permalink
Improve CLI (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineGagne authored Aug 27, 2022
1 parent 459de39 commit 4aa1dd8
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 45 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.stack-work
137 changes: 137 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
name: Release
on:
push:
tags:
- "*.*.*"

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v2
- name: Run tests
run: stack test

build-linux:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Compile binary
run: stack --local-bin-path=build/bin install

- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: parthenon-Linux-x86-64
path: build/bin/parthenon
retention-days: 3

build-macos:
needs: test
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Compile binary
run: stack --local-bin-path=build/bin install

- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: parthenon-Darwin-x86_64
path: build/bin/parthenon
retention-days: 3

build-windows:
needs: test
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: Compile binary
run: stack --local-bin-path=build/bin install

- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: parthenon-Windows-x86_64.exe
path: build/bin/parthenon.exe
retention-days: 3

docker:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

- name: Push Docker Image Scratch
uses: docker/build-push-action@v3
with:
push: true
tags: |
parthenon/parthenon:latest
parthenon/parthenon:${{github.ref}}
parthenon/parthenon:${{github.sha}}
create-release:
needs: [build-linux, build-macos, build-windows]
runs-on: ubuntu-latest
steps:
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
draft: false
prerelease: false

- name: Download Artifacts
uses: actions/download-artifact@v2
with:
path: assets/

- name: Display structure of downloaded files
run: ls -lR
working-directory: assets/

- name: Upload Release Assets
id: upload_release_assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./assets/parthenon-Linux-x86_64/parthenon
asset_name: parthenon-Linux-x86_64
asset_content_type: application/octet-stream

- uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./assets/parthenon-Darwin-x86_64/parthenon
asset_name: parthenon-Darwin-x86_64
asset_content_type: application/octet-stream

- uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./assets/parthenon-Windows-x86_64/parthenon
asset_name: parthenon-Windows-x86_64
asset_content_type: application/octet-stream
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM haskell:9-slim as builder
WORKDIR /tmp/parthenon
RUN cabal update

COPY parthenon.cabal /tmp/parthenon/
RUN cabal build --only-dependencies -j4

COPY . /tmp/parthenon/
RUN cabal install

FROM scratch
LABEL maintainer="Antoine Gagné <gagnantoine@gmail.com>"
COPY --from=builder /root/.cabal/bin/parthenon /usr/bin/
ENTRYPOINT ["parthenon"]
47 changes: 40 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

## How to get

### Build from source
### Building

#### Build from source

After cloning this repository, the following command can be used to build the
binary:
Expand All @@ -15,16 +17,47 @@ binary:
stack build
```

#### Build the Docker image

```sh
docker build . -t parthenon:latest
```

## Usage

### With the binary

```sh
# Create a schema and put it inside the schemas folder (this folder location
# varies depending on the platform)
$ cat << EOF > ~/.local/share/parthenon/vectors
# Create a schema
cat << EOF > vectors
array<struct<x: double, y: double, z: double>>
EOF

# Use the `vectors` schema
$ parthenon vectors '[{x=1.0, y=2.0, z=3.0}, {x=4.0, y=5.1, z=6.2}]'
[{"x":1,"y":2,"z":3},{"x":4,"y":5.1,"z":6.2}]
cat << EOF > athena-vectors
[{x=1.0, y=1.0, z=1.0}]
EOF

echo '[{x=1.0, y=1.0, z=1.0}]' | parthenon @vectors -
# [{"x":1,"y":1,"z":1}]
echo '[{x=1.0, y=1.0, z=1.0}]' | parthenon 'array<struct<x: double, y: double, z: double>>' -
# [{"x":1,"y":1,"z":1}]
parthenon 'array<struct<x: double, y: double, z: double>>' '[{x=1.0, y=1.0, z=1.0}]'
# [{"x":1,"y":1,"z":1}]
parthenon 'array<struct<x: double, y: double, z: double>>' @athena-vectors
# [{"x":1,"y":1,"z":1}]
```

### With the Docker image

Using the Docker image is similar to the commands above:

```sh
docker run --rm -i parthenon:latest 'array<struct<x: double, y: double, z: double>>' '[{x=1.0, y=1.0, z=1.0}]'
echo '[{x=1.0, y=1.0, z=1.0}]' | docker run --rm -i parthenon:latest 'array<struct<x: double, y: double, z: double>>'
```

To make it easier to invoke the image, the following alias can be used:

```sh
alias parthenon='docker run --rm -i parthenon:latest'
```
39 changes: 23 additions & 16 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import Data.Aeson (encode)
import qualified Data.ByteString.Lazy as ByteString
import Data.Text (Text)
import qualified Data.Text.IO as TextIO
import Options.Applicative (execParser)
import Parthenon (Options (..))
import Options.Applicative (customExecParser)
import Parthenon
( FileArgument (..),
InputArgument (..),
Options (..),
)
import qualified Parthenon
import System.Directory (XdgDirectory (XdgData), createDirectoryIfMissing, getXdgDirectory)
import System.FilePath ((</>))
import Text.Megaparsec

main :: IO ()
main = do
defaultDataDirectory <- getXdgDirectory XdgData ""
let defaultDirectory = defaultDataDirectory </> "parthenon"
createDirectoryIfMissing True defaultDirectory
invokedWith <- execParser (Parthenon.options defaultDirectory)
let schemaDirectory = oSchemaDirectory invokedWith
schema = schemaDirectory </> oSchema invokedWith
invokedWith <- customExecParser Parthenon.preferences Parthenon.options
let schema = oSchema invokedWith
input = oInput invokedWith
rawSchema <- TextIO.readFile schema
rawAthena <- getAthena input
rawSchema <- fromFileArgument schema
rawAthena <- fromInputArgument input
case decode rawSchema rawAthena of
Right parsed ->
ByteString.putStr $ encode parsed
Expand All @@ -32,9 +30,18 @@ main = do
parser <- runParser Parthenon.schema "schema" rawSchema
runParser parser "athena" rawAthena

getAthena :: Text -> IO Text
getAthena raw = case raw of
"-" ->
fromFileArgument :: FileArgument -> IO Text
fromFileArgument raw = case raw of
FFromFile filepath ->
TextIO.readFile filepath
FFromArgument other ->
pure other

fromInputArgument :: InputArgument -> IO Text
fromInputArgument raw = case raw of
IFromStdin ->
TextIO.getContents
other ->
IFromFile filepath ->
TextIO.readFile filepath
IFromArgument other ->
pure other
3 changes: 3 additions & 0 deletions src/Parthenon.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Parthenon
( Schema.schema,
Types.Athena (..),
Cli.Options (..),
Cli.FileArgument (..),
Cli.InputArgument (..),
Cli.preferences,
Cli.options,
)
where
Expand Down
Loading

0 comments on commit 4aa1dd8

Please sign in to comment.