rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.11k stars 552 forks source link

String option is incorrectly passed #875

Closed h0jeZvgoxFepBQ2C closed 4 months ago

h0jeZvgoxFepBQ2C commented 4 months ago

F.e.:

  desc "psql_cmd", "runs a psql command on the db"
  option :ns, type: :string, required: false
  option :cmd, type: :string, required: true

  def psql_cmd
....

bundle exec thor db:psql_cmd --cmd "update accounts set encrypted_password = '$2a$10$VasdqcycaeasdasQy/utest123123D.xx2.x2.asdCDSAD.C' where email = 'test@test.at'"

leads to following incorrect password ('a/utest123123D..1cb.H5.Ru4BQJjL0J.C' where vs '$2a$10$VasdqcycaeasdasQy/utest123123D.xx2.x2.asdCDSAD.C'):

[1] pry(#<Db>)> options
=> {"cmd"=>"update accounts set encrypted_password = 'a/utest123123D..1cb.H5.Ru4BQJjL0J.C' where email = 'test@test.at'"}

WTF :D

Any idea on how to solve this? Using zsh

h0jeZvgoxFepBQ2C commented 4 months ago

Chat GPT ftw... 💪

The behavior you're encountering when running the Thor command with zsh (or any other shell) is due to how shells interpret certain characters, notably the dollar sign ($). The dollar sign is used in shells to signify a variable. So, when you include $2a$10$VasdqcycaeasdasQy/utest123123D.xx2.x2.asdCDSAD.C in your command, the shell tries to interpret $2a, $10, $VasdqcycaeasdasQy, and so on as variables. Since these variables are not defined, they are replaced with empty strings, leading to the observed distortion in the command string passed to your Thor task. Solution

To prevent the shell from interpreting these characters, you need to escape them or use single quotes around the entire command. However, since you also have single quotes in your command, you'll need to handle those carefully.