3bb23e8e316452c40eaa0917c8f4be1a2f59ff38

Author
Luiz <40257388+luizfelipevbll@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(transfer): allow encryption of uploads using GPG (#9983)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/transfer/README.md b/plugins/transfer/README.md
  2index 5fa0644451e06857c677adfb178e3c841f4a51e2..825c9a2833366b92296d0cabe7f2be639bea0a77 100644
  3--- a/plugins/transfer/README.md
  4+++ b/plugins/transfer/README.md
  5@@ -2,23 +2,40 @@
  6 
  7 [`transfer.sh`](https://transfer.sh) is an easy to use file sharing service from the command line
  8 
  9-## Usage
 10+To use it, add `transfer` to the plugins array in your zshrc file:
 11 
 12-Add `transfer` to your plugins array in your zshrc file:
 13 ```zsh
 14 plugins=(... transfer)
 15 ```
 16 
 17-Then you can:
 18+## Usage
 19 
 20-- transfer a file:
 21+- Transfer a file: `transfer file.txt`.
 22 
 23-```zsh
 24-transfer file.txt
 25-```
 26+- Transfer a whole directory (it will be automatically compressed): `transfer dir`.
 27 
 28-- transfer a whole directory (it will be automatically compressed):
 29+### Encryption / Decryption
 30 
 31-```zsh
 32-transfer directory/
 33-```
 34+- Encrypt and upload a file with symmetric cipher and create ASCII armored output:
 35+
 36+  ```zsh
 37+  transfer file -ca
 38+  ```
 39+
 40+- Encrypt and upload directory with symmetric cipher and gpg output:
 41+
 42+  ```zsh
 43+  transfer directory -ca
 44+  ```
 45+
 46+- Decrypt file:
 47+
 48+  ```zsh
 49+  gpg -d file -ca
 50+  ```
 51+
 52+- Decrypt directory:
 53+
 54+  ```zsh
 55+  gpg -d your_archive.tgz.gpg | tar xz
 56+  ```
 57diff --git a/plugins/transfer/transfer.plugin.zsh b/plugins/transfer/transfer.plugin.zsh
 58index db744b0cd0e5a12a11a0d42eb4785b4139528df6..b34e115ce926b3f4bfce78faa20ab79a517e42ed 100644
 59--- a/plugins/transfer/transfer.plugin.zsh
 60+++ b/plugins/transfer/transfer.plugin.zsh
 61@@ -1,69 +1,97 @@
 62-# transfer.sh Easy file sharing from the command line
 63-# transfer Plugin
 64-# Usage Example :
 65-# > transfer file.txt
 66-# > transfer directory/
 67-
 68-
 69-
 70 # Author:
 71 #   Remco Verhoef <remco@dutchcoders.io>
 72 #   https://gist.github.com/nl5887/a511f172d3fb3cd0e42d
 73 #   Modified to use tar command instead of zip
 74 #
 75 
 76-curl --version 2>&1 > /dev/null
 77-if [ $? -ne 0 ]; then
 78-  echo "Could not find curl."
 79+transfer() {
 80+  # check arguments
 81+  if [[ $# -eq 0 ]]; then
 82+  cat <<EOF
 83+Error: no arguments specified.
 84+
 85+Usage: transfer [file/folder] [options]
 86+
 87+Examples:
 88+  transfer /tmp/test.md
 89+  transfer /tmp/test.md -ca
 90+  cat /tmp/test.md | transfer test.md
 91+  cat /tmp/test.md | transfer test.md -ca
 92+
 93+Options:
 94+  -ca  Encrypt file with symmetric cipher and create ASCII armored output
 95+EOF
 96   return 1
 97-fi
 98+  fi
 99 
100-transfer() { 
101-    # check arguments
102-    if [ $# -eq 0 ]; 
103-    then 
104-        echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
105-        return 1
106+  if (( ! $+commands[curl] )); then
107+    echo "Error: curl is not installed"
108+    return 1
109+  fi
110+
111+  local tmpfile tarfile item basename
112+
113+  # get temporarily filename, output is written to this file show progress can be showed
114+  tmpfile=$(mktemp -t transferXXX)
115+
116+  # upload stdin or file
117+  item="$1"
118+
119+  # crypt file with symmetric cipher and create ASCII armored output
120+  local crypt=0
121+  if [[ "$2" = -ca ]]; then
122+    crypt=1
123+    if (( ! $+commands[gpg] )); then
124+      echo "Error: gpg is not installed"
125+      return 1
126+    fi
127+  fi
128+
129+  if ! tty -s; then
130+    # transfer from pipe
131+    if (( crypt )); then
132+      gpg -aco - | curl -X PUT --progress-bar -T - "https://transfer.sh/$item" >> $tmpfile
133+    else
134+      curl --progress-bar --upload-file - "https://transfer.sh/$item" >> $tmpfile
135     fi
136+  else
137+    basename=$(basename "$item" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
138 
139-    # get temporarily filename, output is written to this file show progress can be showed
140-    tmpfile=$( mktemp -t transferXXX )
141-    
142-    # upload stdin or file
143-    file=$1
144+    if [[ ! -e $item ]]; then
145+      echo "File $item doesn't exist."
146+      return 1
147+    fi
148 
149-    if tty -s; 
150-    then 
151-        basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g') 
152+    if [[ -d $item ]]; then
153+      # tar directory and transfer
154+      tarfile=$(mktemp -t transferXXX.tar.gz)
155+      cd $(dirname $item) || {
156+        echo "Error: Could not change to directory $(dirname $item)"
157+        return 1
158+      }
159 
160-        if [ ! -e $file ];