Skip to content

Commit

Permalink
Merge pull request #57 from blp1526/aws-cli
Browse files Browse the repository at this point in the history
Add aws-cli
  • Loading branch information
blp1526 authored Dec 15, 2020
2 parents 5600917 + 73118f7 commit 978c164
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
# storage.vim

<img alt="storage.vim" src="https://user-images.githubusercontent.com/1040576/102196982-eb4bd780-3f03-11eb-91b9-2e16ad045ed0.jpg" width="640px">

## Usage

```markdown
$ vim s3://BUCKET/OBJECT
$ vim s3://BUCKET/OBJECT/
```

This executes `s3cmd get s3://BUCKET/OBJECT tempfile`, and shows the result as a new buffer.<br>
If you `:w[rite]`, then `s3cmd put tempfile s3://BUCKET/OBJECT` is executed.
This executes `s3cmd ls --recursive s3://BUCKET/OBJECT/` or `aws s3 ls --recursive s3://BUCKET/OBJECT/`, and shows the result as a quickfix window.

```markdown
$ vim s3://BUCKET/OBJECT/
$ vim s3://BUCKET/OBJECT
```

This executes `s3cmd ls s3://BUCKET/OBJECT/`, and shows the result as a quickfix window.

## Screenshot

![s3cmd](https://cloud.githubusercontent.com/assets/1040576/20217208/1544c4b2-a862-11e6-90c6-91d4c3629c0e.png)
This executes `s3cmd get --force s3://BUCKET/OBJECT tempfile` or `aws s3 cp s3://BUCKET/OBJECT tempfile`, and shows the result as a new buffer.<br>
If you `:w[rite]`, then `s3cmd put tempfile s3://BUCKET/OBJECT` or `aws s3 cp tempfile s3://BUCKET/OBJECT` is executed.

## Requirement

The [s3cmd](https://github.com/s3tools/s3cmd) cli tool, or a same `get --force`
(overwrites the local file if it already exists), `put` and `ls` interfafce cli
tool.
[s3cmd](https://github.com/s3tools/s3cmd) or [aws-cli](https://github.com/aws/aws-cli)

## Option

If you have your favorite cli tool which provides the same interface as s3cmd, then you can add below line to your .vimrc.

```markdown
let g:storage_vim_cmd = 'command_name'
# default 'command_name' is 's3cmd'
let g:storage_vim_cmd = 'aws s3'
```

Default g:storage_vim_cmd is s3cmd.

## Installation

Use your favorite plugin manager, or
Expand Down
50 changes: 35 additions & 15 deletions autoload/storage.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function! storage#read(cmd, path, dict) abort
else
setlocal nomodified
let ls_result = storage#ls_cmd(a:cmd, a:path)
call storage#open_quickfix(ls_result)
call storage#open_quickfix(ls_result, a:path)
endif
endfunction

Expand All @@ -41,14 +41,19 @@ function! storage#write(cmd, dict, path) abort
let &hidden = current_hidden
endfunction

function! storage#open_quickfix(ls_result) abort
function! storage#open_quickfix(ls_result, path) abort
let current_errorformat = &errorformat
let &errorformat = storage#errorformat()
let ls_result_array = split(a:ls_result, "\n")
call map(ls_result_array, 'storage#errorformatted_string(v:val)')
let storage_vim_ls_list = join(ls_result_array, "\n")
" FIXME: more beautify
cgetexpr storage_vim_ls_list
let file_array = []
for index in range(len(ls_result_array))
if storage#last_string(ls_result_array[index]) != "/"
call add(file_array, ls_result_array[index])
endif
endfor
call map(file_array, 'storage#errorformatted_string(v:val, ' . "'" . a:path . "'" .')')
let storage_vim_ls = join(file_array, "\n")
cgetexpr storage_vim_ls
copen
try
" NOTE:
Expand Down Expand Up @@ -78,12 +83,16 @@ function! storage#errorformat() abort
return '%f(%l\,%c):%m'
endfunction

function! storage#errorformatted_string(val) abort
function! storage#errorformatted_string(val, path) abort
let array = split(a:val)
let file = array[(len(array) - 1)]
let filepath = array[3]
let line_column = '(1,1):'
let message = array[(len(array) - 2)]
return (file . line_column . message)
let message = array[0] . ' ' . array[1] . ' ' .array[2]
if g:storage_vim_cmd == 'aws s3'
let backet = 's3://' . split(a:path, '/')[2] . '/'
let filepath = backet . filepath
endif
return (filepath . line_column . message)
endfunction

function! storage#current_line_string() abort
Expand All @@ -95,23 +104,34 @@ function! storage#cmd_script(...) abort
endfunction

function! storage#get_cmd(cmd, bucket, file) abort
let script = storage#cmd_script(a:cmd, 'get --force', a:bucket, a:file)
return storage#run_cmd(script)
if g:storage_vim_cmd == 'aws s3'
let script = storage#cmd_script(a:cmd, 'cp', a:bucket, a:file)
else
let script = storage#cmd_script(a:cmd, 'get --force', a:bucket, a:file)
endif
let result = system(script)
if v:shell_error != 0
echo trim(result)
endif
endfunction

function! storage#put_cmd(cmd, file, bucket) abort
let script = storage#cmd_script(a:cmd, 'put', a:file, a:bucket)
if g:storage_vim_cmd == 'aws s3'
let script = storage#cmd_script(a:cmd, 'cp', a:file, a:bucket)
else
let script = storage#cmd_script(a:cmd, 'put', a:file, a:bucket)
endif
call storage#run_cmd(script)
return '"' . a:bucket . '" ' . 'uploaded'
endfunction

function! storage#ls_cmd(cmd, bucket) abort
let script = storage#cmd_script(a:cmd, 'ls', a:bucket)
let script = storage#cmd_script(a:cmd, 'ls --recursive', a:bucket)
return storage#run_cmd(script)
endfunction

function! storage#run_cmd(script) abort
let result = system(a:script)
let result = system(a:script)
if v:shell_error == 0
return result
else
Expand Down
7 changes: 4 additions & 3 deletions spec/storage_spec.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source $PWD/autoload/storage.vim
source $PWD/plugin/storage.vim

let v:errors = []

Expand Down Expand Up @@ -82,9 +83,9 @@ function! Spec_storage_errorformat() abort
endfunction

function! Spec_storage_errorformatted_string() abort
echo 'storage#errorformatted_string(val)'
let expected = 's3://foo/bar.md(1,1):12324'
let actual = storage#errorformatted_string('2016-10-31 00:00 12324 s3://foo/bar.md')
echo 'storage#errorformatted_string(val, path)'
let expected = 's3://foo/bar.md(1,1):2016-10-31 00:00 12324'
let actual = storage#errorformatted_string('2016-10-31 00:00 12324 s3://foo/bar.md', '')
echo repeat(' ', 2).'should return storage#errorformat() style string'
call assert_equal(expected, actual)
echo "\n"
Expand Down

0 comments on commit 978c164

Please sign in to comment.