Switch 'refresh' command to new CLI module.

This commit is contained in:
Dominik Picheta 2016-12-19 23:35:27 +01:00
commit dcbbb8b62e
3 changed files with 22 additions and 12 deletions

View file

@ -1,6 +1,8 @@
# Copyright (C) Dominik Picheta. All rights reserved. # Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info. # BSD License. Look at license.txt for more info.
import system except TResult
import httpclient, parseopt, os, osproc, pegs, tables, parseutils, import httpclient, parseopt, os, osproc, pegs, tables, parseutils,
strtabs, json, algorithm, sets, uri strtabs, json, algorithm, sets, uri
@ -56,10 +58,10 @@ proc update(options: Options) =
"" ""
proc downloadList(list: PackageList, options: Options) = proc downloadList(list: PackageList, options: Options) =
echo("Downloading \"", list.name, "\" package list") display("Downloading", list.name & " package list", priority = HighPriority)
for i in 0 .. <list.urls.len: for i in 0 .. <list.urls.len:
let url = list.urls[i] let url = list.urls[i]
echo("Trying ", url, "...") display("Trying", url)
let tempPath = options.getNimbleDir() / "packages_temp.json" let tempPath = options.getNimbleDir() / "packages_temp.json"
# Grab the proxy # Grab the proxy
@ -67,21 +69,25 @@ proc update(options: Options) =
if not proxy.isNil: if not proxy.isNil:
var maskedUrl = proxy.url var maskedUrl = proxy.url
if maskedUrl.password.len > 0: maskedUrl.password = "***" if maskedUrl.password.len > 0: maskedUrl.password = "***"
echo("Using proxy ", maskedUrl) display("Connecting", "to proxy at " & $maskedUrl,
priority = LowPriority)
try: try:
downloadFile(url, tempPath, proxy = getProxy(options)) downloadFile(url, tempPath, proxy = getProxy(options))
except: except:
if i == <list.urls.len: if i == <list.urls.len:
raise raise
echo("Could not download: ", getCurrentExceptionMsg()) let message = "Could not download: " & getCurrentExceptionMsg()
display("Warning", message, Warning)
continue continue
if not validatePackagesList(tempPath): if not validatePackagesList(tempPath):
echo("Downloaded packages.json file is invalid, discarding.") let message = "Downloaded packages.json file is invalid, discarding."
display("Warning", message, Warning)
continue continue
copyFile(tempPath, copyFile(tempPath,
options.getNimbleDir() / "packages_$1.json" % list.name.toLowerAscii()) options.getNimbleDir() / "packages_$1.json" % list.name.toLowerAscii())
echo("Done.") display("Success", "Package list downloaded.", Success, HighPriority)
break break
if parameter.isUrl: if parameter.isUrl:

View file

@ -22,12 +22,14 @@ type
HighPriority, MediumPriority, LowPriority HighPriority, MediumPriority, LowPriority
DisplayType* = enum DisplayType* = enum
Error, Warning, Message Error, Warning, Message, Success
const const
longestCategory = len("Downloading") longestCategory = len("Downloading")
foregrounds: array[Error .. Message, ForegroundColor] = [fgRed, fgYellow, fgCyan] foregrounds: array[Error .. Success, ForegroundColor] =
styles: array[HighPriority .. LowPriority, set[Style]] = [{styleBright}, {}, {styleDim}] [fgRed, fgYellow, fgCyan, fgGreen]
styles: array[HighPriority .. LowPriority, set[Style]] =
[{styleBright}, {}, {styleDim}]
proc newCLI(): CLI = proc newCLI(): CLI =
result = CLI( result = CLI(

View file

@ -2,7 +2,7 @@
# BSD License. Look at license.txt for more info. # BSD License. Look at license.txt for more info.
import parsecfg, streams, strutils, os, tables, Uri import parsecfg, streams, strutils, os, tables, Uri
import tools, version, common import tools, version, common, cli
type type
Config* = object Config* = object
@ -50,13 +50,15 @@ proc parseConfig*(): Config =
var f = newFileStream(confFile, fmRead) var f = newFileStream(confFile, fmRead)
if f == nil: if f == nil:
# Try the old deprecated babel.ini # Try the old deprecated babel.ini
# TODO: This can be removed.
confFile = getConfigDir() / "babel" / "babel.ini" confFile = getConfigDir() / "babel" / "babel.ini"
f = newFileStream(confFile, fmRead) f = newFileStream(confFile, fmRead)
if f != nil: if f != nil:
echo("[Warning] Using deprecated config file at ", confFile) display("Warning", "Using deprecated config file at " & confFile,
displayType = Warning)
if f != nil: if f != nil:
echo("Reading from config file at ", confFile) display("Reading", "config file at " & confFile)
var p: CfgParser var p: CfgParser
open(p, f, confFile) open(p, f, confFile)
var currentSection = "" var currentSection = ""