Class: GithubMembers::CLI

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

Constant Summary collapse

EXIT_SUCCESS =
0
EXIT_FAILURE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



9
10
11
# File 'lib/github_members/cli.rb', line 9

def initialize(argv)
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



6
7
8
# File 'lib/github_members/cli.rb', line 6

def argv
  @argv
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/github_members/cli.rb', line 7

def options
  @options
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/github_members/cli.rb', line 13

def run
  begin
    @options = Options.new(argv)
  rescue Options::ParseError => e
    warn e.message
    return EXIT_FAILURE
  end

  case
   when options.help_shown
     puts options.help
     return EXIT_SUCCESS
  when options.version
    puts options.version
    return EXIT_SUCCESS
  when options.github_org.nil?
    warn "Error: Organization is missing"
    warn ""
    warn options.help
    return EXIT_FAILURE
  end

  members = read_members

  Client.new(options).fetch_members(options.github_org).each do |new_member|
    github = new_member.fetch(:github)
    fullname = new_member.fetch(:fullname)
    avatar = new_member.fetch(:avatar)

    if members.key?(github)
      members[github].tap do |m|
        m.fullname = fullname
        m.avatar = avatar
        m.updated = true
      end
    else
      members[github] = member_class.new(
        github: github,
        fullname: fullname,
        avatar: avatar,
        updated: true
      )
    end
  end

  members.delete_if { |_, m| !m.updated }

  write_members(members.values)

  MarkdownWriter.new.write(members: members.values, file: options.markdown_file)

  EXIT_SUCCESS
end