Module: Huebot::CLI::Helpers

Defined in:
lib/huebot/cli/helpers.rb

Class Method Summary collapse

Class Method Details

.check!(programs, device_mapper, io, quiet: false) ⇒ Object

Prints any program errors or warnings, and returns a boolean for each.

Parameters:

  • programs (Array<Huebot::Program>)
  • device_mapper (Huebot::DeviceMapper)
  • io (IO)

    Usually $stdout or $stderr

  • quiet (Boolean) (defaults to: false)

    if true, don’t print anything



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
# File 'lib/huebot/cli/helpers.rb', line 97

def self.check!(programs, device_mapper, io, quiet: false)
  if (invalid_progs = programs.select { |prog| prog.errors.any? }).any?
    errors = invalid_progs.reduce([]) { |acc, prog|
      acc + prog.errors.map { |e| "  #{prog.name}: #{e}" }
    }
    print_messages! io, "Errors", errors unless quiet
  end

  if (imperfect_progs = programs.select { |prog| prog.warnings.any? }).any?
    warnings = imperfect_progs.reduce([]) { |acc, prog|
      acc + prog.warnings.map { |e| "  #{prog.name}: #{e}" }
    }
    print_messages! io, "Warnings", warnings unless quiet
  end

  all_lights = programs.reduce([]) { |acc, p| acc + p.light_names }
  if (missing_lights = device_mapper.missing_lights all_lights).any?
    print_messages! io, "Unknown lights", missing_lights unless quiet
  end

  all_groups = programs.reduce([]) { |acc, p| acc + p.group_names }
  if (missing_groups = device_mapper.missing_groups all_groups).any?
    print_messages! io, "Unknown groups", missing_groups unless quiet
  end

  all_vars = programs.reduce([]) { |acc, p| acc + p.device_refs }
  if (missing_vars = device_mapper.missing_vars all_vars).any?
    print_messages! io, "Unknown device inputs", missing_vars.map { |d| "$#{d}" } unless quiet
  end

  invalid_devices = missing_lights.size + missing_groups.size + missing_vars.size
  return invalid_progs.any?, imperfect_progs.any?, invalid_devices > 0
end

.get_args(argv = ARGV, min: nil, max: nil, num: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/huebot/cli/helpers.rb', line 26

def self.get_args(argv = ARGV, min: nil, max: nil, num: nil)
  args = argv[1..]
  if num
    if num != args.size
      return nil, "Expected #{num} args, found #{args.size}"
    end
  elsif min and max
    if args.size < min or args.size > max
      return nil, "Expected #{min}-#{max} args, found #{args.size}"
    end
  elsif min
    if args.size < min
      return nil, "Expected at least #{min} args, found #{args.size}"
    end
  elsif max
    if args.size > max
      return nil, "Expected no more than #{max} args, found #{args.size}"
    end
  end
  return args, nil
end

.get_args!(argv = ARGV, min: nil, max: nil, num: nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/huebot/cli/helpers.rb', line 17

def self.get_args!(argv = ARGV, min: nil, max: nil, num: nil)
  args, error = get_args(argv, min: min, max: max, num: num)
  if error
    $stderr.puts error
    exit 1
  end
  args
end

.get_cmd(argv = ARGV) ⇒ Symbol

Returns the command given to huebot.

Returns:

  • (Symbol)


13
14
15
# File 'lib/huebot/cli/helpers.rb', line 13

def self.get_cmd(argv = ARGV)
  argv[0].to_s.to_sym
end

.get_input!(opts, argv = ARGV) ⇒ Array<Huebot::Program::Src>

Parses and returns input from the CLI. Serious errors might result in the program exiting.

Parameters:

Returns:



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
# File 'lib/huebot/cli/helpers.rb', line 54

def self.get_input!(opts, argv = ARGV)
  files = argv[1..-1]
  if (bad_paths = files.select { |p| !File.exist? p }).any?
    opts.stderr.puts "Cannot find #{bad_paths.join ', '}"
    return []
  end

  sources = files.map { |path|
    ext = File.extname path
    src =
      case ext
      when ".yaml", ".yml"
        YAML.safe_load(File.read path) || {}
      when ".json"
        JSON.load(File.read path) || {}
      else
        opts.stderr.puts "Unknown file extension '#{ext}'. Expected .yaml, .yml, or .json"
        return []
      end
    version = (src.delete("version") || 1.0).to_f
    Program::Src.new(src, path, version)
  }

  if !opts.stdin.isatty or opts.read_stdin
    opts.stdout.puts "Please enter your YAML or JSON Huebot program below, followed by Ctrl+d:" if opts.read_stdin
    raw = opts.stdin.read.lstrip
    src = raw[0] == "{" ? JSON.load(raw) : YAML.safe_load(raw)

    opts.stdout.puts "Executing..." if opts.read_stdin
    version = (src.delete("version") || 1.0).to_f
    sources << Program::Src.new(src, "STDIN", version)
  end
  sources
end

.get_opts!Object



131
132
133
134
135
136
# File 'lib/huebot/cli/helpers.rb', line 131

def self.get_opts!
  opts = default_options
  parser = option_parser opts
  parser.parse!
  opts
end

.help!Object

Print help and exit



139
140
141
142
143
144
# File 'lib/huebot/cli/helpers.rb', line 139

def self.help!
  opts = default_options
  parser = option_parser opts
  opts.stdout.puts parser.help
  exit 1
end