Class: OSDN::CLI::Command::Package

Inherits:
Base
  • Object
show all
Defined in:
lib/osdn/cli/command/package.rb

Instance Attribute Summary

Attributes inherited from Base

#credential, #format, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#credential_path, #initialize, #load_credential, #load_variables, #set_client_token, #set_credential, #update_token, #update_variables, #write_credential, #write_variables

Constructor Details

This class inherits a constructor from OSDN::CLI::Command::Base

Class Method Details

.descriptionObject



14
15
16
# File 'lib/osdn/cli/command/package.rb', line 14

def self.description
  "Manipulate release package of project"
end

.helpObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/osdn/cli/command/package.rb', line 3

def self.help
  puts "#{$0} package [opts] [list]"
  puts "#{$0} package [opts] create <new-package-name>"
  puts "#{$0} package [opts] update <numeric-package-id> [name]"
  puts "#{$0} package [opts] delete <numeric-package-id>"
  puts "Options:"
  puts "  -f --format=<pretty|json>  Set output format"
  puts "  -p --project=<project>     Target project (numeric id or name)"
  puts "  -v --visibility=<public|private|hidden>"
end

Instance Method Details

#createObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/osdn/cli/command/package.rb', line 67

def create
  name = ARGV.shift
  if !name
    logger.fatal "Package name is missing."
    help
    return
  end
  p = api.create_package target_proj, name, visibility: @visibility
  logger.info "New package has been created."
  puts format_package(p)
end

#deleteObject



90
91
92
93
94
# File 'lib/osdn/cli/command/package.rb', line 90

def delete
  target_id = ARGV.shift
  p = api.delete_package target_proj, target_id
  logger.info "Package #{target_id} has been deleted."
end

#listObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/osdn/cli/command/package.rb', line 56

def list
  list = api.list_packages target_proj
  if format == 'json'
    puts list.to_json
  else
    list.each do |p|
      puts format_package(p)
    end
  end
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/osdn/cli/command/package.rb', line 18

def run
  update_token
  opts = GetoptLong.new(
    [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--visibility', '-v', GetoptLong::REQUIRED_ARGUMENT ],
  )
  opts.each do |opt, arg|
    case opt
    when '--format'
      arg == 'json' and
        self.format = arg
    when '--project'
      arg.empty? or
        @target_proj = arg
    when '--visibility'
      unless %w(public private hidden).member?(arg)
        logger.fatal "Invalid visibility status: #{arg}"
        exit
      end
      @visibility = arg
    end
  end
  command = ARGV.shift || 'list'
  if !command || command.empty?
    logger.fatal "subcommand is missing."
    help
    return
  end
  if self.respond_to? command
    self.send command
  else
    logger.fatal "Invalid subcommand: #{command}"
    help
    return
  end
end

#updateObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/osdn/cli/command/package.rb', line 79

def update
  target_id = ARGV.shift
  args = {name: ARGV.shift}
  if @visibility
    args[:visibility] = @visibility
  end
  p = api.update_package target_proj, target_id, args
  logger.info "Package #{target_id} has been updated."
  puts format_package(p)
end