Class: Ruush::CLI

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

Class Method Summary collapse

Class Method Details

.die(message) ⇒ Object



7
8
9
10
# File 'lib/ruush/cli.rb', line 7

def die(message)
  warn message
  exit
end

.run!(argv = ARGV.dup) ⇒ Object



174
175
176
177
# File 'lib/ruush/cli.rb', line 174

def run!(argv = ARGV.dup)
  slop = setup
  slop.parse! argv
end

.setupObject

this whole thing will (hopefully) be cleaned up at a later date



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruush/cli.rb', line 12

def setup # this whole thing will (hopefully) be cleaned up at a later date
  slop = Slop.new :strict => true, :help => true do |s|
    s.banner "Usage: ruush <command> [options] [<args>]"
    s.on :v, :version, "Print version and exit", :tail => true do puts "ruush v#{Ruush::VERSION} based on puush #{Ruush::PUUSH_VERSION}"; end

    s.command "setup", :help => true do
      description "Interactively set up ~/.ruush"
      banner "Usage: ruush setup [options]"

      run do |opts, args|
        raise Slop::InvalidArgumentError, "Too many arguments (expected zero)" if args.length > 0
        exit if opts.to_h[:help]

        print "Email: "
        email = ($stdin.gets || "").chomp
        exit if email == ""
        print "Password: "
        password = ($stdin.noecho(&:gets) || "").chomp
        print "\n"

        begin
          result = Api::auth_password email, password
          Ruush::config["key"] = result.key
          Ruush::config["email"] = email
          Ruush::config["is_premium"] = result.is_premium
          Ruush::config["usage_bytes"] = result.usage_bytes
          puts "Successfully authenticated with puush"
        rescue BadAuth
          warn "Email or password is incorrect"
        end
      end
    end

    s.command "auth", :help => true do
      description "Authenticate with API key in ~/.ruush"
      banner "Usage: ruush auth [options]"

      run do |opts, args|
        raise Slop::InvalidArgumentError, "Too many arguments (expected zero)" if args.length > 0
        exit if opts.to_h[:help]

        begin
          result = Api::auth_key Ruush::config["key"]
          Ruush::config["is_premium"] = result.is_premium
          Ruush::config["usage_bytes"] = result.usage_bytes
          puts "Successfully authenticated with puush"
        rescue BadAuth
          CLI::die "API key is invalid"
        end
      end
    end

    s.command "upload", :help => true do
      description "Upload a file to puush"
      banner "Usage: ruush upload [options] [<file>]"
      on :s, :silent, "Do not print url"

      run do |opts, args|
        raise Slop::MissingArgumentError, "Missing file argument" if args.length < 1
        raise Slop::InvalidArgumentError, "Too many files (expected one)" if args.length > 1
        exit if opts.to_h[:help]
        CLI::die "Please run `ruush setup` or place your API key in ~/.ruush" if Ruush::config["key"] == ""

        begin
          key = Ruush::config[:key]
          result = Api::upload_file key, File.open(args[0])
          Ruush::config["usage_bytes"] = result.usage_bytes
          puts result.url if !opts.to_h[:silent]
        rescue Error => e
          CLI::die e
        end
      end
    end

    s.command "list", :help => true do
      description "List most recent puushes"
      banner "Usage: ruush list [options]"
      on :f, :files, "Only print filenames"
      on :u, :urls, "Only print URLs"
      on :n=, :number=, "Number of puushes to list (defaults 10)", :as => Integer
      on :o=, :offset=, "How many puushes to skip before listing (defaults 0)", :as => Integer # confusing

      run do |opts, args|
        raise Slop::InvalidArgumentError, "Too many arguments (expected zero)" if args.length > 0
        exit if opts.to_h[:help]
        CLI::die "Please run `ruush setup` or place your API key in ~/.ruush" if Ruush::config["key"] == ""

        begin
          history = Api::get_hist Ruush::config["key"], (opts.to_h[:number] || 10), (opts.to_h[:offset] || 0)
          if history.length == 0
            puts "You have no puushes!"
          else
            history.each do |h|
              if opts.to_h[:files]
                puts h.filename
              elsif opts.to_h[:urls]
                puts h.url
              else
                puts "#{h.url} - #{h.filename}"
              end
            end
          end
        rescue BadKey => e
          CLI::die "API key is invalid"
        end
      end
    end

    s.command "usage", :help => true do
      description "Display current storage usage"
      banner "Usage: ruush usage [options]"
      on :b, :bytes, "Print usage in bytes"
      on :r, :raw, "Only print byte information (implies --bytes)"

      run do |opts, args|
        raise Slop::InvalidArgumentError, "Too many arguments (expected zero)" if args.length > 0
        exit if opts.to_h[:help]
        CLI::die "Please run `ruush setup` or place your API key in ~/.ruush" if Ruush::config["key"] == ""

        begin
          bytes = Api::get_usage Ruush::config["key"]
          Ruush::config["usage_bytes"] = bytes
          if opts.to_h[:raw]
            puts bytes
          elsif opts.to_h[:bytes]
            puts "Current usage: %d bytes" % bytes
          else
            puts "Current usage: %.2fMB" % (bytes / 1048576.0) # bytes in a megabyte
          end
        rescue BadKey => e
          CLI::die "API key is invalid"
        end
      end
    end

    s.command "help", :help => true do
      description "Alias for `<command> --help`"
      banner "Usage: ruush help <command>"

      run do |opts, args|
        exit if opts.to_h[:help]

        if args.length != 0 and s.commands.has_key? args[0]
          s.commands[args[0]].options.detect { |opt| opt.long == "help" }.call
        else
          s.options.detect { |opt| opt.long == "help" }.call
        end
      end
    end

    s.run do |opts, args|
      if args.length == 0
        if opts.to_hash.values.all? { |x| x.nil? }
          s.options.detect { |opt| opt.long == "help" }.call
        end
      else
        raise Slop::InvalidCommandError, "Unknown command #{args[0]}"
      end
    end
  end
end