Sha256: d85627e7e3fb4024cd7b5cd8878d21b0930efd63e91fe1647c79beae79f228c4

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Pcli
  module Services
    module Commands
      module Users
        class Create < Dry::CLI::Command
          include Depends.on(
            'api',
            'api_manager',
            'input',
            'output',
            'screen',
            'prompt'
          )

          desc 'Create a new administrator.'

          def call(**args)
            payload = {}

            name = prompt.ask('Name')
            payload['name'] = name

            username = prompt.ask('Username')
            payload['username'] = username

            spinner = nil
            response = api_manager.ensure_authenticated do
              spinner = SimpleSpinnerBar.start('Creating user...', output)
              r = api.create_user(payload)
              if r.failure?
                spinner.failure
              end
              r
            end

            if response.success?
              spinner.success("User #{Pl.green('created')}")

              output.puts
              output.puts Pl.yellow('New password: ') + response.json['password']
              output.puts
              output.print 'Copy the password and press any key when finished...'
              input.gets
              output.clear_screen

              output.puts
              output.puts TTY::Table.new(rows: [
                                           [Pl.bold('ID'), response.json['id']],
                                           [Pl.bold('Name'), response.json['name']],
                                           [Pl.bold('Username'), response.json['username']]
                                         ]).render(:ascii)
            else
              spinner.failure

              output.puts
              Output::ServerError.show(response, output, screen)
            end

            CommandOutput.continue
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pcli-0.1.1 lib/pcli/services/commands/users/create.rb
pcli-0.1.0 lib/pcli/services/commands/users/create.rb