Coverage for src/cli/main.py: 95.10%

102 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 23:50 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from argparse import ( 

5 _ArgumentGroup, 

6 _MutuallyExclusiveGroup, 

7 ArgumentParser, 

8 Namespace, 

9 RawTextHelpFormatter, 

10) 

11from os import environ 

12from shutil import get_terminal_size 

13from sys import exit as sys_exit 

14 

15# Components 

16from ..package.bundle import Bundle 

17from ..package.settings import Settings 

18from ..package.updates import Updates 

19from ..package.version import Version 

20from ..prints.colors import Colors 

21from ..system.platform import Platform 

22from ..types.environments import Environments 

23from ..types.gitlab import MilestoneState 

24from .entrypoint import Entrypoint 

25 

26# Constants 

27HELP_POSITION: int = 37 

28 

29# Main, pylint: disable=too-many-branches,too-many-statements 

30def main() -> None: 

31 

32 # Variables 

33 environments: Environments 

34 group: _ArgumentGroup 

35 result: Entrypoint.Result = Entrypoint.Result.ERROR 

36 subgroup: _MutuallyExclusiveGroup 

37 

38 # Configure environment variables 

39 environments = Environments() 

40 environments.group = 'environment variables' 

41 environments.add( 

42 'gitlab_token', 

43 Bundle.ENV_GITLAB_TOKEN, 

44 'GitLab API token environment variable', 

45 ) 

46 environments.add( 

47 'ci_job_token', 

48 Bundle.ENV_CI_JOB_TOKEN, 

49 'GitLab CI job token environment variable (CI only)', 

50 ) 

51 

52 # Arguments creation 

53 parser: ArgumentParser = ArgumentParser( 

54 prog=Bundle.NAME, 

55 description=f'{Bundle.NAME}: {Bundle.DESCRIPTION}', 

56 epilog=environments.help(HELP_POSITION), 

57 add_help=False, 

58 formatter_class=lambda prog: RawTextHelpFormatter( 

59 prog, 

60 max_help_position=HELP_POSITION, 

61 width=min( 

62 120, 

63 get_terminal_size().columns - 2, 

64 ), 

65 ), 

66 ) 

67 

68 # Arguments internal definitions 

69 group = parser.add_argument_group('internal arguments') 

70 group.add_argument( 

71 '-h', 

72 '--help', 

73 dest='help', 

74 action='store_true', 

75 help='Show this help message', 

76 ) 

77 group.add_argument( 

78 '--version', 

79 dest='version', 

80 action='store_true', 

81 help='Show the current version', 

82 ) 

83 group.add_argument( 

84 '--no-color', 

85 dest='no_color', 

86 action='store_true', 

87 help=f'Disable colors outputs with \'{Bundle.ENV_NO_COLOR}=1\'\n' 

88 '(or default settings: [themes] > no_color)', 

89 ) 

90 group.add_argument( 

91 '--update-check', 

92 dest='update_check', 

93 action='store_true', 

94 help='Check for newer package updates', 

95 ) 

96 group.add_argument( 

97 '--settings', 

98 dest='settings', 

99 action='store_true', 

100 help='Show the current settings path and contents', 

101 ) 

102 group.add_argument( 

103 '--set', 

104 dest='set', 

105 action='store', 

106 metavar=('GROUP', 'KEY', 'VAL'), 

107 nargs=3, 

108 help='Set settings specific \'VAL\' value to [GROUP] > KEY\n' \ 

109 'or unset by using \'UNSET\' as \'VAL\'', 

110 ) 

111 

112 # Arguments credentials definitions 

113 group = parser.add_argument_group('credentials arguments') 

114 group.add_argument( 

115 '-c', 

116 '--config', 

117 dest='configs', 

118 action='append', 

119 metavar='FILES', 

120 help=f'Python GitLab configuration files' 

121 f' (default: {Bundle.ENV_PYTHON_GITLAB_CFG} environment)', 

122 ) 

123 

124 # Arguments common definitions 

125 group = parser.add_argument_group('common arguments') 

126 group.add_argument( 

127 '--dump', 

128 dest='dump', 

129 action='store_true', 

130 help='Dump Python objects of projects', 

131 ) 

132 

133 # Arguments issues definitions 

134 group = parser.add_argument_group('issues arguments') 

135 group.add_argument( 

136 '--default-estimate', 

137 dest='default_estimate', 

138 default='8', 

139 action='store', 

140 metavar='ESTIMATE', 

141 help='Default issue time estimate if none provided' 

142 'in hours (default: %(default)s)', 

143 ) 

144 group.add_argument( 

145 '--exclude-closed-issues', 

146 dest='exclude_closed_issues', 

147 action='store_true', 

148 help='Exclude issues in closed state', 

149 ) 

150 

151 # Arguments milestones definitions 

152 group = parser.add_argument_group('milestones arguments') 

153 group.add_argument( 

154 '--milestone', 

155 dest='milestone', 

156 action='store', 

157 metavar='MILESTONE', 

158 help='Use a specific milestone by name, by ID, or "None"', 

159 ) 

160 subgroup = group.add_mutually_exclusive_group() 

161 subgroup.add_argument( 

162 '--create-milestone', 

163 dest='create_milestone', 

164 action='store_true', 

165 help='Create a new milestone', 

166 ) 

167 subgroup.add_argument( 

168 '--get-milestone', 

169 dest='get_milestone', 

170 action='store_true', 

171 help='Get existing milestone', 

172 ) 

