|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# files-copy |
| 4 | +# |
| 5 | +# author: opensource@tacc.cloud |
| 6 | +# |
| 7 | +# This script is part of the Agave API command line interface (CLI). |
| 8 | +# Copies a file or folder from one location to another on a remote system. |
| 9 | +# |
| 10 | + |
| 11 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 12 | + |
| 13 | +source "$DIR/common.sh" |
| 14 | + |
| 15 | +# Script logic -- TOUCH THIS {{{ |
| 16 | + |
| 17 | +# A list of all variables to prompt in interactive mode. These variables HAVE |
| 18 | +# to be named exactly as the longname option definition in usage(). |
| 19 | +interactive_opts=(apisecret apikey, destination) |
| 20 | + |
| 21 | +# Print usage |
| 22 | +usage() { |
| 23 | + echo -n "$(basename $0) [OPTION]... [PATH] |
| 24 | +
|
| 25 | +Copies a file or folder from one location to another on a remote system. If no |
| 26 | +system is specified, your default storage system will be used. By specifying a |
| 27 | +system, the path given will be resolved on that remote system. Note that the |
| 28 | +system id, not hostname must be given. |
| 29 | +
|
| 30 | + Options: |
| 31 | + -z, --access_token Access token |
| 32 | + -S, --systemId Specify the system id |
| 33 | + -D, --destination Full target path of the copied file/folder |
| 34 | + --filter Comma separated list of fields to return in the response |
| 35 | + -H, --hosturl URL of the service |
| 36 | + -d, --development Run in dev mode using default dev server |
| 37 | + -f, --force Skip all user interaction |
| 38 | + -i, --interactive Prompt for values |
| 39 | + -q, --quiet Quiet (no output) |
| 40 | + -v, --verbose Verbose output |
| 41 | + -V, --veryverbose Very verbose output |
| 42 | + -h, --help Display this help and exit |
| 43 | + --version Output version information and exit |
| 44 | +" |
| 45 | +} |
| 46 | + |
| 47 | +################################################################## |
| 48 | +################################################################## |
| 49 | +# Begin Script Logic # |
| 50 | +################################################################## |
| 51 | +################################################################## |
| 52 | + |
| 53 | +source "$DIR/files-common.sh" |
| 54 | + |
| 55 | +main() { |
| 56 | + #echo -n |
| 57 | + #set -x |
| 58 | + |
| 59 | + if [ -z "$args" ]; then |
| 60 | + err "Please specify a valid source path to copy" |
| 61 | + else |
| 62 | + if [ -n "$systemId" ]; then |
| 63 | + filesurl="${hosturl}media/system/${systemId}/${args}?pretty=true" |
| 64 | + else |
| 65 | + filesurl="${hosturl}media/${args}?pretty=true" |
| 66 | + fi |
| 67 | + |
| 68 | + cmd="curl -sk -H \"${authheader}\" -X PUT -d \"action=copy&path=${destination}\" '${filesurl}'" |
| 69 | + |
| 70 | + if ((veryverbose)); then |
| 71 | + [ "$piped" -eq 0 ] && log "Calling $cmd" |
| 72 | + fi |
| 73 | + |
| 74 | + response=`curl -sk -H "${authheader}" -X PUT -d "action=copy&path=${destination}" "${filesurl}"` |
| 75 | + |
| 76 | + if [[ $(jsonquery "$response" "status") = 'success' ]]; then |
| 77 | + result=$(format_api_json "$response") |
| 78 | + success "$result" |
| 79 | + else |
| 80 | + errorresponse=$(jsonquery "$response" "message") |
| 81 | + err "$errorresponse" |
| 82 | + fi |
| 83 | + fi |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +format_api_json() { |
| 88 | + |
| 89 | + if ((veryverbose)); then |
| 90 | + echo "$1" |
| 91 | + elif [[ $verbose -eq 1 ]]; then |
| 92 | + result=$(jsonquery "$1" "result" 1) |
| 93 | + json_prettyify "${result}" |
| 94 | + else |
| 95 | + echo "Successfully copied ${args} to ${destination}" |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +################################################################## |
| 100 | +################################################################## |
| 101 | +# End Script Logic # |
| 102 | +################################################################## |
| 103 | +################################################################## |
| 104 | + |
| 105 | +# }}} |
| 106 | + |
| 107 | +# Parse command line options |
| 108 | +source "$DIR/options.sh" |
| 109 | + |
| 110 | +# Main loop {{{ |
| 111 | + |
| 112 | +# Print help if no arguments were passed. |
| 113 | +#[[ $# -eq 0 ]] && set -- "--help" |
| 114 | + |
| 115 | +# Read the options and set stuff |
| 116 | +while [[ $1 = -?* ]]; do |
| 117 | + case $1 in |
| 118 | + -h|--help) usage >&2; safe_exit ;; |
| 119 | + --version) version; copyright; disclaimer; safe_exit ;; |
| 120 | + -z|--access_token) shift; access_token=$1 ;; |
| 121 | + -S|--systemId) shift; systemId=$1 ;; |
| 122 | + -D|--destination) shift; destination=$1 ;; |
| 123 | + --filter) shift; responsefilter=$1 ;; |
| 124 | + -H|--hosturl) shift; hosturl=$1;; |
| 125 | + -d|--development) development=1 ;; |
| 126 | + -v|--verbose) verbose=1 ;; |
| 127 | + -V|--veryverbose) veryverbose=1; verbose=1 ;; |
| 128 | + -q|--quiet) quiet=1 ;; |
| 129 | + -i|--interactive) interactive=1 ;; |
| 130 | + -f|--force) force=1 ;; |
| 131 | + --endopts) shift; break ;; |
| 132 | + *) die "invalid option: $1" ;; |
| 133 | + esac |
| 134 | + shift |
| 135 | +done |
| 136 | + |
| 137 | +# Store the remaining part as arguments. |
| 138 | +args+=("$@") |
| 139 | + |
| 140 | +# }}} |
| 141 | + |
| 142 | +# Run the script logic |
| 143 | +source "$DIR/runner.sh" |
0 commit comments