Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Igniter.CLI for expanded interactivity support #127

Open
zachallaun opened this issue Oct 30, 2024 · 2 comments
Open

Proposal: Igniter.CLI for expanded interactivity support #127

zachallaun opened this issue Oct 30, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@zachallaun
Copy link
Collaborator

zachallaun commented Oct 30, 2024

Original discussion on Discord
Prerequisite: #126

This proposal is for expanding Igniter's CLI support to offer better integration between command line arguments and interactive user prompts.

As an example, consider a version of mix phx.new written using Igniter. It supports many options to configure a new project, including things like --no-ecto, --database postgres, etc. When implemented, this proposal would allow for an interactive series of prompts that would guide users through those options, using command line arguments to skip prompts when appropriate.

$ mix igniter.phx.new my_app

[Database] Set up an Ecto Repo?

[y/N] (default y)> y


[Database] Which database will you use?

1. Postgres
2. MySQL
3. MSSQL
4. SQLite

(default 1): 1

...

With the --no-ecto flag, specified, those prompts would be skipped:

$ mix igniter.phx.new --no-ecto my_app

[Database] Set up an Ecto Repo? NO (--no-ecto)

...

And to skip all prompts, the Igniter-global --yes could be used:

$ mix igniter.phx.new --yes

[Database] Set up an Ecto Repo? YES (--yes)
[Database] Which database will you use? Postgres (--yes)
...

To support this, a new Igniter.CLI module will be created. The specific API is a work in progress, but it may look something like this:

@impl Igniter.Mix.Task
def info(_argv, _composing_task) do
  %Igniter.Mix.Task.Info{
    schema: [
      ecto: :boolean,
      database: :string
    ],
    ...
  }
end

@impl Igniter.Mix.Task
def igniter(igniter) do
  igniter
  |> Igniter.CLI.yes_no_prompt(:ecto, "[Database] Set up an Ecto Repo?")
  |> Igniter.CLI.select_prompt(:database, "[Database] Which database will you use?",
    # only issue this prompt if the function returns truthy
    if: &(&1.args.options.ecto),
    options: [
      {"postgres", "Postgres"},
      {"mysql", "MySQL"},
      {"mssql", "MSSQL"},
      {"sqlite", "SQLite"}
    ]
  )
  |> generate_ecto()
end

defp generate_ecto(igniter) do
  if igniter.args.options.ecto do
    database = igniter.args.options.database
    ...
  else
    igniter
  end
end
@zachallaun zachallaun added the enhancement New feature or request label Oct 30, 2024
@zachdaniel
Copy link
Contributor

Looks good! One tiny note: the way you do no- options is by doing ecto: :boolean and OptionParser figures out that --no-ecto means --ecto false IIRC.

@zachallaun
Copy link
Collaborator Author

Looks good! One tiny note: the way you do no- options is by doing ecto: :boolean and OptionParser figures out that --no-ecto means --ecto false IIRC.

Oh! Awesome. I'll update the example to reflect that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants