Search code examples
javascriptjsongruntjsgrunt-ssh

Passing variable file name to grunt.file.read


When trying to pass the users private key to grunt-ssh I want each developer to be able to setup their server keys in a Gruntsetting.json loaded in the Gruntfile like so:

    settings: grunt.file.readJSON('Gruntsettings.json'),

    sshexec: {
        test: {
            command: 'uptime',
            options: {
                privateKey: grunt.file.read('<%- settings.keyPath %>'),
                host: '<%= settings.host %>',
                username: '<%= settings.username %>'
            }
        },
    }

However when I use the keyPath setting in the grunt.file.read statement I get the error:

Error: Unable to read "<%- settings.keyPath %>" file (Error code: ENOENT).

Is it possible to pass variables from a read json file back to another read command?


Solution

  • Expand the privateKey template to include the file-read method call:

    settings: grunt.file.readJSON('Gruntsettings.json'),
    
    sshexec: {
        test: {
            command: 'uptime',
            options: {
                privateKey: '<%= grunt.file.read(settings.keyPath) %>',
                host: '<%= settings.host %>',
                username: '<%= settings.username %>'
            }
        },
    }