Class: Dri::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dri/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Command

Returns a new instance of Command.



19
20
21
# File 'lib/dri/command.rb', line 19

def initialize(*options)
  @options = options
end

Instance Method Details

#add_color(str, *color) ⇒ Object



78
79
80
# File 'lib/dri/command.rb', line 78

def add_color(str, *color)
  @options[:no_color] ? str : pastel.decorate(str, *color)
end

#api_client(ops: false) ⇒ Object



39
40
41
# File 'lib/dri/command.rb', line 39

def api_client(ops: false)
  ApiClient.new(config, ops)
end

#command(**options) ⇒ Object

The external commands runner



107
108
109
110
# File 'lib/dri/command.rb', line 107

def command(**options)
  require 'tty-command'
  TTY::Command.new(**options)
end

#configObject

Main configuration



28
29
30
31
32
33
34
35
36
37
# File 'lib/dri/command.rb', line 28

def config
  @config ||= begin
    config = TTY::Config.new
    config.filename = ".dri_profile"
    config.extname = ".yml"
    config.append_path Dir.home
    config.append_path Dir.pwd
    config
  end
end

#cursorObject

The cursor movement



117
118
119
120
# File 'lib/dri/command.rb', line 117

def cursor
  require 'tty-cursor'
  TTY::Cursor
end

#editorObject

Open a file or text in the user’s preferred editor



127
128
129
130
# File 'lib/dri/command.rb', line 127

def editor
  require 'tty-editor'
  TTY::Editor
end

#emojiObject



47
48
49
# File 'lib/dri/command.rb', line 47

def emoji
  @emoji ||= profile["settings"]["emoji"]
end

#executeObject

Execute this command

Raises:

  • (NotImplementedError)


85
86
87
88
89
90
# File 'lib/dri/command.rb', line 85

def execute(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#handover_report_pathObject



67
68
69
# File 'lib/dri/command.rb', line 67

def handover_report_path
  @handover_report_path ||= profile["settings"]["handover_report_path"]
end

#loggerObject



92
93
94
95
# File 'lib/dri/command.rb', line 92

def logger
  require 'tty-logger'
  TTY::Logger.new
end

#ops_tokenObject



59
60
61
# File 'lib/dri/command.rb', line 59

def ops_token
  @ops_token ||= profile["settings"]["ops_token"]
end

#pastel(**options) ⇒ Object



23
24
25
# File 'lib/dri/command.rb', line 23

def pastel(**options)
  Pastel.new(**options)
end

#profileObject



43
44
45
# File 'lib/dri/command.rb', line 43

def profile
  @profile ||= config.read
end

#prompt(**options) ⇒ Object

The interactive prompt



137
138
139
140
# File 'lib/dri/command.rb', line 137

def prompt(**options)
  require 'tty-prompt'
  TTY::Prompt.new(**options.merge(interrupt: :exit))
end

#spinnerObject



97
98
99
100
# File 'lib/dri/command.rb', line 97

def spinner
  require 'tty-spinner'
  TTY::Spinner.new("[:spinner] ⏳", format: :classic)
end

#timezoneObject



63
64
65
# File 'lib/dri/command.rb', line 63

def timezone
  @timezone ||= profile["settings"]["timezone"]
end

#tokenObject



55
56
57
# File 'lib/dri/command.rb', line 55

def token
  @token ||= profile["settings"]["token"]
end

#usernameObject



51
52
53
# File 'lib/dri/command.rb', line 51

def username
  @username ||= profile["settings"]["user"]
end

#verify_config_existsObject



71
72
73
74
75
76
# File 'lib/dri/command.rb', line 71

def verify_config_exists
  return if config.exist?

  logger.error "Oops, could not find a configuration. Try using #{add_color('dri init', :yellow)} first."
  exit 1
end