Class: RKSeal::CLI

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

Overview

Thor-based command-line interface: parses ARGV, validates options, and dispatches to the orchestration commands. It is intentionally thin – it maps flags/positionals onto RKSeal::Commands::Create / RKSeal::Commands::Edit, prints their RKSeal::Commands::Result, and turns the gem’s fail-fast Errors into a single clean line + non-zero exit. No business logic lives here.

rubocop:disable Metrics/ClassLength – length here is Thor’s declarative ‘method_option` surface (every flag for both subcommands plus their long_desc help text), not logic. The two command bodies stay thin and delegate straight to the orchestration classes.

Constant Summary collapse

SCOPE_SYMBOLS =

kubeseal’s ‘–scope` strings, as exposed on the CLI, mapped to the symbols the command/adapter layers expect. Thor does not underscore enum values.

{
  "strict" => :strict,
  "namespace-wide" => :namespace_wide,
  "cluster-wide" => :cluster_wide
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dispatch(*args) ⇒ void

This method returns an undefined value.

Entry point used by ‘exe/rkseal`, and Thor’s internal command router.

Dual-role on arity:

- called as `dispatch(argv)` (a single Array, from `exe/rkseal`): run
  {Thor.start} and translate any deliberately-raised {RKSeal::Error}
  into a one-line stderr message with a non-zero exit -- no backtrace.
  Thor's own parse errors keep their {exit_on_failure?} handling;
  unexpected exceptions propagate.
- called by Thor internally (`dispatch(meth, args, opts, config)`):
  delegate to Thor's preserved router unchanged.

Parameters:

  • args (Array)

    either ‘[argv]` (public) or Thor’s four router args.



52
53
54
55
56
57
58
59
60
61
# File 'lib/rkseal/cli.rb', line 52

def dispatch(*args)
  return thor_dispatch(*args) unless args.length == 1 && args.first.is_a?(Array)

  begin
    start(args.first)
  rescue RKSeal::Error => e
    warn(e.message)
    exit(1)
  end
end

.exit_on_failure?Boolean

Make argument/usage errors (and our rescued errors) exit non-zero rather than return 0, so the CLI is shell-script friendly.

Returns:

  • (Boolean)


28
29
30
# File 'lib/rkseal/cli.rb', line 28

def self.exit_on_failure?
  true
end

.thor_dispatchObject

‘dispatch` is also Thor’s own internal 4-arg command router, which Thor.start calls. Preserve it under an alias so our public 1-arg entry point can reuse the name (as the gem’s contract requires) without clobbering Thor’s routing.



37
# File 'lib/rkseal/cli.rb', line 37

alias thor_dispatch dispatch

Instance Method Details

#create(namespace, name) ⇒ void

This method returns an undefined value.

Author a new SealedSecret.

Parameters:

  • namespace (String)

    target namespace.

  • name (String)

    Secret name (also the output filename stem).



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rkseal/cli.rb', line 99

def create(namespace, name)
  validate_identifiers!(namespace, name)
  result = Commands::Create.new(
    namespace: namespace, name: name,
    scope: scope_symbol, type: options["type"],
    from_file: parsed_from_file, no_edit: options["no-edit"],
    string_data: options["string-data"],
    kubeseal: build_kubeseal
  ).call
  report(result)
end

#edit(namespace, name) ⇒ void

This method returns an undefined value.

Edit an existing SealedSecret. Reads current values from the cluster; if the Secret is absent there but a local <NAME>.yaml exists, automatically falls back to the offline local edit. ‘–local` forces the offline path.

Parameters:

  • namespace (String)

    target namespace.

  • name (String)

    Secret name (also the output filename stem).



175
176
177
178
179
# File 'lib/rkseal/cli.rb', line 175

def edit(namespace, name)
  validate_identifiers!(namespace, name)
  result = options["local"] ? edit_local(namespace, name) : edit_auto(namespace, name)
  report(result)
end

#list(namespace = nil) ⇒ void

This method returns an undefined value.

List SealedSecrets (read-only, metadata only).

Parameters:

  • namespace (String, nil) (defaults to: nil)

    limit to this namespace; omit for all.



299
300
301
302
# File 'lib/rkseal/cli.rb', line 299

def list(namespace = nil)
  Secret.validate_identifier!(field: "namespace", value: namespace) if namespace
  say(Commands::List.new(namespace: namespace, kubectl: Kubectl.new).call)
end

#reencrypt(namespace, name) ⇒ void

This method returns an undefined value.

Re-encrypt an existing SealedSecret to the newest controller key.

Parameters:

  • namespace (String)

    target namespace.

  • name (String)

    Secret name (also the output filename stem).



211
212
213
214
215
216
217
218
219
# File 'lib/rkseal/cli.rb', line 211

def reencrypt(namespace, name)
  validate_identifiers!(namespace, name)
  result = Commands::Reencrypt.new(
    namespace: namespace, name: name,
    deploy: options["deploy"], assume_yes: options["yes"],
    kubectl: Kubectl.new, kubeseal: build_kubeseal
  ).call
  report(result)
end

#validate(namespace = nil, name = nil) ⇒ void

This method returns an undefined value.

Validate a SealedSecret (local <NAME>.yaml, or –file <path>).

Parameters:

  • namespace (String, nil) (defaults to: nil)

    target namespace (omit with –file).

  • name (String, nil) (defaults to: nil)

    Secret name (omit with –file).

Raises:



248
249
250
251
252
253
254
255
256
257
# File 'lib/rkseal/cli.rb', line 248

def validate(namespace = nil, name = nil)
  file = options["file"]
  raise InvalidInputError, "give NAMESPACE NAME or --file <path>" if file.nil? && name.nil?

  validate_identifiers!(namespace, name) unless file
  path = Commands::Validate.new(
    namespace: namespace, name: name, file: file, kubeseal: build_kubeseal
  ).call
  say("SealedSecret #{path} is valid.")
end

#versionvoid

This method returns an undefined value.



307
308
309
# File 'lib/rkseal/cli.rb', line 307

def version
  say("rkseal #{RKSeal::VERSION}")
end

#view(namespace, name) ⇒ void

This method returns an undefined value.

Print the live Secret for a SealedSecret (read-only).

Parameters:

  • namespace (String)

    target namespace.

  • name (String)

    Secret name.



277
278
279
280
281
282
283
# File 'lib/rkseal/cli.rb', line 277

def view(namespace, name)
  validate_identifiers!(namespace, name)
  manifest = Commands::View.new(
    namespace: namespace, name: name, reveal: options["reveal"], kubectl: Kubectl.new
  ).call
  say(manifest)
end