Class: KBSecret::CLI::Command::Session

Inherits:
Abstract
  • Object
show all
Defined in:
lib/kbsecret/cli/command/session.rb

Overview

The implementation of kbsecret session.

Constant Summary collapse

SUBCOMMANDS =

The list of subcommands supported by kbsecret session.

%w[new rm].freeze

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kbsecret/cli/command/session.rb', line 11

def initialize(argv)
  super(argv) do |cli|
    cli.slop cmds: SUBCOMMANDS do |o|
      o.banner = <<~HELP
        Usage:
          kbsecret session [options] <new|rm> <label>
      HELP

      o.string "-t", "--team", "the team to create the session under"
      o.array "-u", "--users", "the Keybase users", default: [Keybase::Local.current_user]
      o.string "-r", "--root", "the secret root directory"
      o.bool "-c", "--create-team", "create the Keybase team if it does not exist"
      o.bool "-f", "--force", "force creation (ignore overwrites, etc.)"
      o.bool "-n", "--no-notify", "do not send a notification to session members"
      o.bool "-d", "--delete", "unlink the session in addition to deconfiguration"
    end

    cli.dreck do
      string :command
      string :session
    end

    cli.ensure_session! :argument if cli.args[:command] == "rm"
  end
end

Instance Method Details

#new_sessionvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



60
61
62
63
64
65
66
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
# File 'lib/kbsecret/cli/command/session.rb', line 60

def new_session
  if Config.session?(@label) && !cli.opts.force?
    cli.die "Refusing to overwrite a session without --force."
  end

  if cli.opts[:team]
    teams = Keybase::Local::Team.list_self_memberships.teams

    unless teams.map(&:fq_name).include?(cli.opts[:team])
      if cli.opts.create_team?
        Keybase::Local::Team.create_team cli.opts[:team]
        Keybase::Local::Team.add_members cli.opts[:team], users: [{
          username: Keybase::Local.current_user,
          role: "admin",
        }]
      else
        cli.die "No such team (either nonexistent or non-member)."
      end
    end

    Config.configure_session(@label, team: cli.opts[:team], root: @label)
  else
    cli.die "Missing `-r', `--root' option." unless cli.opts[:root]

    cli.opts[:users].each do |user|
      cli.die "Nonexistent Keybase user: #{user}." unless Keybase::API.user? user
    end

    unless cli.opts[:users].include? Keybase::Local.current_user
      cli.warn "You didn't include yourself in the user list, but I'll add you."
      cli.opts[:users] << Keybase::Local.current_user
    end

    Config.configure_session(@label, users: cli.opts[:users], root: cli.opts[:root])

    unless cli.opts.no_notify? && cli.opts[:users] != [Keybase::Local.current_user]
      users = cli.opts[:users].join(",")

      Keybase::Local::Chat.send_message cli.opts[:users], <<~MESSAGE
        You've been added to a KBSecret session!

        To access this session, please run the following:

        ```
          $ kbsecret session new -r '#{cli.opts[:root]}' -u #{users} <label>
        ```

        If you don't have KBSecret installed, you can install it from `gem`:

        ```
          $ gem install kbsecret
        ```
      MESSAGE
    end
  end
end

#rm_sessionvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



119
120
121
122
# File 'lib/kbsecret/cli/command/session.rb', line 119

def rm_session
  cli.session.unlink! if cli.opts.delete?
  Config.deconfigure_session @label
end

#run!Object

See Also:



49
50
51
52
53
54
55
56
# File 'lib/kbsecret/cli/command/session.rb', line 49

def run!
  case @subcmd
  when "new"
    new_session
  when "rm"
    rm_session
  end
end

#setup!Object

See Also:



38
39
40
41
# File 'lib/kbsecret/cli/command/session.rb', line 38

def setup!
  @label = cli.args[:session]
  @subcmd = cli.args[:command]
end

#validate!Object

See Also:



44
45
46
# File 'lib/kbsecret/cli/command/session.rb', line 44

def validate!
  cli.die "Unknown subcommand: #{@subcmd}." unless SUBCOMMANDS.include?(@subcmd)
end