Class: Homebrew::Cmd::Services Private

Inherits:
AbstractCommand show all
Defined in:
cmd/services.rb,
sorbet/rbi/dsl/homebrew/cmd/services.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::Cmd::Services::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/cmd/services.rbi', line 10

def args; end

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'cmd/services.rb', line 67

def run
  # pbpaste's exit status is a proxy for detecting the use of reattach-to-user-namespace
  if ENV.fetch("HOMEBREW_TMUX", nil) && File.exist?("/usr/bin/pbpaste") && !quiet_system("/usr/bin/pbpaste")
    raise UsageError,
          "`brew services` cannot run under tmux!"
  end

  # Keep this after the .parse to keep --help fast.
  require "utils"

  if !Homebrew::Services::System.launchctl? && !Homebrew::Services::System.systemctl?
    raise UsageError,
          "`brew services` is supported only on macOS or Linux (with systemd)!"
  end

  if (sudo_service_user = args.sudo_service_user)
    unless Homebrew::Services::System.root?
      raise UsageError,
            "`brew services` is supported only when running as root!"
    end

    unless Homebrew::Services::System.launchctl?
      raise UsageError,
            "`brew services --sudo-service-user` is currently supported only on macOS " \
            "(but we'd love a PR to add Linux support)!"
    end

    Homebrew::Services::Cli.sudo_service_user = sudo_service_user
  end

  # Parse arguments.
  subcommand, *formulae = args.named

  no_named_formula_commands = [
    *Homebrew::Services::Commands::List::TRIGGERS,
    *Homebrew::Services::Commands::Cleanup::TRIGGERS,
  ]
  if no_named_formula_commands.include?(subcommand)
    raise UsageError, "The `#{subcommand}` subcommand does not accept a formula argument!" if formulae.present?
    raise UsageError, "The `#{subcommand}` subcommand does not accept the --all argument!" if args.all?
  end

  if args.file
    file_commands = [
      *Homebrew::Services::Commands::Start::TRIGGERS,
      *Homebrew::Services::Commands::Run::TRIGGERS,
      *Homebrew::Services::Commands::Restart::TRIGGERS,
    ]
    if file_commands.exclude?(subcommand)
      raise UsageError, "The `#{subcommand}` subcommand does not accept the --file= argument!"
    elsif args.all?
      raise UsageError,
            "The `#{subcommand}` subcommand does not accept the --all and --file= arguments at the same time!"
    end
  end

  unless Homebrew::Services::Commands::Stop::TRIGGERS.include?(subcommand)
    raise UsageError, "The `#{subcommand}` subcommand does not accept the --keep argument!" if args.keep?
    raise UsageError, "The `#{subcommand}` subcommand does not accept the --no-wait argument!" if args.no_wait?
    if args.max_wait
      raise UsageError, "The `#{subcommand}` subcommand does not accept the --max-wait= argument!"
    end
  end

  opoo "The --all argument overrides provided formula argument!" if formulae.present? && args.all?

  targets = if args.all?
    if subcommand == "start"
      Homebrew::Services::Formulae.available_services(
        loaded:    false,
        skip_root: !Homebrew::Services::System.root?,
      )
    elsif subcommand == "stop"
      Homebrew::Services::Formulae.available_services(
        loaded:    true,
        skip_root: !Homebrew::Services::System.root?,
      )
    else
      Homebrew::Services::Formulae.available_services
    end
  elsif formulae.present?
    formulae.map { |formula| Homebrew::Services::FormulaWrapper.new(Formulary.factory(formula)) }
  else
    []
  end

  # Exit successfully if --all was used but there is nothing to do
  return if args.all? && targets.empty?

  if Homebrew::Services::System.systemctl?
    ENV["DBUS_SESSION_BUS_ADDRESS"] = ENV.fetch("HOMEBREW_DBUS_SESSION_BUS_ADDRESS", nil)
    ENV["XDG_RUNTIME_DIR"] = ENV.fetch("HOMEBREW_XDG_RUNTIME_DIR", nil)
  end

  # Dispatch commands and aliases.
  case subcommand.presence
  when *Homebrew::Services::Commands::List::TRIGGERS
    Homebrew::Services::Commands::List.run(json: args.json?)
  when *Homebrew::Services::Commands::Cleanup::TRIGGERS
    Homebrew::Services::Commands::Cleanup.run
  when *Homebrew::Services::Commands::Info::TRIGGERS
    Homebrew::Services::Commands::Info.run(targets, verbose: args.verbose?, json: args.json?)
  when *Homebrew::Services::Commands::Restart::TRIGGERS
    Homebrew::Services::Commands::Restart.run(targets, args.file, verbose: args.verbose?)
  when *Homebrew::Services::Commands::Run::TRIGGERS
    Homebrew::Services::Commands::Run.run(targets, args.file, verbose: args.verbose?)
  when *Homebrew::Services::Commands::Start::TRIGGERS
    Homebrew::Services::Commands::Start.run(targets, args.file, verbose: args.verbose?)
  when *Homebrew::Services::Commands::Stop::TRIGGERS
    Homebrew::Services::Commands::Stop.run(
      targets,
      verbose:  args.verbose?,
      no_wait:  args.no_wait?,
      max_wait: args.max_wait.to_f,
      keep:     args.keep?,
    )
  when *Homebrew::Services::Commands::Kill::TRIGGERS
    Homebrew::Services::Commands::Kill.run(targets, verbose: args.verbose?)
  else
    raise UsageError, "unknown subcommand: `#{subcommand}`"
  end
end