configs.nim 3.4 KB
Newer Older
Nong Hoang Tu's avatar
Nong Hoang Tu committed
1
import logging
Nong Hoang Tu's avatar
Nong Hoang Tu committed
2
import os
Nong Hoang Tu's avatar
Nong Hoang Tu committed
3
4
5
import json
import strutils
import times
Nong Hoang Tu's avatar
Nong Hoang Tu committed
6
7
8
9
10

type
  Err* = enum
    updated,
    outdated,
Nong Hoang Tu's avatar
Nong Hoang Tu committed
11
12
13
    updateSuccess,
    checkVersionErr,
    failed
14
15
16
  LogHandler* = object
    print*: ConsoleLogger
    save*: FileLogger
Nong Hoang Tu's avatar
Nong Hoang Tu committed
17
  DebPackage* = object
18
    log*: LogHandler
Nong Hoang Tu's avatar
Nong Hoang Tu committed
19
20
    name*: string # name of project
    path*: string # full project path
Nong Hoang Tu's avatar
Nong Hoang Tu committed
21
22
23
  ProgConf* = object
    rootPath*: string
    logPath*: string
24
25
    uscanPath*: string
    gbpPath*: string
Nong Hoang Tu's avatar
Nong Hoang Tu committed
26
27
28
    gitUrl*: string
    devName*: string
    devMail*: string
Nong Hoang Tu's avatar
Nong Hoang Tu committed
29
30

# TODO variables for config parser, logger
31
var mainLog = newConsoleLogger(fmtStr="$levelname [$date $time] ")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
32

33
34
35
36
37
38
39
proc checkPrograms(gbpPath, uscanPath: string): bool =
  if not fileExists(gbpPath):
    mainLog.log(lvlError, gbpPath & " not found. Try install git-buildpackage")
    return false
  if not fileExists(uscanPath):
    mainLog.log(lvlError, uscanPath & " not found. Try install uscan")
    return false
Nong Hoang Tu's avatar
Nong Hoang Tu committed
40
41
  return true

Nong Hoang Tu's avatar
Nong Hoang Tu committed
42
43
44
45
46
47
48
49
50

proc dirName*(name: string): string =
  #[
    Add slash for folder name
  ]#
  result = if not name.endsWith("/"): name & "/" else: name


proc initConfig*(config: var ProgConf): bool =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
51
52
53
  #[
    Read the config from update file and generate log
  ]#
Nong Hoang Tu's avatar
Nong Hoang Tu committed
54
55
56
57
58
59
60
  const path = "config.json" # TODO edit here
  try:
    let
      conf = parseFile(path)
      rootFolder = conf["root_path"].getStr()
      logFolder = conf["log_path"].getStr()
      gitUrl = conf["git_url"].getStr()
61
62
63
      gbpPath = conf["gbp_path"].getStr()
      uscanPath = conf["uscan_path"].getStr()

64
65
66
67
68
69
70
71
72
73
74
75
76
77
    # Check value of gbp path
    if isEmptyOrWhitespace(gbpPath):
      mainLog.log(lvlInfo, "gbp path is empty! Use default value.")
      config.gbpPath = "/usr/bin/gbp"
    else:
      config.gbpPath = gbpPath
    
    # Check value of uscan path
    if isEmptyOrWhitespace(uscanPath):
      mainLog.log(lvlInfo, "uscan path is empty! Use default value.")
      config.uscanPath = "/usr/bin/gbp"
    else:
      config.uscanPath = uscanPath
 
78
    # Check if gbp and uscan is available
79
    if not checkPrograms(config.gbpPath, uscanPath):
80
81
82
      return false

    # Check if root folder of projects is available
Nong Hoang Tu's avatar
Nong Hoang Tu committed
83
    if isEmptyOrWhitespace(rootFolder):
84
      config.rootPath = dirName(parentDir(paramStr(0)))
Nong Hoang Tu's avatar
Nong Hoang Tu committed
85
86
87
    else:
      config.rootPath = dirName(rootFolder)
    
88
    # Create new log folder as day
Nong Hoang Tu's avatar
Nong Hoang Tu committed
89
90
    let logDate = now().format("dd-MM-yyyy")
    if isEmptyOrWhitespace(logFolder):
91
      config.logPath = dirName(parentDir(paramStr(0)).dirName() & logDate)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
92
93
    else:
      config.logPath = dirName(logFolder.dirName() & logDate)
94
    
Nong Hoang Tu's avatar
Nong Hoang Tu committed
95
96
    if not dirExists(config.logPath):
      createDir(config.logPath)
97
    # Check for valid URL
Nong Hoang Tu's avatar
Nong Hoang Tu committed
98
    if gitUrl.startSwith("http"):
99
      mainLog.log(lvlError, "HTTP protocol is not supported. Please use SSH URL!")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
100
      return false
101
102
    elif isEmptyOrWhitespace(gitUrl):
      mainLog.log(lvlError, "URL of git repository must be not empty.")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
103
104
    else:
      config.gitUrl = gitURL
105
106
    
    # Check name and email of maintainer
Nong Hoang Tu's avatar
Nong Hoang Tu committed
107
108
    if isEmptyOrWhitespace(conf["dev_name"].getStr()) or
      isEmptyOrWhitespace(conf["dev_mail"].getStr()):
109
110
        mainLog.log(lvlError, "Name and email of maintainer must be not empty!")
        return false
Nong Hoang Tu's avatar
Nong Hoang Tu committed
111
112
113
    else:
      config.devName = conf["dev_name"].getStr()
      config.devMail = conf["dev_mail"].getStr()
114
115
    mainLog.log(lvlInfo, "Load configuration completed.")
    return true
Nong Hoang Tu's avatar
Nong Hoang Tu committed
116
  except:
117
    mainLog.log(lvlError, "Unexpected error while reading configurations.")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
118
    return false