SSHKit/Thor Variable Access

147 views
Skip to first unread message

Jay Mitchell

unread,
May 15, 2014, 7:43:51 PM5/15/14
to capis...@googlegroups.com
I would like to use SSHKit in a Thor script. The problem is that I don't have access to the options variable once I use the SSHKit DSL methods. Has anybody used SSHKit with Thor and been able to access options? / Are there known scoping workarounds with SSHKit?

Code:

# Echo.thor
require 'sshkit'
require 'sshkit/dsl'

class Echo < Thor

  HOST = SSHKit::Host.new('some_user@some_box')

  desc 'echo', 'Simple example'
  method_option :echo, desc: 'Thing to echo.'
  def echo
    # options variable is available here...
    puts "options[:echo]: #{options[:echo]}"
    on HOST do
      # ...but not here.
      execute :echo, options[:echo]
    end
  end
end

Result:

>thor echo:echo --echo Timmy
options[:echo]: Timmy
D:/Code/link/scripts/phalanx/echo.thor:15:in `block in echo': undefined local variable or method `options' for #<SSHKit::Backend::Netssh:0x2e4ef20> (NameError)
        from C:/Ruby200/lib/ruby/gems/2.0.0/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
        from C:/Ruby200/lib/ruby/gems/2.0.0/gems/sshkit-1.4.0/lib/sshkit/backends/netssh.rb:54:in `run'
        from C:/Ruby200/lib/ruby/gems/2.0.0/gems/sshkit-1.4.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'

Versions:
  • Ruby 2.0.0p353 (Windows 8) and 2.1.1p76 (Mac Mavericks with RbEnv)
  • Capistrano N/A
  • SSHKit 1.4.0
  • Thor 0.19.1 (Windows) and 0.14.6 (Mac)
Platform:
  • Tried both Windows 8 and Mac Mavericks.
  • Target machine is Ubuntu, but shouldn't be a factor.
echo.thor

Lee Hambley

unread,
May 16, 2014, 3:19:39 AM5/16/14
to capistrano
 The problem is that I don't have access to the options variable once I use the SSHKit DSL methods. Has anybody used SSHKit with Thor and been able to access options?

Sorry, it's not possible. As you mentioned it's a scoping problem, everything in SSHKit is instance-evaluated in a the context of the SSHKit::Coordinator, and/or an SSHKit commend instance.

You may have some luck injecting a module into the SSHKit instance, but honestly I wouldn't bother. Capistrano 2 suffered from DSL overload, where faux convenience DSLs replaced equally useful, and more sane pure Ruby, Thor suffers the same way. 

If you want command line integration, use something written with sympathy for the language https://github.com/davetron5000/gli, or Rake.

Might I ask, why are you using Thor. We spent a lot of time evaluating different task DSLs, Thor was eliminated first, as the following returns with exit status 0, which is plainly broken: (this is the smallest problem with Thor.) We chose Rake because it's stable, installed everywhere, and sane.

class Echo < Thor
  def echo
    raise RuntimeError
  end
end
> thor echo:echo

--
You received this message because you are subscribed to the Google Groups "Capistrano" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capistrano+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/capistrano/f692a609-fc7a-44e5-a58f-91e085b19c3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jay Mitchell

unread,
May 16, 2014, 12:38:14 PM5/16/14
to capis...@googlegroups.com
Thanks Lee.

If I drop down below the DSL level, to just require SSHKit, is that an easy API to work with, and is it documented somewhere? When I search for SSHKit, the examples always use the DSL. (If it's not documented, I can drop into the DSL code to see how it's using SSHKit easily enough.)

I did notice that I didn't have these scoping issues using the DSL from a raw ruby script, so I'll look into a Thor alternative, like you suggested.

As far as choosing Thor, I've written a handful of scripts over the years, and have used ARGV, Rake, OptionParser, and Thor for scripts with arguments. Of those, Thor seems to give the most robust CLI for the least amount of code. The exit code issue would be a nonstarter for many cases, but something I'm not too worried about with this script. I also don't like using a tool via inheritance like Thor requires. However, there are pros and cons to all of the solutions I've tried, so I accept the trade offs.

Rake is a fantastic option, particularly when there are task dependencies. I prefer the Thor CLI interface to the one provided by Rake. My mind doesn't map well to the task argument square bracket syntax of Rake. Also, I prefer flag arguments to positional arguments. I'm not a Rake expert, if it provides flag arguments, I'd gladly stand corrected.

Lee Hambley

unread,
May 23, 2014, 6:00:56 AM5/23/14
to capistrano
I'm not a Rake expert, if it provides flag arguments, I'd gladly stand corrected.

Waking up an old thread… It' might be overkill for some of what you are doing, but it's rather easy to subclass Rake, and run that: 
There's also a decent test suite application_spec.rb which shows how we tested that, too.... it was neater than one might have expected. It's a shame Jim died before really people understood that Rake was an amazing, amazing piece of well designed, extensible and reliable software.

Reply all
Reply to author
Forward
0 new messages