Class: Gyaazle::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



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

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 Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/gyaazle/cli.rb', line 3

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/gyaazle/cli.rb', line 3

def config
  @config
end

Instance Method Details

#authorizeObject



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

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

#captureObject



56
57
58
59
60
# File 'lib/gyaazle/cli.rb', line 56

def capture
  tmpfile = "/tmp/gyaazle_capture_#{Time.now.strftime("%F %T")}.png"
  system capture_cmd(tmpfile)
  tmpfile
end

#check_credentials!Object



79
80
81
82
83
84
85
# File 'lib/gyaazle/cli.rb', line 79

def check_credentials!
  if config.load.nil? || config.load[:refresh_token].nil?
    initialize_tokens(authorize)
  else
    client.refresh_token!
  end
end

#edit_configObject



62
63
64
# File 'lib/gyaazle/cli.rb', line 62

def edit_config
  system(ENV["EDITOR"], config.file)
end

#initialize_tokens(verifier = nil) ⇒ Object



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

def initialize_tokens(verifier = nil)
  tokens = client.get_tokens(verifier || authorize)
  config.save(tokens)
  tokens
end

#run!Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gyaazle/cli.rb', line 28

def run!
  if @opts[:edit]
    edit_config
  else
    if @opts[:capture] || @argv.empty?
      @argv = [capture]
    end

    check_credentials!
    upload
  end
end

#uploadObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gyaazle/cli.rb', line 41

def upload
  @argv.each do |file|
    fileobj = client.upload(file)
    puts "#{file}:"
    puts "  * url: #{fileobj[:alternateLink]}"
    puts "  * download: #{fileobj[:downloadUrl]}"
    puts "  * deep link: https://drive.google.com/uc?export=view&id=#{fileobj[:id]}"
    puts
    if @opts[:open]
      Launchy.open fileobj[:alternateLink]
    end
    client.set_permissions(fileobj[:id])
  end
end