Class: SoracomCli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/soracom/cli.rb

Overview

Using Thor for CLI Implementation

Instance Method Summary collapse

Instance Method Details

#authObject



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/soracom/cli.rb', line 582

def auth
  puts "testing authentication... #{options.profile}"
  begin
    client = Soracom::Client.new(profile:options.profile)
    puts <<EOS
authentication succeeded.
apiKey: #{client.api_key}
operatorId: #{client.operator_id}
userName: #{client.user_name}
token: #{client.token}
EOS
  rescue => evar
    abort 'ERROR: ' + evar.to_s
  end
end

#completeObject



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/soracom/cli.rb', line 655

def complete
  commands = CLI.commands.select { |k, _v| k != 'complete' }.map { |k, _v| k }.join(' ')
  subcommands = {}
  CLI.subcommand_classes.each { |k, v| subcommands[k] = v.commands.map { |k2, _v2| k2 } }
  subcommands_string = subcommands.map do |k, v|
    <<EOS
  [ "$3" = "#{k}" ] && COMPREPLY=( $( compgen -W "#{v.join(' ')}" ${COMP_WORDS[COMP_CWORD]} ) )
EOS
  end.join
  print <<EOS
_soracom()
{
  [ "$3" = "soracom" ] && COMPREPLY=( $( compgen -W "#{commands}" ${COMP_WORDS[COMP_CWORD]} ) )
#{subcommands_string}
}
complete -F _soracom soracom
EOS
end

#configureObject



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/soracom/cli.rb', line 599

def configure
  print <<EOS
--- SORACOM CLI setup ---
This will create .soracom directory under #{ENV['HOME']} and place '#{options.profile}.json' in it.

Please select which authentication method to use.

1. Input AuthKeyId and AuthKey *Recommended*
2. Input Operator credentials (Operator Email and Password)
3. Input SAM credentials (OperatorId and UserName and Password)

EOS
  mode = STDIN.tap{print "select(1-3)> "}.gets.chomp
  begin
    Dir.mkdir("#{ENV['HOME']}/.soracom/",0700)
  rescue Errno::EEXIST
  end
  case mode
  when '1'
    authKeyId = STDIN.tap{print "authKeyId: "}.gets.chomp
    authKey = STDIN.tap{print "authKey: "}.noecho(&:gets).tap{print "\n"}.chomp
    File.open("#{ENV['HOME']}/.soracom/#{options.profile}.json", "w") do |f|
      f.print JSON.pretty_generate ({authKeyId: authKeyId, authKey: authKey})
    end
  when '2'
    email = STDIN.tap{print "Email: "}.gets.chomp
    password = STDIN.tap{print "Password: "}.noecho(&:gets).tap{print "\n"}.chomp
    File.open("#{ENV['HOME']}/.soracom/#{options.profile}.json", "w") do |f|
      f.print JSON.pretty_generate ({email: email, password: password})
    end
  when '3'
    operatorId = STDIN.tap{print "operatorId: "}.gets.chomp
    userName = STDIN.tap{print "userName: "}.gets.chomp
    password = STDIN.tap{print "password: "}.noecho(&:gets).tap{print "\n"}.chomp
    File.open("#{ENV['HOME']}/.soracom/#{options.profile}.json", "w") do |f|
      f.print JSON.pretty_generate ({operatorId: operatorId, userName: userName, password: password})
    end
  else
    abort "invalid number."
  end
  print "wrote to #{ENV['HOME']}/.soracom/#{options.profile}.json\n"
end

#supportObject



643
644
645
646
647
# File 'lib/soracom/cli.rb', line 643

def support
  client = Soracom::Client.new(profile:options.profile)
  url = client.get_support_url
  system "open '#{url}' &> /dev/null || ( echo open following URL in your browser. ; echo '#{url}' )"
end

#versionObject



650
651
652
# File 'lib/soracom/cli.rb', line 650

def version
  puts "Soracom API tool v#{Soracom::VERSION}"
end