matt/src/cmd/sync.c
2024-07-16 20:37:46 +03:00

63 lines
1.4 KiB
C

#include <libmp/all.h>
#include <libmp/error.h>
#include <stdio.h>
#include "../cmd.h"
#include "../log.h"
bool cmd_sync_callback(lm_ctx_t *ctx, lm_pool_t *pool, lm_ctx_sync_state_t state, size_t current, size_t total, void *data){
switch (state) {
case SYNC_DOWNLOADING_INFO:
bar(current, total);
break;
case SYNC_INFO_SUCCESS:
bar_free();
break;
case SYNC_INFO_FAIL:
bar_free();
error(_("Failed to sync "FG_BOLD"%s"FG_RESET": %s"), pool->name, lm_strerror());
break;
case SYNC_DOWNLOADING_LIST:
bar(current, total);
break;
case SYNC_LIST_SUCCESS:
bar_free();
success(_("Synced "FG_BOLD"%s [%s]"FG_RESET), pool->name, pool->info.pubkey);
break;
case SYNC_LIST_FAIL:
bar_free();
if(LM_ERR_InfoNotLoaded != lm_error())
error(_("Failed to sync %s: %s"), pool->name, lm_strerror());
break;
}
return true;
}
bool cmd_sync(lm_ctx_t *ctx, config_t *config, args_t *args){
size_t sycned = 0;
bool ret = false;
if(0 == config->pool_count){
error(_("There are no pools specified in the configuration"));
goto end;
}
if((sycned = lm_ctx_sync(ctx, true, cmd_sync_callback, NULL) < 0)){
error(_("Failed to sync pools: %s"), lm_strerror());
goto end;
}
info(_("Synced "FG_BOLD"%d/%d"FG_RESET" pools"), sycned, config->pool_count);
ret = true;
end:
bar_free();
return ret;
}