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

Inherits:
FrsBase 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 FrsBase

#run

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



16
17
18
# File 'lib/osdn/cli/command/package.rb', line 16

def self.description
  "Manipulate frs packages of project"
end

Instance Method Details

#_set_package_idObject



88
89
90
91
# File 'lib/osdn/cli/command/package.rb', line 88

def _set_package_id
  return if ARGV[0].to_i == 0
  @target_package = ARGV[0].to_i
end

#createObject



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

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



82
83
84
85
86
# File 'lib/osdn/cli/command/package.rb', line 82

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

#helpObject



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

def 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

#listObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/osdn/cli/command/package.rb', line 44

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

#process_optionsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/osdn/cli/command/package.rb', line 20

def process_options
  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
end

#updateObject



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

def update
  _set_package_id
  args = {}
  if ARGV[1].to_s != ""
    args[:name] = ARGV[1]
  end
  if @visibility
    args[:visibility] = @visibility
  end
  logger.debug "Updating package #{target_package} as #{args.inspect}"
  p = api.update_package target_proj, target_package, args
  logger.info "Package #{target_package} has been updated."
  puts format_package(p)
end