Class: GithubMembers::CLI

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

Defined Under Namespace

Classes: RequiredMemberAttributes

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.



11
12
13
# File 'lib/github_members/cli.rb', line 11

def initialize(argv)
  @argv = argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



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

def argv
  @argv
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#runObject



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/github_members/cli.rb', line 15

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

  org = Organization.new(client.fetch_organization(options.github_org))

  yaml_file = options.yaml_file
  members =
    begin
      read_members(yaml_file)
    rescue RequiredMemberAttributes => e
      warn e.message
      return EXIT_FAILURE
    end

  client.fetch_members(org.name).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, yaml_file)
  puts "Updated: #{yaml_file}"

  markdown_file = options.markdown_file
  MarkdownWriter.new.write(org: org, members: members.values, file: markdown_file)
  puts "Updated: #{markdown_file}"

  EXIT_SUCCESS
end