]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/transfer/transfer.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / transfer / transfer.plugin.zsh
1 # Author:
2 #   Remco Verhoef <remco@dutchcoders.io>
3 #   https://gist.github.com/nl5887/a511f172d3fb3cd0e42d
4 #   Modified to use tar command instead of zip
5 #
6
7 transfer() {
8   # check arguments
9   if [[ $# -eq 0 ]]; then
10   cat <<EOF
11 Error: no arguments specified.
12
13 Usage: transfer [file/folder] [options]
14
15 Examples:
16   transfer /tmp/test.md
17   transfer /tmp/test.md -ca
18   cat /tmp/test.md | transfer test.md
19   cat /tmp/test.md | transfer test.md -ca
20
21 Options:
22   -ca  Encrypt file with symmetric cipher and create ASCII armored output
23 EOF
24   return 1
25   fi
26
27   if (( ! $+commands[curl] )); then
28     echo "Error: curl is not installed"
29     return 1
30   fi
31
32   local tmpfile tarfile item basename
33
34   # get temporarily filename, output is written to this file show progress can be showed
35   tmpfile=$(mktemp -t transferXXX)
36
37   # upload stdin or file
38   item="$1"
39
40   # crypt file with symmetric cipher and create ASCII armored output
41   local crypt=0
42   if [[ "$2" = -ca ]]; then
43     crypt=1
44     if (( ! $+commands[gpg] )); then
45       echo "Error: gpg is not installed"
46       return 1
47     fi
48   fi
49
50   if ! tty -s; then
51     # transfer from pipe
52     if (( crypt )); then
53       gpg -aco - | curl -X PUT --progress-bar -T - "https://transfer.sh/$item" >> $tmpfile
54     else
55       curl --progress-bar --upload-file - "https://transfer.sh/$item" >> $tmpfile
56     fi
57   else
58     basename=$(basename "$item" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
59
60     if [[ ! -e $item ]]; then
61       echo "File $item doesn't exist."
62       return 1
63     fi
64
65     if [[ -d $item ]]; then
66       # tar directory and transfer
67       tarfile=$(mktemp -t transferXXX.tar.gz)
68       cd $(dirname $item) || {
69         echo "Error: Could not change to directory $(dirname $item)"
70         return 1
71       }
72
73       tar -czf $tarfile $(basename $item)
74       if (( crypt )); then
75         gpg -cao - "$tarfile" | curl --progress-bar -T "-" "https://transfer.sh/$basename.tar.gz.gpg" >> $tmpfile
76       else
77         curl --progress-bar --upload-file "$tarfile" "https://transfer.sh/$basename.tar.gz" >> $tmpfile
78       fi
79       rm -f $tarfile
80     else
81       # transfer file
82       if (( crypt )); then
83         gpg -cao - "$item" | curl --progress-bar -T "-" "https://transfer.sh/$basename.gpg" >> $tmpfile
84       else
85         curl --progress-bar --upload-file "$item" "https://transfer.sh/$basename" >> $tmpfile
86       fi
87     fi
88   fi
89
90   # cat output link
91   cat $tmpfile
92   # add newline
93   echo
94
95   # cleanup
96   rm -f $tmpfile
97 }