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



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

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

Instance Method Details

#createObject



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

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



91
92
93
94
95
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
# File 'lib/osdn/cli/command/relfile.rb', line 91

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}..."
  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



146
147
148
149
150
151
152
153
154
155
# File 'lib/osdn/cli/command/relfile.rb', line 146

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
# 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"
end

#listObject



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

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



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

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],
  )
  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
    end
  end
end

#updateObject



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

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