Skip to content

Commit ba62009

Browse files
willcl-arkpinheadmz
authored andcommitted
use inquirer for quickstart
1 parent 5fbcd63 commit ba62009

File tree

1 file changed

+121
-47
lines changed

1 file changed

+121
-47
lines changed

src/warnet/main.py

Lines changed: 121 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ def cli():
4747
def quickstart():
4848
"""Setup warnet"""
4949
try:
50+
# Requirements checks
5051
process = subprocess.Popen(
5152
["/bin/bash", str(QUICK_START_PATH)],
5253
stdout=subprocess.PIPE,
5354
stderr=subprocess.STDOUT,
5455
universal_newlines=True,
5556
env=dict(os.environ, TERM="xterm-256color"),
5657
)
57-
for line in iter(process.stdout.readline, ""):
58-
click.echo(line, nl=False)
59-
process.stdout.close()
58+
if process.stdout:
59+
for line in iter(process.stdout.readline, ""):
60+
click.echo(line, nl=False)
61+
process.stdout.close()
6062
return_code = process.wait()
6163
if return_code != 0:
6264
click.secho(
@@ -65,67 +67,139 @@ def quickstart():
6567
click.secho("Install missing requirements before proceeding", fg="yellow")
6668
return False
6769

68-
create_project = click.confirm(
69-
click.style("\nDo you want to create a new project?", fg="blue", bold=True),
70-
default=True,
71-
)
72-
if not create_project:
70+
# New project setup
71+
questions = [
72+
inquirer.Confirm(
73+
"create_project",
74+
message=click.style("Do you want to create a new project?", fg="blue", bold=True),
75+
default=True,
76+
),
77+
]
78+
answers = inquirer.prompt(questions)
79+
if answers is None:
80+
click.secho("Setup cancelled by user.", fg="yellow")
81+
return False
82+
if not answers["create_project"]:
7383
click.secho("\nSetup completed successfully!", fg="green", bold=True)
7484
return True
7585

76-
default_path = os.path.abspath(os.getcwd())
77-
project_path = click.prompt(
78-
click.style("\nEnter the project directory path", fg="blue", bold=True),
79-
default=default_path,
80-
type=click.Path(file_okay=False, dir_okay=True, resolve_path=True),
81-
)
82-
83-
custom_network = click.confirm(
84-
click.style("\nDo you want to create a custom network?", fg="blue", bold=True),
85-
default=True,
86-
)
87-
if not custom_network:
88-
create_warnet_project(Path(project_path))
86+
# Custom project setup
87+
questions = [
88+
inquirer.Path(
89+
"project_path",
90+
message=click.style("Enter the project directory path", fg="blue", bold=True),
91+
path_type=inquirer.Path.DIRECTORY,
92+
exists=False,
93+
),
94+
inquirer.Confirm(
95+
"custom_network",
96+
message=click.style(
97+
"Do you want to create a custom network?", fg="blue", bold=True
98+
),
99+
default=True,
100+
),
101+
]
102+
proj_answers = inquirer.prompt(questions)
103+
if proj_answers is None:
104+
click.secho("Setup cancelled by user.", fg="yellow")
105+
return False
106+
if not proj_answers["custom_network"]:
107+
create_warnet_project(Path(proj_answers["project_path"]))
89108
click.secho("\nSetup completed successfully!", fg="green", bold=True)
109+
click.echo(
110+
"\nRun the following command to deploy this network using the default demo network:"
111+
)
112+
click.echo(f"warcli deploy {proj_answers['project_path']}/networks/6_node_bitcoin")
90113
return True
114+
answers.update(proj_answers)
91115

92-
network_name = click.prompt(
93-
click.style("\nEnter the network name", fg="blue", bold=True),
94-
type=str,
95-
)
96-
97-
nodes = click.prompt(
98-
click.style("\nHow many nodes would you like?", fg="blue", bold=True),
99-
type=int,
100-
default=15,
101-
)
102-
connections = click.prompt(
103-
click.style(
104-
"\nHow many connections would you like each node to have?", fg="blue", bold=True
116+
# Custom network configuration
117+
questions = [
118+
inquirer.Text(
119+
"network_name", message=click.style("Enter your network name", fg="blue", bold=True)
105120
),
106-
type=int,
107-
default=8,
108-
)
109-
version = click.prompt(
110-
click.style(
111-
"\nWhich version would you like nodes to be by default?", fg="blue", bold=True
121+
inquirer.List(
122+
"nodes",
123+
message=click.style("How many nodes would you like?", fg="blue", bold=True),
124+
choices=["8", "12", "20", "50", "other"],
125+
default="12",
112126
),
113-
type=click.Choice(SUPPORTED_TAGS, case_sensitive=False),
114-
default=DEFAULT_TAG,
115-
)
127+
inquirer.List(
128+
"connections",
129+
message=click.style(
130+
"How many addnode connections would you like each node to have?",
131+
fg="blue",
132+
bold=True,
133+
),
134+
choices=["0", "1", "2", "8", "12", "other"],
135+
default="8",
136+
),
137+
inquirer.List(
138+
"version",
139+
message=click.style(
140+
"Which version would you like nodes to be by default?", fg="blue", bold=True
141+
),
142+
choices=SUPPORTED_TAGS,
143+
default=DEFAULT_TAG,
144+
),
145+
]
146+
147+
net_answers = inquirer.prompt(questions)
148+
if net_answers is None:
149+
click.secho("Setup cancelled by user.", fg="yellow")
150+
return False
151+
152+
if net_answers["nodes"] == "other":
153+
custom_nodes = inquirer.prompt(
154+
[
155+
inquirer.Text(
156+
"nodes",
157+
message=click.style("Enter the number of nodes", fg="blue", bold=True),
158+
validate=lambda _, x: int(x) > 0,
159+
)
160+
]
161+
)
162+
if custom_nodes is None:
163+
click.secho("Setup cancelled by user.", fg="yellow")
164+
return False
165+
net_answers["nodes"] = custom_nodes["nodes"]
166+
167+
if net_answers["connections"] == "other":
168+
custom_connections = inquirer.prompt(
169+
[
170+
inquirer.Text(
171+
"connections",
172+
message=click.style(
173+
"Enter the number of connections", fg="blue", bold=True
174+
),
175+
validate=lambda _, x: int(x) >= 0,
176+
)
177+
]
178+
)
179+
if custom_connections is None:
180+
click.secho("Setup cancelled by user.", fg="yellow")
181+
return False
182+
net_answers["connections"] = custom_connections["connections"]
183+
answers.update(net_answers)
116184

117185
click.secho("\nCreating project structure...", fg="yellow", bold=True)
118-
create_warnet_project(Path(project_path))
186+
create_warnet_project(Path(answers["project_path"]))
119187
click.secho("\nGenerating custom network...", fg="yellow", bold=True)
120-
custom_network_path = Path(project_path) / "networks" / network_name
121-
custom_graph(nodes, connections, version, custom_network_path)
188+
custom_network_path = Path(answers["project_path"]) / "networks" / answers["network_name"]
189+
custom_graph(
190+
int(answers["nodes"]),
191+
int(answers["connections"]),
192+
answers["version"],
193+
custom_network_path,
194+
)
122195
click.secho("\nSetup completed successfully!", fg="green", bold=True)
123196
click.echo("\nRun the following command to deploy this network:")
124197
click.echo(f"warnet deploy {custom_network_path}")
125198
except Exception as e:
199+
click.echo(f"{e}\n\n")
126200
click.secho(f"An error occurred while running the quick start script:\n\n{e}\n\n", fg="red")
127201
click.secho(
128-
"Please report this to https://github.com/bitcoin-dev-project/warnet/issues",
202+
"Please report the above context to https://github.com/bitcoin-dev-project/warnet/issues",
129203
fg="yellow",
130204
)
131205
return False

0 commit comments

Comments
 (0)