Skip to content

Commit

Permalink
Convert to mise plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbode committed Mar 25, 2024
1 parent 5c7a2fb commit 88826aa
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style = space
indent_size = 2
65 changes: 65 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Main workflow

on:
pull_request:
push:
schedule:
- cron: 0 0 * * 5

jobs:
test:
strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-latest

runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@v3
- uses: jdx/mise-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
cache: false
- run: mkdir -p ~/.local/share/mise/plugins
- run: ln -s $PWD ~/.local/share/mise/plugins/sops
- run: mise i sops@latest
- run: mise x sops@latest -- sops --version

lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: Install ShellCheck
run: brew install shellcheck

- name: Run ShellCheck
run: make lint

format:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: Install shfmt
run: brew update && brew install shfmt

- name: Run shfmt
run: make format-check
1 change: 1 addition & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
external-sources=true
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SH_SRCFILES = $(shell git ls-files "bin/*")
SHFMT_BASE_FLAGS = -i 2 -ci

fmt:
shfmt -w $(SHFMT_BASE_FLAGS) $(SH_SRCFILES)
.PHONY: fmt

format-check:
shfmt -d $(SHFMT_BASE_FLAGS) $(SH_SRCFILES)
.PHONY: format-check

lint:
shellcheck $(SH_SRCFILES)
.PHONY: lint
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# asdf-sops
[![Build Status](https://travis-ci.org/feniix/asdf-sops.svg?branch=master)](https://travis-ci.org/feniix/asdf-sops)
# mise-sops

[Sops](https://github.com/mozilla/sops) plugin for the [asdf](https://github.com/asdf-vm/asdf) version manager.
[Sops](https://github.com/mozilla/sops) plugin for the
[Mise](https://github.com/jdx/mise) version manager.

## Install

```
asdf plugin-add sops https://github.com/feniix/asdf-sops.git
sops plugin-add sops https://github.com/joshbode/mise-sops.git
```

## Use
## Environment Activation

Check out the [asdf](https://github.com/asdf-vm/asdf) readme for instructions.
Automatically activate the SOPS environment when entering the directory with the
following `.mise.toml`:

```toml
[tools]
sops = { version = "latest", filename = ".sops.env" }
```

## License

Licensed under the
[MIT license](https://github.com/joshbode/mise-sops/blob/main/LICENSE).
9 changes: 9 additions & 0 deletions bin/exec-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/env bash

set -euo pipefail

# shellcheck source-path=.
source "${MISE_PLUGIN_PATH}/bin/utils.sh"

# shellcheck disable=SC1090
source <(sops_env)
19 changes: 10 additions & 9 deletions bin/install
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#! /usr/bin/env bash

set -euo pipefail

Expand All @@ -22,7 +22,8 @@ install_sops() {
local bin_install_path="$install_path/bin"
local bin_path="${bin_install_path}/sops"

local download_url="$(get_download_url $version)"
local download_url
download_url=$(get_download_url "$version")

mkdir -p "${bin_install_path}"

Expand All @@ -40,27 +41,27 @@ get_cpu() {
machine_hardware_name=${ASDF_SOPS_OVERWRITE_ARCH:-"$(uname -m)"}

case "$machine_hardware_name" in
'x86_64') local cpu_type="amd64";;
'aarch64') local cpu_type="arm64";;
*) local cpu_type="$machine_hardware_name";;
'x86_64') local cpu_type="amd64" ;;
'aarch64') local cpu_type="arm64" ;;
*) local cpu_type="$machine_hardware_name" ;;
esac

echo "$cpu_type"
}

get_download_url() {
local version="$1"
local platform="$(get_arch)"
local platform
platform=$(get_arch)
local url=""

if [[ "$version" < "v3.7.2" ]]; then
url="https://github.com/getsops/sops/releases/download/${version}/sops-${version}.${platform}"
else
url="https://github.com/getsops/sops/releases/download/${version}/sops-${version}.${platform}.$(get_cpu)"
fi

echo "$url"
}

install_sops "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"

25 changes: 14 additions & 11 deletions bin/list-all
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#!/usr/bin/env bash
#! /usr/bin/env bash

releases_path=https://api.github.com/repos/getsops/sops/releases
cmd="curl -s"
if [ -n "$GITHUB_API_TOKEN" ]; then
cmd="$cmd -H 'Authorization: token $GITHUB_API_TOKEN'"
fi
set -euo pipefail

RELEASES_PATH=https://api.github.com/repos/getsops/sops/releases

cmd="$cmd $releases_path"
if [[ -n ${GITHUB_API_TOKEN-} ]]; then
set -- "$@" -H "Authorization: token ${GITHUB_API_TOKEN}"
fi

function sort_versions() {
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | \
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' |
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
}

# stripping off the 'v' prefix as it causes issues with 'asdf install foo latest'
echo $(eval "$cmd" | grep -oE "tag_name\": \".{1,15}\"," | grep -v "2.0.10" | sed 's/tag_name": "//; s/^v//; s/",//' | sort_versions)

# stripping off the 'v' prefix as it causes issues with 'mise install foo latest'
curl -qsSL "$@" "${RELEASES_PATH}" |
grep -oE "tag_name\": \".{1,15}\"," |
grep -v "2.0.10" |
sed 's/tag_name": "//; s/^v//; s/",//' |
sort_versions
30 changes: 30 additions & 0 deletions bin/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#! /usr/bin/env bash

if [ "${MISE_TRACE-}" = "1" ]; then
set -x
fi

echoerr() {
echo "$1" >&2
}

sops_bin() {
echo "$MISE_INSTALL_PATH/bin/sops"
}

sops_env() {
local filename
filename=$(eval "printf '%s' '${MISE_TOOL_OPTS__FILENAME-}'")
if [[ -z ${filename} ]]; then
echoerr "mise-sops: No filename provided."
return 1
fi
if [[ ${filename} != /* ]] && [[ -n ${MISE_PROJECT_ROOT-} ]]; then
filename="${MISE_PROJECT_ROOT-}/${filename}"
fi
if [[ ! -f ${filename} ]]; then
echoerr "mise-sops: Filename '${filename}' not found."
return 1
fi
"$(sops_bin)" -d "${filename}"
}
27 changes: 27 additions & 0 deletions mise.plugin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[exec-env]
cache-key = [
"""
{% if project_root %}
{{ project_root | canonicalize }}
{% endif %}
""",
"""
{% if opts.filename %}
{{ opts.filename }}
{% endif %}
"""
]

[list-bin-paths]
cache-key = [
"""
{% if project_root %}
{{ project_root | canonicalize }}
{% endif %}
""",
"""
{% if opts.filename %}
{{ opts.filename }}
{% endif %}
"""
]

0 comments on commit 88826aa

Please sign in to comment.