Skip to content

Commit

Permalink
refactor: arguments of cli and logger output
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Apr 26, 2018
1 parent 7660c04 commit 2e78571
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 82 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
hpr_image_name ?= icyleafcn/hpr
hpr_version ?= 0.4.0
hpr_version ?= 0.5.0

all: build release publish

Expand All @@ -15,7 +15,7 @@ release: ## Docker release image
docker build --no-cache -t $(hpr_image_name):$(hpr_version) .
rm -rf hpr deps

publish:
publish:
docker tag $(hpr_image_name):$(hpr_version) $(hpr_image_name):latest
docker push $(hpr_image_name):latest
docker push $(hpr_image_name):$(hpr_version)
Expand Down
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![logo](_media/icon.png)

# ḫpr <small>0.4.0</small>
# ḫpr <small>0.5.0</small>

> 镜像任意 git 仓库到 gitlab 的同步工具,具有静态加速、定时更新的功能。
Expand Down
16 changes: 13 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ GET /info
```json
{
"hpr": {
"version": "0.2.0",
"version": "0.5.0",
"repositroies": {
"total": 2,
"entry": [
Expand All @@ -191,13 +191,23 @@ GET /info
}
},
"jobs": {
"total_scheduled": 7,
"total_scheduled": 2,
"total_enqueued": 0,
"total_failures": 0,
"total_processed": 111,
"total_queues": {
"default": 0
}
}
},
"scheduleds": [
{
"name": "project1",
"scheduled_at": "2018-04-28 15:47:48 UTC"
},
{
"name": "project2",
"scheduled_at": "2018-04-28 20:47:48 UTC"
}
]
}
```
8 changes: 5 additions & 3 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ Here are 4 mirrored repositories:
## 创建镜像仓库

```bash
$ hpr -c --name icyleaf-salt https://github.com/icyleaf/salt.git
$ hpr --create --url https://github.com/icyleaf/salt.git icyleaf-salt
# or
$ hpr -c -U https://github.com/icyleaf/salt.git
```

## 更新镜像仓库

```bash
$ hpr -u --name icyleaf-salt
$ hpr -u icyleaf-salt
```

## 删除镜像仓库

```bash
$ hpr -d --name icyleaf-salt
$ hpr -d icyleaf-salt
```
2 changes: 1 addition & 1 deletion docs/en/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![logo](../_media/icon.png)

# ḫpr <small>0.4.0</small>
# ḫpr <small>0.5.0</small>

> Mirror git repositories to self-host gitlab services.
Expand Down
27 changes: 12 additions & 15 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ hpr 使用 Crystal 编写的工具可以被安装在 macOS、Linux、树莓派

## Docker

