Skip to main content

src/gleem@install.erl

-module(gleem@install).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleem/install.gleam").
-export([get_line/1, ask/0, main/0]).
-export_type([answer/0]).

-type answer() :: yes | no.

-file("src/gleem/install.gleam", 5).
-spec get_line(binary()) -> binary().
get_line(Prompt) ->
    io:get_line(Prompt).

-file("src/gleem/install.gleam", 15).
-spec ask() -> answer().
ask() ->
    case io:get_line(<<"Do you wish to install Gleem? (y/n) "/utf8>>) of
        <<"y\n"/utf8>> ->
            yes;

        <<"n\n"/utf8>> ->
            no;

        _ ->
            ask()
    end.

-file("src/gleem/install.gleam", 23).
-spec install() -> {ok, nil} | {error, binary()}.
install() ->
    gleam_stdlib:println(
        <<"Downloading Gleem binary and making it executable..."/utf8>>
    ),
    Output = gleem_ffi:cmd(
        <<"curl -L https://github.com/Thebugcanfly13/gleem/releases/latest/download && chmod +x /usr/bin/gleem"/utf8>>
    ),
    case Output of
        <<""/utf8>> ->
            gleam_stdlib:println(
                gleam_community@ansi:green(<<"Installation successful!"/utf8>>)
            ),
            gleam_stdlib:println(<<"Thank you for using Gleem!"/utf8>>),
            {ok, nil};

        Err ->
            {error, Err}
    end.

-file("src/gleem/install.gleam", 39).
-spec main() -> nil.
main() ->
    gleam_stdlib:println(
        gleam_community@ansi:pink(<<"Welcome to the Gleem installer!"/utf8>>)
    ),
    gleam_stdlib:println(
        <<"Gleem currently only supports Linux systems with the /usr/bin directory."/utf8>>
    ),
    _pipe = case ask() of
        yes ->
            case install() of
                {ok, _} ->
                    gleam_community@ansi:green(
                        <<"Installed binary successfully!"/utf8>>
                    );

                {error, Err} ->
                    gleam_community@ansi:red(
                        <<"Installation failed: "/utf8, Err/binary>>
                    )
            end;

        no ->
            gleam_community@ansi:red(<<"Installation cancelled."/utf8>>)
    end,
    gleam_stdlib:println(_pipe).