11#!/usr/bin/env python3
22import asyncio
3- import click
43import os
5- import colorama
6-
74from functools import wraps
8- from github import DeploymentState , GitHub , read_github_event_data , get_current_deployment_id , get_current_environment
9- from util import get_repo_or_fallback
5+
6+ import click
7+ import colorama
8+ from github import DeploymentState , GitHub , get_current_deployment_id , get_current_environment , read_github_event_data
9+ from util import get_repo_fallback
1010
1111
1212def coroutine (f ):
@@ -17,69 +17,120 @@ def wrapper(*args, **kwargs):
1717 return wrapper
1818
1919
20+ def click_repo_option ():
21+ repo_path = get_repo_fallback (read_github_event_data ())
22+ return click .option ("-r" , "--repo" ,
23+ required = repo_path is None ,
24+ default = repo_path ,
25+ help = "Repository to use, e.g. moneymeets/ghd" )
26+
27+
28+ def click_deployment_id_option ():
29+ deployment_id = get_current_deployment_id ()
30+ return click .option ("-d" , "--deployment-id" ,
31+ type = int ,
32+ required = deployment_id is None ,
33+ default = deployment_id ,
34+ help = "Deployment ID" )
35+
36+
2037@click .group ()
2138def main_group ():
2239 pass
2340
2441
2542@main_group .command (name = "list" , short_help = "List deployments" )
26- @click .option ("-r" , "--repo" , required = False , help = "Repository to use, e.g. moneymeets/ghd" )
27- @click .option ("-v" , "--verbose" , required = False , is_flag = True , flag_value = True , default = False ,
43+ @click_repo_option ()
44+ @click .option ("-v" , "--verbose" ,
45+ required = False ,
46+ is_flag = True ,
47+ flag_value = True ,
48+ default = False ,
2849 help = "Print deployment states (slow)" )
29- @click .option ("-l" , "--limit" , required = False , default = 10 , help = "How many deployments to list" )
50+ @click .option ("-l" , "--limit" ,
51+ required = False ,
52+ default = 10 ,
53+ help = "How many deployments to list" )
3054@coroutine
3155async def cmd_list (repo : str , verbose : bool , limit : int ):
32- github_event_data = read_github_event_data ()
33- async with GitHub (repo_path = get_repo_or_fallback (repo , github_event_data )) as gh :
56+ async with GitHub (repo_path = repo ) as gh :
3457 await gh .list (limit = limit , verbose = verbose )
3558
3659
3760@main_group .command (name = "deploy" , short_help = "Create new deployment" )
38- @click .option ("-r" , "--repo" , required = False , help = "Repository to use, e.g. moneymeets/ghd" )
39- @click .option ("-R" , "--ref" , required = True , help = "Reference to create the deployment from" )
40- @click .option ("-e" , "--environment" , required = True , prompt = True , help = "Environment name" )
41- @click .option ("-T" , "--task" , default = "deploy" , help = "Deployment task" )
42- @click .option ("-t" , "--transient" , required = False , is_flag = True , flag_value = True , prompt = True , default = False ,
61+ @click_repo_option ()
62+ @click .option ("-R" , "--ref" ,
63+ required = True ,
64+ help = "Reference to create the deployment from" )
65+ @click .option ("-e" , "--environment" ,
66+ required = True ,
67+ prompt = True ,
68+ help = "Environment name" )
69+ @click .option ("-T" , "--task" ,
70+ default = "deploy" ,
71+ help = "Deployment task" )
72+ @click .option ("-t" , "--transient" ,
73+ required = False ,
74+ is_flag = True ,
75+ flag_value = True ,
76+ prompt = True ,
77+ default = False ,
4378 help = "Mark as transient environment" )
44- @click .option ("-p" , "--production" , required = False , is_flag = True , flag_value = True , prompt = True , default = False ,
79+ @click .option ("-p" , "--production" ,
80+ required = False ,
81+ is_flag = True ,
82+ flag_value = True ,
83+ prompt = True ,
84+ default = False ,
4585 help = "Mark as production environment" )
46- @click .option ("-d" , "--description" , default = "Deployed via GHD" , prompt = True , help = "Deployment description" )
86+ @click .option ("-d" , "--description" ,
87+ default = "Deployed via GHD" ,
88+ prompt = True ,
89+ help = "Deployment description" )
4790@coroutine
4891async def cmd_deploy (repo : str , ref : str , environment : str , task : str , transient : bool , production : bool ,
4992 description : str ):
50- github_event_data = read_github_event_data ()
51- async with GitHub ( repo_path = get_repo_or_fallback ( repo , github_event_data )) as gh :
52- await gh . deploy ( environment = environment , ref = ref or os .environ .get ("GITHUB_SHA" ),
93+ async with GitHub ( repo_path = repo ) as gh :
94+ await gh . deploy ( environment = environment ,
95+ ref = ref or os .environ .get ("GITHUB_SHA" ),
5396 transient = transient ,
5497 production = production ,
5598 task = task ,
5699 description = description )
57100
58101
59102@main_group .command (name = "set-state" , short_help = "Set deployment state" )
60- @click .option ("-r" , "--repo" , required = False , help = "Repository to use, e.g. moneymeets/ghd" )
61- @click .option ("-e" , "--environment" , required = True , default = get_current_environment (), help = "Environment name" )
62- @click .option ("-d" , "--deployment-id" , type = int , required = True , default = get_current_deployment_id (),
63- help = "Deployment ID" )
103+ @click_repo_option ()
104+ @click .option ("-e" , "--environment" ,
105+ required = True ,
106+ default = get_current_environment (),
107+ help = "Environment name" )
108+ @click_deployment_id_option ()
64109@click .option ("-s" , "--state" ,
65110 type = click .Choice (choices = DeploymentState .__members__ .keys ()),
66- required = True , help = "State" )
67- @click .option ("-D" , "--description" , default = "Deployed via GHD" , help = "Deployment description" )
111+ required = True ,
112+ help = "State" )
113+ @click .option ("-D" , "--description" ,
114+ default = "Deployed via GHD" ,
115+ help = "Deployment description" )
68116@coroutine
69117async def cmd_set_state (repo : str , environment : str , deployment_id : int , state : str , description : str ):
70- async with GitHub (repo_path = get_repo_or_fallback ( repo , read_github_event_data ()) ) as gh :
118+ async with GitHub (repo_path = repo ) as gh :
71119 await gh .create_deployment_status (deployment_id = deployment_id ,
72120 state = DeploymentState [state ],
73121 environment = environment ,
74122 description = description )
75123
76124
77125@main_group .command (name = "inspect" , short_help = "Inspect deployment state history" )
78- @click .option ("-r" , "--repo" , required = False , help = "Repository to use, e.g. moneymeets/ghd" )
79- @click .argument ("deployment-id" , type = int , required = True , nargs = 1 )
126+ @click_repo_option ()
127+ @click .argument ("deployment-id" ,
128+ type = int ,
129+ required = True ,
130+ nargs = 1 )
80131@coroutine
81132async def cmd_inspect (repo : str , deployment_id : int ):
82- async with GitHub (repo_path = get_repo_or_fallback ( repo , read_github_event_data ()) ) as gh :
133+ async with GitHub (repo_path = repo ) as gh :
83134 await gh .inspect (deployment_id = deployment_id )
84135
85136
@@ -88,5 +139,5 @@ def run_main():
88139 main_group ()
89140
90141
91- if __name__ == ' __main__' :
142+ if __name__ == " __main__" :
92143 run_main ()
0 commit comments