mecano
Version:
Common functions for system deployment.
74 lines (55 loc) • 2.17 kB
Markdown
Change the permissions of a file or directory.
* `destination`
Where the file or directory is copied.
* `mode`
Permissions of the file or the parent directory.
* `log`
Function called with a log related messages.
* `ssh` (object|ssh2)
Run the action on a remote server using SSH, an ssh2 instance or an
configuration object used to initialize the SSH connection.
* `stat` (Stat instance, optional)
Pass the Stat object relative to the destination file or directory, to be
used as an optimization.
* `err`
Error object if any.
* `modified`
Number of permissions with modifications.
```js
require('mecano').chmod({
destination: '~/my/project',
mode: 0o755
}, function(err, modified){
console.log(err ? err.message : 'File was modified: ' + modified);
});
```
module.exports = (options, callback) ->
return callback Error "Missing destination: #{JSON.stringify options.destination}" unless options.destination
return callback Error "Missing option 'mode'" unless options.mode
do_stat = ->
return do_chmod options.stat if options.stat
options.log? "Mecano `chmod`: stat \"#{options.destination}\" [DEBUG]"
fs.stat options.ssh, options.destination, (err, stat) ->
return callback err if err
do_chmod stat
do_chmod = (stat) ->
if misc.mode.compare stat.mode, options.mode
options.log? "Mecano `chmod`: identical permissions on '#{options.destination}' [INFO]"
return callback()
fs.chmod options.ssh, options.destination, options.mode, (err) ->
options.log? "Mecano `chmod`: change permissions from '#{stat.mode}' to '#{options.mode}' on '#{options.destination}' [WARN]" unless err
callback err, true
do_stat()
## Dependencies
fs = require 'ssh2-fs'
misc = require './misc'