From 2e785714c87b3a317853bdda3063b96507c34b60 Mon Sep 17 00:00:00 2001 From: icyleaf Date: Thu, 26 Apr 2018 15:07:06 +0800 Subject: [PATCH] refactor: arguments of cli and logger output --- Makefile | 4 +- docs/_coverpage.md | 2 +- docs/api.md | 16 ++++++-- docs/cli.md | 8 ++-- docs/en/_coverpage.md | 2 +- docs/install.md | 27 ++++++------- shard.yml | 2 +- src/cli.cr | 57 +++++++++++----------------- src/hpr/client.cr | 9 ++--- src/hpr/repository.cr | 11 ------ src/hpr/utils.cr | 5 +++ src/hpr/version.cr | 2 +- src/hpr/workers/clone_repository.cr | 2 +- src/hpr/workers/delete_repository.cr | 2 +- src/hpr/workers/update_repository.cr | 10 ++++- 15 files changed, 77 insertions(+), 82 deletions(-) diff --git a/Makefile b/Makefile index 86f8031..978fe6b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ hpr_image_name ?= icyleafcn/hpr -hpr_version ?= 0.4.0 +hpr_version ?= 0.5.0 all: build release publish @@ -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) diff --git a/docs/_coverpage.md b/docs/_coverpage.md index 152b78e..586b1fe 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,6 +1,6 @@ ![logo](_media/icon.png) -# ḫpr 0.4.0 +# ḫpr 0.5.0 > 镜像任意 git 仓库到 gitlab 的同步工具,具有静态加速、定时更新的功能。 diff --git a/docs/api.md b/docs/api.md index 8f592c1..054a865 100644 --- a/docs/api.md +++ b/docs/api.md @@ -181,7 +181,7 @@ GET /info ```json { "hpr": { - "version": "0.2.0", + "version": "0.5.0", "repositroies": { "total": 2, "entry": [ @@ -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" + } + ] } ``` diff --git a/docs/cli.md b/docs/cli.md index 823b84b..6d72db0 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -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 ``` diff --git a/docs/en/_coverpage.md b/docs/en/_coverpage.md index 0002eff..1c03c7b 100644 --- a/docs/en/_coverpage.md +++ b/docs/en/_coverpage.md @@ -1,6 +1,6 @@ ![logo](../_media/icon.png) -# ḫpr 0.4.0 +# ḫpr 0.5.0 > Mirror git repositories to self-host gitlab services. diff --git a/docs/install.md b/docs/install.md index 3033d83..25fc028 100644 --- a/docs/install.md +++ b/docs/install.md @@ -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: @@ -68,7 +68,7 @@ $ cd hpr $ shards build --release --no-debug ``` -### 运行 redis +### 运行 redis ```bash $ brew services start redis @@ -79,7 +79,7 @@ $ brew services start redis ```bash $ ./bin/hpr --help -Usage: hpr [--name=] [] +Usage: hpr [--url=] Actions: @@ -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: @@ -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 -``` \ No newline at end of file +hpr v0.5.0 in Crystal v0.24.2 +``` diff --git a/shard.yml b/shard.yml index 6e743ce..19f4b99 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: hpr -version: 0.4.0 +version: 0.5.0 authors: - icyleaf diff --git a/src/cli.cr b/src/cli.cr index a06c94b..0033e3b 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -15,8 +15,6 @@ module Hpr Delete end - NEDD_URL_FLAGS = ["-c", "--create"] - def initialize(args = ARGV) @client = Client.new @@ -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 @@ -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 } @@ -56,7 +50,7 @@ module Hpr o Start a API server: - $ hpr -s + $ hpr -s o List all mirrored repositories: @@ -68,19 +62,19 @@ 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 @@ -88,11 +82,7 @@ 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 @@ -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"]}" @@ -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 @@ -207,7 +194,7 @@ EXAMPLES end private def usage - "Usage: hpr [--name=] []" + "Usage: hpr [--url=] " end private def version diff --git a/src/hpr/client.cr b/src/hpr/client.cr index 53c86f8..0c4a56e 100644 --- a/src/hpr/client.cr +++ b/src/hpr/client.cr @@ -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 @@ -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 @@ -87,7 +86,7 @@ module Hpr end end - def reopsitory_stored?(name) + private def reopsitory_stored?(name) Dir.exists?(Utils.repository_path(name)) end diff --git a/src/hpr/repository.cr b/src/hpr/repository.cr index 31b3754..6bf724e 100644 --- a/src/hpr/repository.cr +++ b/src/hpr/repository.cr @@ -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("~", "") diff --git a/src/hpr/utils.cr b/src/hpr/utils.cr index 72430d9..67ab136 100644 --- a/src/hpr/utils.cr +++ b/src/hpr/utils.cr @@ -2,6 +2,11 @@ module Hpr module Utils extend self + def user_error!(message) + Hpr.logger.error message + raise Hpr::Error.new message + end + def current_datetime Time.now.to_s("%F %T %z") end diff --git a/src/hpr/version.cr b/src/hpr/version.cr index dff6434..d1dd7f1 100644 --- a/src/hpr/version.cr +++ b/src/hpr/version.cr @@ -1,3 +1,3 @@ module Hpr - VERSION = "0.4.0" + VERSION = "0.5.0" end diff --git a/src/hpr/workers/clone_repository.cr b/src/hpr/workers/clone_repository.cr index 2e7a43e..85d2fde 100644 --- a/src/hpr/workers/clone_repository.cr +++ b/src/hpr/workers/clone_repository.cr @@ -16,7 +16,7 @@ module Hpr repository_path = Hpr.config.repository_path Dir.cd repository_path - Hpr.logger.info "cloning from #{url} ... #{name}" + Hpr.logger.info "cloning #{url} ... #{name}" Utils.run_cmd "git clone --mirror #{url} #{name}" end diff --git a/src/hpr/workers/delete_repository.cr b/src/hpr/workers/delete_repository.cr index ba638c1..7daf193 100644 --- a/src/hpr/workers/delete_repository.cr +++ b/src/hpr/workers/delete_repository.cr @@ -3,7 +3,7 @@ module Hpr include Sidekiq::Worker def perform(name : String) - Hpr.logger.info "deleting folder ... #{name}" + Hpr.logger.info "deleting directory ... #{name}" FileUtils.rm_rf Utils.repository_path(name) end end diff --git a/src/hpr/workers/update_repository.cr b/src/hpr/workers/update_repository.cr index 9855740..b715fe0 100644 --- a/src/hpr/workers/update_repository.cr +++ b/src/hpr/workers/update_repository.cr @@ -5,10 +5,16 @@ module Hpr def perform(name : String) repository_path = Utils.repository_path(name) # Skip when repository id not exists (may be deleted). - return unless Dir.exists?(repository_path) + unless Dir.exists?(repository_path) + Hpr.logger.error "repository folder not exists ... #{name}" + return + end # Sikp when repository not exists at gitlab service(deleted remotely) - return unless project = search_project(name) + unless project = search_project(name) + Hpr.logger.error "repository of gitlab not exists ... #{name}" + return + end description = project["description"].to_s update_project_description(project, "[Syncing] #{description}")