Skip to content

Commit

Permalink
wip:
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao authored and oneingan committed Jul 27, 2024
1 parent 58e6300 commit d14933d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
32 changes: 32 additions & 0 deletions src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
_: cmd: script: ''
# GitHub case
if [[ -v CI ]] && [[ -v BRANCH ]] && [[ -v OWNER_AND_REPO ]] && command gh > /dev/null ; then
set +e # diff exits 1 if diff existed
read -r -d "" DIFFSTREAM <<DIFF
## DiffPost
This PR would generate the following \`${cmd}\` diff:
<details><summary>Preview</summary>
\`\`\`diff
$(${script})
\`\`\`
</details>
DIFF
set -e # we're past the invocation of diff
if ! gh pr --repo "$OWNER_AND_REPO" comment "$BRANCH" --edit-last -b "$DIFFSTREAM"; then
echo "Make a first post ..."
gh pr --repo "$OWNER_AND_REPO" comment "$BRANCH" -b "$DIFFSTREAM"
fi
exit 0
fi
''
33 changes: 5 additions & 28 deletions src/std/fwlib/blockTypes/kubectl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Available actions:
*/
let
inherit (root) mkCommand;
inherit (super) addSelectorFunctor askUserToProceedSnippet;
inherit (super) addSelectorFunctor askUserToProceedSnippet postDiffToGitHubSnippet;
in
name: {
__functor = addSelectorFunctor;
Expand Down Expand Up @@ -137,34 +137,11 @@ in
} "$manifest_path/";
}
# GitHub case
if [[ -v CI ]] && [[ -v BRANCH ]] && [[ -v OWNER_AND_REPO ]] && command gh > /dev/null ; then
${postDiffToGitHubSnippet "kubectl" "diff"}
set +e # diff exits 1 if diff existed
read -r -d "" DIFFSTREAM <<DIFF
## Standard DiffPost
This PR would generate the following \`kubectl\` diff:
<details><summary>Preview</summary>
\`\`\`diff
$(diff)
\`\`\`
</details>
DIFF
set -e # we're past the invocation of diff
if ! gh pr --repo "$OWNER_AND_REPO" comment "$BRANCH" --edit-last -b "$DIFFSTREAM"; then
echo "Make a first post ..."
gh pr --repo "$OWNER_AND_REPO" comment "$BRANCH" -b "$DIFFSTREAM"
fi
else
KUBECTL_EXTERNAL_DIFF="icdiff -N -r"
export KUBECTL_EXTERNAL_DIFF
diff
fi
KUBECTL_EXTERNAL_DIFF="icdiff -N -r"
export KUBECTL_EXTERNAL_DIFF
diff
'' {})
(mkCommand currentSystem "apply" "Apply the manifests to K8s" [pkgs.kubectl pkgs.icdiff] ''
${build}
Expand Down
37 changes: 26 additions & 11 deletions src/std/fwlib/blockTypes/terra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Available actions:
*/
let
inherit (root) mkCommand;
inherit (super) addSelectorFunctor;
inherit (super) addSelectorFunctor postDiffToGitHubSnippet;
in
name: repo: {
inherit name;
__functor = self: selectors: self // selectors;
__functor = addSelectorFunctor;
type = "terra";
actions = {
currentSystem,
Expand Down Expand Up @@ -77,15 +77,30 @@ in
jq '.' ${terraformConfiguration} > "$dir/config.tf.json"
'';

wrap = cmd: ''
${setup}
terraform-backend-git git \
--dir "$dir" \
--repository ${git.repo} \
--ref ${git.ref} \
--state ${git.state} \
terraform ${cmd} "$@";
'';
wrap = cmd:
setup
+ (
(pkgs.lib.optionalString cmd == "plan") (
postDiffToGitHubSnippet cmd ''
terraform-backend-git git \
--dir "$dir" \
--repository ${git.repo} \
--ref ${git.ref} \
--state ${git.state} \
terraform plan \
-lock=false \
-no-color
''
)
)
+ ''
terraform-backend-git git \
--dir "$dir" \
--repository ${git.repo} \
--ref ${git.ref} \
--state ${git.state} \
terraform ${cmd} "$@";
'';
in [
(mkCommand currentSystem "init" "tf init" [pkgs.jq pkgs.terraform pkgs.terraform-backend-git] (wrap "init") {})
(mkCommand currentSystem "plan" "tf plan" [pkgs.jq pkgs.terraform pkgs.terraform-backend-git] (wrap "plan") {})
Expand Down

0 comments on commit d14933d

Please sign in to comment.