Files
Dominik Schulz 7c63ba09b7 chore(deps): migrate from urfave/cli v2 to v3 (#3428)
Migrate the entire codebase from github.com/urfave/cli/v2 to
github.com/urfave/cli/v3 (v3.9.0).

Key breaking changes addressed:
- cli.App removed, replaced by *cli.Command
- ActionFunc signature: func(*Context) error -> func(context.Context, *Command) error
- BeforeFunc signature: func(*Context) error -> func(context.Context, *Command) (context.Context, error)
- app.RunContext -> app.Run
- app.EnableBashCompletion -> app.EnableShellCompletion
- Subcommands field renamed to Commands
- EnvVars on flags -> Sources: cli.EnvVars(...)
- cli.NewContext removed; test helpers updated to use cmd.Run() pattern
- cli.Flag interface updated (Apply removed, Get/PreParse/PostParse/Set added)
- VersionPrinter type changed to func(*Command)

Also updates .capabilities.json baseline to reflect new cli/v3 call paths.

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
2026-05-16 18:13:19 +02:00

102 lines
2.6 KiB
Go

package action
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/updater"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/protect"
"github.com/urfave/cli/v3"
)
// Version prints the gopass version.
func (s *miscHandler) Version(ctx context.Context, cmd *cli.Command) error {
ctx = ctxutil.WithGlobalFlags(ctx, cmd)
version := make(chan string, 1)
go s.checkVersion(ctx, version)
cli.VersionPrinter(cmd)
select {
case vi := <-version:
if vi != "" {
fmt.Fprintln(stdout, vi)
}
case <-time.After(2 * time.Second):
out.Errorf(ctx, "Version check timed out")
case <-ctx.Done():
return exit.Error(exit.Aborted, nil, "user aborted")
}
return nil
}
func (s *miscHandler) checkVersion(ctx context.Context, u chan string) {
msg := ""
defer func() {
u <- msg
}()
if disabled := os.Getenv("CHECKPOINT_DISABLE"); disabled != "" {
debug.Log("remote version check disabled by CHECKPOINT_DISABLE")
return
}
// NB: "updater.check" isn't supported as a local config option, hence no mount point here
if cfg, _ := config.FromContext(ctx); cfg.IsSet("updater.check") && !config.AsBool(cfg.Get("updater.check")) {
debug.Log("remote version check disabled by updater.check = false")
return
}
// force checking for updates, mainly for testing.
force := os.Getenv("GOPASS_FORCE_CHECK") != ""
if !force && strings.HasSuffix(s.version.String(), "+HEAD") {
// chan not check version against HEAD.
debug.Log("remote version check disabled for dev version")
return
}
if !force && protect.ProtectEnabled {
// chan not check version
// against pledge(2)'d OpenBSD.
debug.Log("remote version check disabled for pledge(2)'d version")
return
}
r, err := updater.FetchLatestRelease(ctx)
if err != nil {
msg = color.RedString("\nError checking latest version: %s", err)
return
}
if !r.Version.GT(s.version) {
_ = s.rem.Reset("update")
debug.Log("gopass is up-to-date (local: %q, GitHub: %q)", s.version, r.Version)
return
}
notice := fmt.Sprintf("\nYour version (%s) of gopass is out of date!\nThe latest version is %s.\n", s.version, r.Version.String())
notice += "You can update by downloading from https://www.gopass.pw/#install"
if err := updater.IsUpdateable(ctx); err == nil {
notice += " by running 'gopass update'"
}
notice += " or via your package manager"
msg = color.YellowString(notice)
}