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



23
24
25
# File 'lib/osdn/cli/command/relfile.rb', line 23

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

Instance Method Details

#createObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osdn/cli/command/relfile.rb', line 84

def create
  if ARGV.empty? || ARGV.first == ""
    logger.fatal "Target filename is missing."
    help
    return
  end

  ARGV.each do |f|
    create_one(f)
  end
end

#create_one(filename) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/osdn/cli/command/relfile.rb', line 96

def create_one(filename)
  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 && @show_progress != false || @show_progress and
    OSDN::CLI._show_progress = true
  logger.info "Starting upload #{file}..."
  max_upload_tries = 5
  upload_tries = 0
  f = nil
  begin
    upload_tries += 1
    f = api.create_release_file target_proj, target_package, target_release, fio, visibility: @visibility
  rescue OSDNClient::ApiError => e
    if max_upload_tries - upload_tries <= 0 
      logger.error "Max upload attempts (#{max_upload_tries}) has been exceeded, give up!"
      raise e
    elsif [0, 100, 502].member?(e.code.to_i)
      fio.rewind
      logger.error "Upload error (#{e.code} #{e.message}), retrying (#{upload_tries}/#{max_upload_tries})..."
      sleep 10
      retry
    else
      raise e
    end
  ensure
    fio.close
    OSDN::CLI._show_progress = false
  end
  
  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



171
172
173
174
175
176
177
178
179
180
# File 'lib/osdn/cli/command/relfile.rb', line 171

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

def help
  puts "#{$0} relfile [opts] [list]"
  puts "#{$0} relfile [opts] create <target-file> [target-files...]"
  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=<package-id>  Target package (numeric id)"
  puts "     --release=<release-id>  Target release (numeric id)"
  puts "  -v --visibility=<public|private|hidden>"
  puts "      --force-digest         Calc local file digest forcely"
  puts "      --progress             Force to show upload progress"
  puts "      --no-progress          Force to hide upload progress"
  puts "      --bwlimit=RATE         Limit bandwidth (in KB)"
end

#listObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/osdn/cli/command/relfile.rb', line 72

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



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
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/osdn/cli/command/relfile.rb', line 27

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],
    [ '--progress', GetoptLong::NO_ARGUMENT],
    [ '--no-progress', GetoptLong::NO_ARGUMENT],
    [ '--bwlimit', 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 '--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
    when '--progress'
      @show_progress = true
    when '--no-progress'
      @show_progress = false
    when '--bwlimit'
      arg.to_i != 0 and
        OSDN::CLI._rate_limit = arg.to_i * 1024
    end
  end
end

#updateObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/osdn/cli/command/relfile.rb', line 155

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