Class: OSDN::CLI::Command::Relfile

Inherits:
FrsBase show all
Defined in:
lib/osdn/cli/command/relfile.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



20
21
22
# File 'lib/osdn/cli/command/relfile.rb', line 20

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

Instance Method Details

#createObject



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
# File 'lib/osdn/cli/command/relfile.rb', line 71

def create
  filename = ARGV.shift
  if !filename
    logger.fatal "Target filename is missing."
    help
    return
  end
  file = Pathname('.') + filename
  logger.debug "Calculating digest for #{file}..."

  vars = load_variables(file.dirname)
  digests = nil
  if !@force_digest && vars.local_file_info &&
     vars.local_file_info[file.basename.to_s]
    finfo = vars.local_file_info[file.basename.to_s]
    if finfo[:size] == file.size && finfo.mtime == file.mtime
      digests = vars.local_file_info[file.basename.to_s].digests
    end
  end

  unless digests
    logger.info "Calculating digest for #{file}..."
    digests = {
      sha256: hexdigest(Digest::SHA256, file),
      sha1:   hexdigest(Digest::SHA1, file),
      md5:    hexdigest(Digest::MD5, file),
    }
    update_variables file.dirname, {local_file_info: {file.basename.to_s => {digests: digests, mtime: file.mtime, size: file.size}}}
  end

  fio = file.open
  logger.level <= Logger::INFO and
    OSDN::CLI._show_progress = true
  logger.info "Starting upload #{file}..."
  f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility
  fio.close
  OSDN::CLI._show_progress = false
  if digests.find { |type, dig| dig != f.send("digest_#{type}") }
    logger.error "File digests are mismatch! Upload file #{file} may be broken! Please check."
  else
    logger.info "Upload complete."
  end
  puts format_file(f)
end

#deleteObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/osdn/cli/command/relfile.rb', line 132

def delete
  target_id = ARGV.shift
  if !target_id
    logger.fatal "Target file ID is missing."
    help
    return
  end
  f = api.delete_release_file target_proj, target_package, target_release, target_id
  logger.info "file #{target_id} has been deleted."
end

#helpObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/osdn/cli/command/relfile.rb', line 6

def help
  puts "#{$0} relfile [opts] [list]"
  puts "#{$0} relfile [opts] create <target-file>"
  puts "#{$0} relfile [opts] update <numeric-file-id>"
  puts "#{$0} relfile [opts] delete <numeric-file-id>"
  puts "Options:"
  puts "  -f --format=<pretty|json>  Set output format"
  puts "  -p --project=<project>     Target project (numeric id or name)"
  puts "     --package=<project>     Target package (numeric id)"
  puts "     --release=<project>     Target release (numeric id)"
  puts "  -v --visibility=<public|private|hidden>"
  puts "      --force-digest         Calc local file digest forcely"
end

#listObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/osdn/cli/command/relfile.rb', line 59

def list
  release = api.get_release target_proj, target_package, target_release
  list = release.files
  if format == 'json'
    puts list.map{|i| i.to_hash}.to_json
  else
    list.each do |f|
      puts format_file(f)
    end
  end
end

#process_optionsObject



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
55
56
57
# File 'lib/osdn/cli/command/relfile.rb', line 24

def process_options
  opts = GetoptLong.new(
    [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--package', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--release', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--visibility', '-v', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--force-digest', GetoptLong::NO_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 '--package'
      arg.empty? or
        @target_package = arg
    when '--release'
      arg.empty? or
        @target_release = arg
    when '--force-digest'
      @force_digest = true
    when '--visibility'
      unless %w(public private hidden).member?(arg)
        logger.fatal "Invalid visibility status: #{arg}"
        exit
      end
      @visibility = arg
    end
  end
end

#updateObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/osdn/cli/command/relfile.rb', line 116

def update
  target_id = ARGV.shift
  if !target_id
    logger.fatal "Target file ID is missing."
    help
    return
  end
  if @visibility.nil?
    logger.fatal "Visibility status is missing. Use '-v <public|private|hidden>'."
    return
  end
  f = api.update_release_file target_proj, target_package, target_release, target_id, visibility: @visibility
  logger.info "file #{target_id} has been updated."
  puts format_file(f)
end