Class: Jets::Commands::Configure

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/commands/configure.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configure

Returns a new instance of Configure.



5
6
7
# File 'lib/jets/commands/configure.rb', line 5

def initialize(options)
  @options = options
end

Instance Method Details

#load_yamlObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jets/commands/configure.rb', line 17

def load_yaml
  if File.exist?(path)
    YAML.load_file(path)
  else
    {}
  end
rescue Psych::SyntaxError => e
  puts "WARN: There was an error reading #{pretty(path)}".color(:yellow)
  puts "WARN: #{e.class} #{e.message}".color(:yellow)
  {}
end

#pathObject



33
34
35
# File 'lib/jets/commands/configure.rb', line 33

def path
  "#{ENV['HOME']}/.jets/config.yml"
end

#pretty(path) ⇒ Object



29
30
31
# File 'lib/jets/commands/configure.rb', line 29

def pretty(path)
  path.sub(ENV['HOME'], '~')
end

#promptObject



42
43
44
45
46
47
48
49
# File 'lib/jets/commands/configure.rb', line 42

def prompt
  puts <<~EOL
    You are about to configure your ~/.jets/config.yml
    You can get a token from serverlessgems.com
  EOL
  print "Please provide your token: "
  $stdin.gets.strip
end

#runObject



9
10
11
12
13
14
15
# File 'lib/jets/commands/configure.rb', line 9

def run
  data = load_yaml
  data['key'] = token
  FileUtils.mkdir_p(File.dirname(path))
  IO.write(path, YAML.dump(data))
  puts "Updated #{pretty(path)}"
end

#tokenObject



37
38
39
# File 'lib/jets/commands/configure.rb', line 37

def token
  @options[:token] || prompt
end