Class: Gitcloudcli::Github

Inherits:
Git
  • Object
show all
Defined in:
lib/gitcloudcli/command/git/github.rb

Instance Method Summary collapse

Methods inherited from Git

#request

Constructor Details

#initialize(url, token) ⇒ Github

Returns a new instance of Github.



8
9
10
11
12
13
14
# File 'lib/gitcloudcli/command/git/github.rb', line 8

def initialize(url, token)
  uri = URI::parse(url)
  result = uri.path.to_s.split("/")
  @username=result[1]
  @repo=result[2].to_s.split(".")[0]
  @token=token
end

Instance Method Details

#delete(remote_path, message = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gitcloudcli/command/git/github.rb', line 78

def delete(remote_path, message=nil)
  info(remote_path) do |value|
    sha=value["sha"]
    paramters={
        "sha" => sha,
        "message" => message ? message : "remove #{remote_path}"
    }
    response = request(:DELETE, "https://api.github.com/repos/#{@username}/#{@repo}/contents/#{remote_path}?access_token=#{@token}", {"Content-Type" => "text/json"}, nil ,paramters.to_json)
    if response.code!="200"
      puts "删除失败 #{response.code} #{response.body}"
    else
      puts "删除 #{remote_path} 成功"
    end
  end
end

#info(remote_path, infos = []) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitcloudcli/command/git/github.rb', line 39

def info(remote_path, infos=[])
  response = request(:GET, "https://api.github.com/repos/#{@username}/#{@repo}/contents/#{remote_path}",nil, nil, nil)
  if response.code!="200"
    puts "查询失败 #{remote_path} #{response.code} #{response.body}"
  elsif block_given?
    yield JSON.parse(response.body)
  else
    result = JSON.parse(response.body)
    puts "#{result["name"]}   #{result["download_url"]}"
  end
end

#list(dir, infos = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitcloudcli/command/git/github.rb', line 16

def list(dir, infos=[])
  _dir = dir ? dir: ""
  response = request(:GET, "https://api.github.com/repos/#{@username}/#{@repo}/contents/#{_dir}", nil, nil, nil)
  if response.code != "200"
    puts "查询失败 #{dir} #{response.code} #{response.body}"
  elsif block_given?
    yield JSON.parse(response.body)
  else
    result = JSON.parse(response.body)
    result.each do |value|
      puts "每个文件的信息 #{value.keys}"
      break
    end
    result.each do |value|
      if value["type"]=="dir"
        puts "#{value["name"]}/ #{value.values_at(*infos)}"
      else
        puts "#{value["name"]}  #{value["download_url"]} #{value.values_at(*infos)}"
      end
    end
  end
end

#upload(local_path, path = nil, message = nil, infos = []) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gitcloudcli/command/git/github.rb', line 51

def upload(local_path, path=nil, message=nil, infos=[])
  file = open(local_path)
  content = Base64.encode64(file.read)
  path = path ? path : local_path.to_s.split("/").last
  message = message ? message : "upload #{path}"
  paramters={
      "content" => content,
      "message" => message
  }
  response = request(:PUT, "https://api.github.com/repos/#{@username}/#{@repo}/contents/#{path}?access_token=#{@token}", {"Content-Type" => "text/json"}, nil ,paramters.to_json)
  if response.code=="201"
    result = JSON.parse(response.body)
    puts "上传成功:#{path} #{local_path}"
    result["content"].each do |key, value|
      if infos and infos.length > 0
        if infos.include? key
          puts "#{key}: #{value}"
        end
      else
        puts "#{key}: #{value}"
      end
    end
  else
    puts "上传失败 #{response.code} #{response.body}"
  end
end