> 需要拉取 [hpr]()[redis]() 两个镜像。
> 需要拉取 [hpr](https://hub.docker.com/r/icyleafcn/hpr)[redis](https://hub.docker.com/_/redis) 两个镜像。
获取指定版本的 hpr:

```bash
$ docker pull icyleafcn/hpr:0.4.0
$ docker pull icyleafcn/hpr:0.5.0
```

或者获取最新版本的 hpr:
Expand Down Expand Up @@ -68,7 +68,7 @@ $ cd hpr
$ shards build --release --no-debug
```

### 运行 redis
### 运行 redis

```bash
$ brew services start redis
Expand All @@ -79,7 +79,7 @@ $ brew services start redis

```bash
$ ./bin/hpr --help
Usage: hpr <action> [--name=<name>] [<url>]
Usage: hpr <action> [--url=<url>] <name>

Actions:

Expand All @@ -95,11 +95,8 @@ Option in server action:

Option in create action:

--mirror-only Only mirror the repository without clone in create action

Option in create/update/delete action:

--name NAME The name of mirror repository
-U URL, --url URL The url of mirror repository
-M, --mirror-only Only mirror the repository without clone in create action

Global options:

Expand All @@ -122,21 +119,21 @@ Examples:

o Create a new repository:

$ hpr -c --name "icyleaf-hpr" https://github.com/icyleaf/hpr.git
$ hpr -c --url https://github.com/icyleaf/hpr.git icyleaf-hpr

o Clone and push a new repository without create gitlab project:

$ hpr -c --mirror-only --name "icyleaf-hpr" https://github.com/icyleaf/hpr.git
$ hpr -c --mirror-only --url https://github.com/icyleaf/hpr.git icyleaf-hpr

o Update a repository:

$ hpr -u --name "icyleaf-hpr"
$ hpr -u icyleaf-hpr

o Delete a repository:

$ hpr -d --name "icyleaf-hpr"
$ hpr -d icyleaf-hpr

More detail to check: https://icyleaf.github.io/hpr/

hpr v0.4.0 in Crystal v0.24.2
```
hpr v0.5.0 in Crystal v0.24.2
```
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hpr
version: 0.4.0
version: 0.5.0

authors:
- icyleaf <icyleaf.cn@gmail.com>
Expand Down
57 changes: 22 additions & 35 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ module Hpr
Delete
end

NEDD_URL_FLAGS = ["-c", "--create"]

def initialize(args = ARGV)
@client = Client.new

Expand All @@ -26,8 +24,6 @@ module Hpr
@mirror_only = false
@server_port = 8848

need_flags = args.select { |v| NEDD_URL_FLAGS.includes?(v) }.size > 0

parser = OptionParser.parse(args) do |parser|
parser.banner = usage

Expand All @@ -42,10 +38,8 @@ module Hpr
parser.on("-P PORT", "--port PORT", "the port of server (by default is 8848)") { |port| @server_port = port.to_i }

parser.separator("\nOption in create action:\n")
parser.on("--mirror-only", "Only mirror the repository without clone in create action") { @mirror_only = true }

parser.separator("\nOption in create/update/delete action:\n")
parser.on("--name NAME", "The name of mirror repository") { |name| @repo_name = name }
parser.on("-U URL", "--url URL", "The url of mirror repository") { |url| @repo_url = url }
parser.on("-M", "--mirror-only", "Only mirror the repository without clone in create action") { @mirror_only = true }

parser.separator("\nGlobal options:\n")
parser.on("-v", "--version", "Show version") { puts version }
Expand All @@ -56,7 +50,7 @@ module Hpr
o Start a API server:
$ hpr -s
$ hpr -s
o List all mirrored repositories:
Expand All @@ -68,31 +62,27 @@ module Hpr
o Create a new repository:
$ hpr -c --name "icyleaf-hpr" https://github.com/icyleaf/hpr.git
$ hpr -c --url https://github.com/icyleaf/hpr.git icyleaf-hpr
o Clone and push a new repository without create gitlab project:
$ hpr -c --mirror-only --name "icyleaf-hpr" https://github.com/icyleaf/hpr.git
$ hpr -c --mirror-only --url https://github.com/icyleaf/hpr.git icyleaf-hpr
o Update a repository:
$ hpr -u --name "icyleaf-hpr"
$ hpr -u icyleaf-hpr
o Delete a repository:
$ hpr -d --name "icyleaf-hpr"
$ hpr -d icyleaf-hpr
More detail to check: https://icyleaf.github.io/hpr/
EXAMPLES

parser.separator("\n#{version}")

parser.unknown_args do |unknown_args|
if need_flags
raise Error.new("Missing url argument.") if unknown_args.size.zero?

@repo_url = unknown_args.first
end
@repo_name = unknown_args.first if unknown_args.size > 0
end
end

Expand Down Expand Up @@ -121,22 +111,21 @@ EXAMPLES
obj << Utils.repository_info(name) if Utils.repository_path?(name)
end

puts "Here are #{repositories.size} mirrored repositories:\n"
Hpr.logger.info "listing repositories (#{repositories.size}):"
@client.list_repositories.each do |repository|
puts "* #{repository}"
end
end

private def create_repository
start_worker
sleep 100.milliseconds # waiting sidekiq is ready
Utils.user_error! "Missing url argument." if @repo_url.empty?

@repo_name = Utils.project_name(@repo_url) if @repo_name.empty?
if Utils.repository_path?(@repo_name)
project_path = Utils.repository_path(@repo_name)
project_info = Utils.repository_info(@repo_name)

puts "repository was exists ... #{@repo_name}"
Hpr.logger.info "repository exists ... #{@repo_name}"
puts "* path: #{project_path}"
puts "* original url: #{project_info["url"]}"
puts "* mirror url: #{project_info["mirror_url"]}"
Expand All @@ -148,49 +137,47 @@ EXAMPLES
exit
end

start_worker
sleep 100.milliseconds # waiting sidekiq is ready

@client.create_repository(@repo_url, @repo_name, @mirror_only)
print "* repository is creating "
loop do
sleep 1.seconds
print "."

if !Utils.repository_cloning?(@repo_name) &&
(info = Utils.repository_info(@repo_name)) &&
!info["updated_at"].empty?
break
end
end
puts " [done]"
Hpr.logger.info "create repository ... done"
end

private def update_repository
Utils.user_error! "Missing name argument." if @repo_name.empty?

start_worker
sleep 1.seconds # waiting sidekiq is ready
@client.update_repository(@repo_name)

print "* repository is updating "
loop do
sleep 1.seconds
print "."

break unless Utils.repository_updating?(@repo_name)
end
puts " [done]"
Hpr.logger.info "update repository ... done"
end

private def delete_repository
Utils.user_error! "Missing name argument." if @repo_name.empty?

start_worker
sleep 1.seconds # waiting sidekiq is ready

print "* repository is deleting "
@client.delete_repository(@repo_name)
loop do
sleep 1.seconds
print "."

break unless Utils.repository_path?(@repo_name)
end
puts " [done]"
Hpr.logger.info "delete repository ... done"
end

private def start_server
Expand All @@ -207,7 +194,7 @@ EXAMPLES
end

private def usage
"Usage: hpr <action> [--name=<name>] [<url>]"
"Usage: hpr <action> [--url=<url>] <name>"
end

private def version
Expand Down
9 changes: 4 additions & 5 deletions src/hpr/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module Hpr
repo = Repository.new url
project_name = (name && !name.empty?) ? name : repo.mirror_name

raise RepositoryExistsError.new "Exists Repository: #{project_name}" if reopsitory_stored?(project_name)
Utils.user_error! "Exists Repository: #{project_name}" if reopsitory_stored?(project_name)

Hpr.logger.info "creating repository in gitlab ... #{@group["name"]}/#{project_name}"
Hpr.logger.info "creating repository ... #{@group["name"]}/#{project_name}"

loop do
begin
Expand Down Expand Up @@ -62,8 +62,7 @@ module Hpr

def update_repository(name : String)
unless reopsitory_stored?(name)
Hpr.logger.error "repository not exists ... #{name}"
raise NotFoundRepositoryError.new "Not found repository: #{name}"
Utils.user_error! "repository not exists ... #{name}"
end

UpdateRepositoryWorker.async.perform name
Expand All @@ -87,7 +86,7 @@ module Hpr
end
end

def reopsitory_stored?(name)
private def reopsitory_stored?(name)
Dir.exists?(Utils.repository_path(name))
end

Expand Down
11 changes: 0 additions & 11 deletions src/hpr/repository.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ module Hpr
end
end

private def extract_name_and_user(path : String)
namespace = if paths.size >= 2
strip_tail paths[-2]
else
""
end
name = strip_tail paths.last

[namespace, name]
end

private def strip_tail(text : String)
text.gsub(".git", "")
.gsub("~", "")
Expand Down
Loading

0 comments on commit 2e78571

Please sign in to comment.