Class: Gyaazle::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gyaazle/cli.rb', line 3

def initialize(argv)
  @argv = argv
  @opts = Trollop.options(@argv) do
    version "Gyaazle #{Gyaazle::VERSION}"
    banner <<TEXT
Gyaazle #{Gyaazle::VERSION}

Upload file(s) to Google Drive.
See https://github.com/uu59/gyaazle for more information.

Usage: #{$0} [options] <file1> <file2> ..

[options] are:
TEXT
    opt :config, "Use config file", :default => File.join(ENV["HOME"], ".gyaazle", "config.json"), :short => :none
    opt :edit, "Edit config file by $EDITOR", :type => :boolean, :default => false
    opt :capture, "Capture screenshot to go upload", :type => :boolean, :default => false, :short => "-c"
    opt :open, "Open uploaded file by browser", :type => :boolean, :default => false
  end
  @config = Config.new(@opts[:config])
  @client = Client.new(@config)
end

Instance Method Details

#authorizeObject



77
78
79
80
81
82
# File 'lib/gyaazle/cli.rb', line 77

def authorize
  puts "Open this link by your browser, and authorize"
  puts @client.authorize_url
  print "Paste code here: "
  STDIN.gets.strip
end

#captureObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gyaazle/cli.rb', line 54

def capture
  tmpfile = "/tmp/gyaazle_capture_#{Time.now.strftime("%F %T")}.png"
  capture_cmd = case RUBY_PLATFORM
    when /darwin/
      "screencapture -i '#{tmpfile}'"
    else
      "import '#{tmpfile}'"
  end
  system capture_cmd
  tmpfile
end

#edit_configObject



66
67
68
69
# File 'lib/gyaazle/cli.rb', line 66

def edit_config
  system(ENV["EDITOR"], @config.file)
  puts "Updated!"
end

#initialize_tokensObject



71
72
73
74
75
# File 'lib/gyaazle/cli.rb', line 71

def initialize_tokens
  tokens = @client.get_tokens(authorize)
  @config.save(tokens)
  tokens
end

#run!Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/gyaazle/cli.rb', line 26

def run!
  if @opts[:edit]
    edit_config
  else
    if @opts[:capture]
      @argv = [capture]
    end
    upload
  end
end

#uploadObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gyaazle/cli.rb', line 37

def upload
  if @config.load.nil? || @config.load[:refresh_token].nil?
    credentials = initialize_tokens
  else
    @client.refresh_token!
    credentials = @client.credentials
  end
  @argv.each do |file|
    fileobj = @client.upload(file)
    puts "#{file}: #{fileobj[:alternateLink]}"
    if @opts[:open]
      Launchy.open fileobj[:alternateLink]
    end
    @client.set_permissions(fileobj[:id])
  end
end