ladybird/Userland/Utilities/open.cpp
Shannon Booth e800605ad3 AK+LibURL: Move AK::URL into a new URL library
This URL library ends up being a relatively fundamental base library of
the system, as LibCore depends on LibURL.

This change has two main benefits:
 * Moving AK back more towards being an agnostic library that can
   be used between the kernel and userspace. URL has never really fit
   that description - and is not used in the kernel.
 * URL _should_ depend on LibUnicode, as it needs punnycode support.
   However, it's not really possible to do this inside of AK as it can't
   depend on any external library. This change brings us a little closer
   to being able to do that, but unfortunately we aren't there quite
   yet, as the code generators depend on LibCore.
2024-03-18 14:06:28 -04:00

48 lines
1.4 KiB
C++

/*
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibDesktop/Launcher.h>
#include <LibFileSystem/FileSystem.h>
#include <LibMain/Main.h>
#include <LibURL/URL.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Core::EventLoop loop;
Vector<StringView> urls_or_paths;
Core::ArgsParser parser;
parser.set_general_help("Open a file or URL by executing the appropriate program.");
parser.add_positional_argument(urls_or_paths, "URL or file path to open", "url-or-path");
parser.parse(arguments);
bool all_ok = true;
for (auto& url_or_path : urls_or_paths) {
auto path_or_error = FileSystem::real_path(url_or_path);
URL::URL url;
if (path_or_error.is_error()) {
url = url_or_path;
if (!url.is_valid()) {
warnln("Failed to open: '{}': {}", url_or_path, strerror(path_or_error.error().code()));
continue;
}
} else {
url = URL::create_with_url_or_path(path_or_error.value());
}
if (!Desktop::Launcher::open(url)) {
warnln("Failed to open '{}'", url);
all_ok = false;
}
}
return all_ok ? 0 : 1;
}