Class: Reivt::CLI

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

Overview

Rev’s cli

Author:

  • brwnrclse

Instance Method Summary collapse

Instance Method Details

#__print_versionObject



22
23
24
# File 'lib/reivt/cli.rb', line 22

def __print_version
  puts VERSION
end

#create(path) ⇒ Object



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
# File 'lib/reivt/cli.rb', line 42

def create(path)
  doc_set = Set.new
  doc_ids = Set.new
  paths = path.split
  paths += options[:list_files] if options[:list_files]
  rev_id = nil
  spinner = TTY::Spinner.new('[:spinner] :msg', format: :bouncing_ball)

  Reivt::Auth.logged_in

  spinner.update(msg: 'Creating a rev...')
  spinner.run do
    rev_id = RevAPI.create_rev(options[:title], options[:description])
  end
  spinner.success(Paint['rev created', :green])

  paths.each do |x|
    type = File.exist?(x) ? File.ftype(x) : 'other'

    if type == 'file'
      spinner.update(msg: "#{Paint['Adding', nil, '#e3b505']} #{x}")
      spinner.run do
        doc_set.add(Util.doc_from_path(x))
      end
      spinner.success(Paint['document created', :green])
    elsif type == 'directory'
      spinner.update(msg: "#{Paint['Adding', nil, '#e3b505']} docs from " \
                     "#{Paint[path, '#2de1fc']}")

      if Dir.entries(x).include?('.git')
        spinner.run do
          doc_set.merge(Util.docs_from_repo(x, spinner))
        end
        spinner.success(Paint['repo docs created', :green])
      else
        spinner.run do
          doc_set.merge(Util.docs_from_dir(x))
        end
        spinner.success(Paint['dir docs created', :green])
      end
    else
      spinner.error("#{Paint['Unsupported file type:']} #{path}")
    end
  end

  spinner.update(msg: 'Uploading docs to rev api')
  spinner.run do
    doc_set.each do |doc|
      doc_id = RevAPI.create_doc(doc.blob, doc.content_type, doc.doc_name,
                                 doc.has_diff, rev_id)
      doc_ids.add(doc_id)
    end
  end
  spinner.success(Paint['docs uploaded to api', :green])

  Reivt::LOGGER.info(
    "Login at #{Paint['wver.vaemoi.co/home', :green]} to start your rev!"
  )
rescue Errno::ECONNRESET, Errno::EINVAL, EOFError, Net::HTTPBadResponse,
       Net::HTTPHeaderSyntaxError, Net::OpenTimeout, Net::ProtocolError,
       Reivt::BaemptyException, Reivt::GraphQLDataException,
       Reivt::GraphQLValidationException => e

  Reivt::DEVLOGGER.error(e.message)
  Reivt::LOGGER.error(e.message)

  unless doc_ids.empty?
    doc_ids.each do |id|
      RevAPI.delete_doc(id)
    end
  end

  unless rev_id.nil?
    RevAPI.delete_rev(rev_id) unless rev_id.nil?
  end
  Reivt::LOGGER.info('Done!')
end

#loginObject



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
# File 'lib/reivt/cli.rb', line 129

def 
  Reivt::LOGGER.info('Login here for your token:')
  Reivt::LOGGER.info(Paint[Auth.auth_code_url, :green])

  auth_code = Thor::Shell::Basic.new.ask("\nEnter your auth code => ")
  spinner = TTY::Spinner.new('[:spinner] :msg', format: :bouncing_ball)

  spinner.update(msg: 'Logging in')
  spinner.run do
    begin
      auth_token = Auth.auth_token(auth_code)
      user_id = RevAPI.(auth_token[:auth0_id])

      if user_id.nil?
        spinner.update(msg: 'User not found! Creating...')
        user_id = RevAPI.create_user(auth_token[:auth0_id])
        spinner.success(Paint['User created', :green])
      end

      Reivt::REIVT_STORE.transaction do |store|
        store[:access_token] = auth_token[:access_token].strip
        store[:expires] = auth_token[:expires]
        store[:auth0_id] = auth_token[:auth0_id].strip
        store[:user_id] = user_id
      end
      spinner.success(Paint['Login successful :)', :green])
    rescue Errno::ECONNRESET, Errno::EINVAL, EOFError,
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
           Net::OpenTimeout, Net::ProtocolError,
           Reivt::GraphQLDataException,
           Reivt::GraphQLValidationException => e

      Reivt::DEVLOGGER.error(e.message)
      Reivt::DEVLOGGER.error(e.message)
    end
  end
end