analytics.rb 1.27 KB
Newer Older
Markus Reiter's avatar
Markus Reiter committed
1
# typed: true
2
3
# frozen_string_literal: true

4
require "cli/parser"
5

Xu Cheng's avatar
Xu Cheng committed
6
module Homebrew
Markus Reiter's avatar
Markus Reiter committed
7
8
  extend T::Sig

9
10
  module_function

Markus Reiter's avatar
Markus Reiter committed
11
  sig { returns(CLI::Parser) }
12
13
14
  def analytics_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
15
        `analytics` [<subcommand>]
16

17
        Control Homebrew's anonymous aggregate user behaviour analytics.
18
19
        Read more at <https://docs.brew.sh/Analytics>.

20
21
22
        `brew analytics` [`state`]:
        Display the current state of Homebrew's analytics.

EricFromCanada's avatar
EricFromCanada committed
23
        `brew analytics` (`on`|`off`):
24
25
26
27
        Turn Homebrew's analytics on or off respectively.

        `brew analytics regenerate-uuid`:
        Regenerate the UUID used for Homebrew's analytics.
28
      EOS
EricFromCanada's avatar
EricFromCanada committed
29

30
      max_named 1
31
32
33
    end
  end

Xu Cheng's avatar
Xu Cheng committed
34
  def analytics
Markus Reiter's avatar
Markus Reiter committed
35
    args = analytics_args.parse
Xu Cheng's avatar
Xu Cheng committed
36

37
    case args.named.first
Xu Cheng's avatar
Xu Cheng committed
38
    when nil, "state"
39
40
      if Utils::Analytics.disabled?
        puts "Analytics are disabled."
Xu Cheng's avatar
Xu Cheng committed
41
      else
42
43
        puts "Analytics are enabled."
        puts "UUID: #{Utils::Analytics.uuid}" if Utils::Analytics.uuid.present?
Xu Cheng's avatar
Xu Cheng committed
44
45
      end
    when "on"
46
      Utils::Analytics.enable!
Xu Cheng's avatar
Xu Cheng committed
47
    when "off"
48
      Utils::Analytics.disable!
Xu Cheng's avatar
Xu Cheng committed
49
    when "regenerate-uuid"
50
      Utils::Analytics.regenerate_uuid!
Xu Cheng's avatar
Xu Cheng committed
51
    else
52
      raise UsageError, "unknown subcommand: #{args.named.first}"
Xu Cheng's avatar
Xu Cheng committed
53
54
55
    end
  end
end