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.



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

def initialize(*options)
  @options = options
end

Instance Method Details

#add_color(str, *color) ⇒ Object



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

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

#api_client(ops: false) ⇒ Object



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

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

#bold(str) ⇒ Object



81
82
83
# File 'lib/dri/command.rb', line 81

def bold(str)
  pastel.bold(str)
end

#command(**options) ⇒ Object

The external commands runner



110
111
112
113
# File 'lib/dri/command.rb', line 110

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

#configObject

Main configuration



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

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



120
121
122
123
# File 'lib/dri/command.rb', line 120

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

#editorObject

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



130
131
132
133
# File 'lib/dri/command.rb', line 130

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

#emojiObject



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

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

#executeObject

Execute this command

Raises:

  • (NotImplementedError)


88
89
90
91
92
93
# File 'lib/dri/command.rb', line 88

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

#handover_report_pathObject



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

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

#loggerObject



95
96
97
98
# File 'lib/dri/command.rb', line 95

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

#ops_tokenObject



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

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

#pastel(**options) ⇒ Object



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

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

#profileObject



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

def profile
  @profile ||= config.read
end

#prompt(**options) ⇒ Object

The interactive prompt



140
141
142
143
# File 'lib/dri/command.rb', line 140

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

#spinnerObject



100
101
102
103
# File 'lib/dri/command.rb', line 100

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

#timezoneObject



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

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

#tokenObject



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

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

#usernameObject



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

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

#verify_config_existsObject



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

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