1package action
2
3import (
4	"io"
5	"os"
6	"path/filepath"
7
8	"github.com/gopasspw/gopass/internal/config"
9	"github.com/gopasspw/gopass/internal/reminder"
10	"github.com/gopasspw/gopass/internal/store/root"
11	"github.com/gopasspw/gopass/pkg/debug"
12
13	"github.com/blang/semver/v4"
14)
15
16var (
17	stdin  io.Reader = os.Stdin
18	stdout io.Writer = os.Stdout
19)
20
21// Action knows everything to run gopass CLI actions
22type Action struct {
23	Name    string
24	Store   *root.Store
25	cfg     *config.Config
26	version semver.Version
27	rem     *reminder.Store
28}
29
30// New returns a new Action wrapper
31func New(cfg *config.Config, sv semver.Version) (*Action, error) {
32	return newAction(cfg, sv, true)
33}
34
35func newAction(cfg *config.Config, sv semver.Version, remind bool) (*Action, error) {
36	name := "gopass"
37	if len(os.Args) > 0 {
38		name = filepath.Base(os.Args[0])
39	}
40
41	act := &Action{
42		Name:    name,
43		cfg:     cfg,
44		version: sv,
45		Store:   root.New(cfg),
46	}
47
48	if remind {
49		r, err := reminder.New()
50		if err != nil {
51			debug.Log("failed to init reminder: %s", err)
52		} else {
53			// only populate the reminder variable on success, the implementation
54			// can handle being called on a nil pointer
55			act.rem = r
56		}
57	}
58
59	return act, nil
60}
61
62// String implement fmt.Stringer
63func (s *Action) String() string {
64	return s.Store.String()
65}
66