/
stapler
/
stplr
Обзор
Документация
Войти
/
stapler
/
stplr
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
main
internal/cliutils/utils.go
85 строк
2 KB
Maxim Slipenko
fix: correct display error while plugin setup (#359)
13 мар 2026, 17:39
Не верифицирован
13 мар 2026, 17:39
a5a04fd
Код
Авторство
О чём код?
// SPDX-License-Identifier: GPL-3.0-or-later // // This file was originally part of the project "ALR - Any Linux Repository" // created by the ALR Authors. // It was later modified as part of "Stapler" by Maxim Slipenko and other Stapler Authors. // // Copyright (C) 2025 The ALR Authors // Copyright (C) 2025 The Stapler Authors // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. package cliutils import ( "context" stdErrors "errors" "fmt" "log/slog" "github.com/hashicorp/go-plugin" "github.com/urfave/cli/v3" "go.stplr.dev/stplr/internal/app/output" ) type BashCompleteWithErrorFunc func(ctx context.Context, c *cli.Command) error func BashCompleteWithError[F ~func(context.Context, *cli.Command) T, T error](f F) cli.ShellCompleteFunc { return func(ctx context.Context, c *cli.Command) { HandleExitCoder(ctx, c, f(ctx, c)) } } func HandleExitCoder(ctx context.Context, c *cli.Command, err error) { out := output.FromContext(ctx) if err == nil { return } slog.Error(fmt.Sprintf("%s command failed", c.Name), "err", err) var startupErr *plugin.PluginStartupError if stdErrors.As(err, &startupErr) { out.Error(startupErr.Message) cli.OsExiter(1) return } if exitErr, ok := err.(cli.ExitCoder); ok { if err.Error() != "" { if _, ok := exitErr.(cli.ErrorFormatter); ok { out.Error(fmt.Sprintf("%+v\n", err)) } else { out.Error(err.Error()) } } cli.OsExiter(exitErr.ExitCode()) return } out.Error(err.Error()) cli.OsExiter(1) } func FormatCliExit(msg string, err error) cli.ExitCoder { return FormatCliExitWithCode(msg, err, 1) } func FormatCliExitWithCode(msg string, err error, exitCode int) cli.ExitCoder { if err == nil { return cli.Exit(stdErrors.New(msg), exitCode) } return cli.Exit(fmt.Errorf("%s: %w", msg, err), exitCode) }