173 subgroup.add_argument( 

174 '--milestones-statistics', 

175 dest='milestones_statistics', 

176 action='store_true', 

177 help='Inject milestones statistics into milestones description', 

178 ) 

179 group.add_argument( 

180 '--exclude-closed-milestones', 

181 dest='exclude_closed_milestones', 

182 action='store_true', 

183 help='Exclude milestones in closed state', 

184 ) 

185 group.add_argument( 

186 '--ignore-issues-without-milestone', 

187 dest='ignore_issues_without_milestone', 

188 action='store_true', 

189 help='Ignore issues without a milestone', 

190 ) 

191 group.add_argument( 

192 '--set-milestone-description', 

193 dest='set_milestone_description', 

194 action='store', 

195 metavar='TEXT', 

196 help='Set milestone description', 

197 ) 

198 group.add_argument( 

199 '--set-milestone-state', 

200 dest='set_milestone_state', 

201 action='store', 

202 metavar='STATE', 

203 help=f'Set milestone state [{",".join(MilestoneState.names())}]', 

204 ) 

205 group.add_argument( 

206 '--set-milestone-start-date', 

207 dest='set_milestone_start_date', 

208 action='store', 

209 metavar='DATE', 

210 help='Set milestone start date', 

211 ) 

212 group.add_argument( 

213 '--set-milestone-due-date', 

214 dest='set_milestone_due_date', 

215 action='store', 

216 metavar='DATE', 

217 help='Set milestone due date', 

218 ) 

219 

220 # Arguments positional definitions 

221 group = parser.add_argument_group('positional arguments') 

222 group.add_argument( 

223 '--', 

224 dest='double_dash', 

225 action='store_true', 

226 help='Positional arguments separator (recommended)', 

227 ) 

228 group.add_argument( 

229 dest='url_path', 

230 action='store', 

231 nargs='?', 

232 help='GitLab project path URL', 

233 ) 

234 

235 # Arguments parser 

236 options: Namespace = parser.parse_args() 

237 

238 # Help informations 

239 if options.help: 

240 print(' ') 

241 parser.print_help() 

242 print(' ') 

243 Platform.flush() 

244 sys_exit(0) 

245 

246 # Instantiate settings 

247 settings: Settings = Settings(name=Bundle.NAME) 

248 

249 # Prepare no_color 

250 if not options.no_color: 

251 if settings.has('themes', 'no_color'): 

252 options.no_color = settings.get_bool('themes', 'no_color') 

253 else: 

254 options.no_color = False 

255 settings.set_bool('themes', 'no_color', options.no_color) 

256 

257 # Configure no_color 

258 if options.no_color: 

259 environ[Bundle.ENV_FORCE_COLOR] = '0' 

260 environ[Bundle.ENV_NO_COLOR] = '1' 

261 

262 # Prepare colors 

263 Colors.prepare() 

264 

265 # Settings setter 

266 if options.set: 

267 settings.set(options.set[0], options.set[1], options.set[2]) 

268 settings.show() 

269 sys_exit(0) 

270 

271 # Settings informations 

272 if options.settings: 

273 settings.show() 

274 sys_exit(0) 

275 

276 # Instantiate updates 

277 updates: Updates = Updates( 

278 name=Bundle.PACKAGE, 

279 settings=settings, 

280 ) 

281 

282 # Version informations 

283 if options.version: 

284 print( 

285 f'{Bundle.NAME} {Version.get()} from {Version.path()} (python {Version.python()})' 

286 ) 

287 Platform.flush() 

288 sys_exit(0) 

289 

290 # Check for current updates 

291 if options.update_check: 

292 if not updates.check(): 

293 updates.check(older=True) 

294 sys_exit(0) 

295 

296 # Arguments validation pylint: disable=too-many-boolean-expressions 

297 if not options.url_path: 

298 result = Entrypoint.Result.CRITICAL 

299 if (options.create_milestone \ 

300 or options.get_milestone \ 

301 or options.set_milestone_description \ 

302 or options.set_milestone_start_date \ 

303 or options.set_milestone_due_date \ 

304 ) and not options.milestone: 

305 result = Entrypoint.Result.CRITICAL 

306 

307 # Header 

308 print(' ') 

309 Platform.flush() 

310 

311 # Tool identifier 

312 if result != Entrypoint.Result.CRITICAL: 

313 print(f'{Colors.BOLD} {Bundle.NAME}' 

314 f'{Colors.YELLOW_LIGHT} ({Version.get()})' 

315 f'{Colors.RESET}') 

316 Platform.flush() 

317 

318 # CLI entrypoint 

319 if result != Entrypoint.Result.CRITICAL: 

320 result = Entrypoint.cli( 

321 options, 

322 environments, 

323 ) 

324 

325 # CLI helper 

326 else: 

327 parser.print_help() 

328 

329 # Footer 

330 print(' ') 

331 Platform.flush() 

332 

333 # Check for daily updates 

334 if updates.enabled and updates.daily: 

335 updates.check() 

336 

337 # Result 

338 if result in [ 

339 Entrypoint.Result.SUCCESS, 

340 Entrypoint.Result.FINALIZE, 

341 ]: 

342 sys_exit(0) 

343 else: 

344 sys_exit(1) 

345 

346# Entrypoint 

347if __name__ == '__main__': # pragma: no cover 

348 main()