18 from typing
import Optional
22 from ..modules
import ui, project_utils
23 from ..modules.user
import initializeUserSession
24 from ..modules.utils
import onBeforeCommandExecute
25 from ...entities
import Project, ProjectVisibility
26 from ...networking
import RequestFailedError
27 from ...configuration
import UserConfiguration
31 @click.option("--name", "-n", type = str, help = "Project name")
32 @click.option("--type", "-t", "projectType", type = int, help = "Project type")
33 @click.option("--description", "-d", type = str, help = "Project description")
34 def create(name: Optional[str], projectType: Optional[int], description: Optional[str]) ->
None:
35 userConfig = UserConfiguration.load()
36 project = project_utils.createProject(userConfig.frontendUrl, name, projectType, description)
38 selectNewProject = ui.clickPrompt(
"Do you want to select the new project as default? (Y/n)", type = bool, default =
True)
40 userConfig.selectProject(project.id)
41 ui.successEcho(f
"Project \"{project.name}\" successfully selected.")
45 @click.option("--project", "-p", type = str, help = "Project name")
46 @click.option("--name", "-n", type = str, help = "New Project name")
47 @click.option("--description", "-d", type = str, help = "New Project description")
48 def edit(project: Optional[str], name: Optional[str], description: Optional[str]) ->
None:
49 userConfiguration = UserConfiguration.load()
50 defaultProjectId = userConfiguration.projectId
51 if defaultProjectId
is None and project
is None:
52 ui.errorEcho(f
"To use edit command you need to specifiy project name using \"--project\" or \"-p\" flag, or you can select default project using \"coretex project select\" command.")
55 if project
is None and defaultProjectId
is not None:
56 selectedProject = Project.fetchById(defaultProjectId)
58 selectedProject = Project.fetchOne(name = project)
61 name = ui.clickPrompt(
"Please enter new name for your project", type = str, default = selectedProject.name)
63 if description
is None:
64 description = ui.clickPrompt(
"Please enter new description for your project", type = str, default = selectedProject.description, show_default =
False)
67 selectedProject.update(name = selectedProject.name, description = description)
68 selectedProject.updateVisibility(ProjectVisibility.private)
69 ui.successEcho(f
"Project id \"{selectedProject.id}\" successfully edited.")
71 except RequestFailedError:
72 raise click.ClickException(f
"Failed to edit project \"{selectedProject.name}\".")
76 @click.argument("name", type = str)
77 def select(name: str) ->
None:
78 project: Optional[Project] =
None
79 userConfig = UserConfiguration.load()
81 ui.progressEcho(
"Validating project...")
84 project = Project.fetchOne(name = name)
85 ui.successEcho(f
"Project \"{name}\" selected successfully!")
86 userConfig.selectProject(project.id)
88 ui.errorEcho(f
"Project \"{name}\" not found.")
89 project = project_utils.promptProjectCreate(
90 "Do you want to create a project with that name?",
92 userConfig.frontendUrl
97 userConfig.selectProject(project.id)
101 @onBeforeCommandExecute(initializeUserSession)
102 def project() -> None:
106 project.add_command(create,
"create")
107 project.add_command(edit,
"edit")
108 project.add_command(select,
"select")