Prevent Rake sh command from command echo

Whenever I call sh from rake, it often executes an echo command that will be run right before it starts. How can I prevent sh from logging commands in stdout. I would like to prevent this, because I have api keys in the command that I am invoking, and I do not want to open them in my build log.

+4
source share
1 answer

There are two parts to solving this problem. The first is to pass a parameter verbose: falsethat will prevent the command from printing before it is executed:

$ cat Rakefile
SECRET = 'foobarbaz'

task :foo do
  sh "echo #{SECRET} > secrets.txt", verbose: false
end

$ rake foo
(no output)

However, this does not help if there is an error, since Rake will output an unsuccessful command if it returns an error:

$ cat Rakefile
SECRET = 'foobarbaz'

task :foo do
  sh "echo #{SECRET} > secrets.txt; exit 1", verbose: false
end

$ rake foo
rake aborted!
Command failed with status (1): [echo foobarbaz > secrets.txt; exit 1...]
...

docs sh :

, OK (true ) Process:: Status. RuntimeError , .

, Rake. , , :

$ cat Rakefile
SECRET = "foobarbaz"

task :foo do
  sh "echo #{SECRET} > secrets.txt; exit 1", verbose: false do |ok, status|
    unless ok
      fail "Command failed with status (#{status.exitstatus}): [command hidden]"
    end
  end
end

$ rake foo
rake aborted!
Command failed with status (1): [command hidden]
...

!

, ; - :

def quiet_sh(*cmd)
  options = (Hash === cmd.last) ? cmd.pop : {}
  options = { verbose: false }.merge(options)

  sh *cmd, options do |ok, status|
    unless ok
      fail "Command failed with status (#{status.exitstatus}): [command hidden]"
    end
  end
end

SECRET = "foobarbaz"

task :foo do
  quiet_sh "do_secret_things"
end
+8

All Articles