-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (115 loc) · 2.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"errors"
"fmt"
"log"
"os"
"passfu/commandpkg"
"passfu/pwstore"
"strings"
"time"
"github.com/charmbracelet/bubbles/filepicker"
tea "github.com/charmbracelet/bubbletea"
"github.com/urfave/cli"
)
type appstate struct {
filepicker filepicker.Model
selectedfile string
quitting bool
err error
}
type clearErrorMsg struct{}
func clearErrorAfter(t time.Duration) tea.Cmd {
return tea.Tick(t, func(_ time.Time) tea.Msg {
return clearErrorMsg{}
})
}
func (a appstate) Init() tea.Cmd {
return a.filepicker.Init()
}
func (a appstate) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
a.quitting = true
return a, tea.Quit
default:
a.quitting = false
}
case clearErrorMsg:
a.err = nil
default:
a.quitting = false
}
var cmd tea.Cmd
a.filepicker, cmd = a.filepicker.Update(msg)
var didSelect bool
var path string
didSelect, path = a.filepicker.DidSelectFile(msg)
if didSelect {
a.selectedfile = path
}
didSelect, path = a.filepicker.DidSelectDisabledFile(msg)
if didSelect {
a.err = errors.New(path + " is not valid.")
a.selectedfile = ""
return a, tea.Batch(cmd, clearErrorAfter(2*time.Second))
}
return a, nil
}
func (a appstate) View() string {
if a.quitting {
return ""
}
var s strings.Builder
s.WriteString("\n ")
if a.err != nil {
s.WriteString(a.filepicker.Styles.DisabledFile.Render(a.err.Error()))
}
s.WriteString("\n\n" + a.filepicker.View() + "\n")
return s.String()
}
func main() {
var me cli.Author = cli.Author{
Name: "Enrico Tuvera Jr",
Email: "[email protected]",
}
var authors []cli.Author
authors = append(authors, me)
var commands []cli.Command
commands = append(commands, pwstore.NewDatabase)
commands = append(commands, pwstore.NewPassword)
commands = append(commands, pwstore.GetPassword)
commands = append(commands, commandpkg.EncryptDatabase)
commands = append(commands, commandpkg.DecryptDatabase)
commands = append(commands, commandpkg.SanityCheck)
var app *cli.App = &cli.App{
Name: "passfu",
Usage: "A password manager for the command line.",
Action: func(*cli.Context) error {
var fp filepicker.Model = filepicker.New()
fp.AllowedTypes = []string{}
fp.CurrentDirectory, _ = os.Getwd()
fmt.Println(fp.CurrentDirectory)
var a appstate = appstate{filepicker: fp}
var prog *tea.Program = tea.NewProgram(&a)
var err error
var mod tea.Model
mod, err = prog.Run()
if err != nil {
return err
}
// gotta read up on type assertions in golang
var mm appstate = mod.(appstate)
fmt.Println("\n You selected: " + a.filepicker.Styles.Selected.Render(mm.selectedfile) + "\n")
return nil
},
Authors: authors,
Commands: commands,
}
var err error = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}