🔊 feat(log): Log to file

This commit is contained in:
punkfairie 2024-09-08 18:52:55 -07:00
parent 6dd925467a
commit d06436d993
5 changed files with 143 additions and 13 deletions

3
go.mod
View file

@ -12,9 +12,11 @@ require (
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/log v0.4.0 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
@ -23,6 +25,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.3.8 // indirect

6
go.sum
View file

@ -6,12 +6,16 @@ github.com/charmbracelet/bubbletea v1.1.0 h1:FjAl9eAL3HBCHenhz/ZPjkKdScmaS5SK69J
github.com/charmbracelet/bubbletea v1.1.0/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=
github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@ -29,6 +33,8 @@ github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1n
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View file

@ -2,7 +2,6 @@ package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"time"
@ -18,7 +17,7 @@ func (m menu) installPackage() tea.Cmd {
return func() tea.Msg {
pkg := m.order[m.current]
m.appendOutput(fmt.Sprintf("Installing %s", pkg))
m.logger.Infof("Installing %s...", pkg)
cmd := exec.Command("./test.sh")
out, err := cmd.StdoutPipe()
@ -35,8 +34,9 @@ func (m menu) installPackage() tea.Cmd {
line, _, err := buf.ReadLine()
if err == io.EOF {
m.appendOutput(fmt.Sprintf("Finished installing %s!", pkg))
time.Sleep(3 * time.Second)
m.logger.Infof("Finished installing %s!", pkg)
m.logger.Infof("Output: %s\n\n", *m.output)
time.Sleep(2 * time.Second)
return cmdDoneMsg{}
}

108
log.txt Normal file
View file

@ -0,0 +1,108 @@
INFO Installing atuin...
INFO Finished installing atuin!
INFO Output:
0 line
1 line
2 line
3 line
4 line
5 line
6 line
7 line
8 line
9 line
10 line
11 line
12 line
13 line
14 line
15 line
16 line
17 line
18 line
19 line
20 line
21 line
22 line
23 line
24 line
25 line
26 line
27 line
28 line
29 line
30 line
INFO Installing bash-completion...
INFO Finished installing bash-completion!
INFO Output:
0 line
1 line
2 line
3 line
4 line
5 line
6 line
7 line
8 line
9 line
10 line
11 line
12 line
13 line
14 line
15 line
16 line
17 line
18 line
19 line
20 line
21 line
22 line
23 line
24 line
25 line
26 line
27 line
28 line
29 line
30 line
INFO Installing bandwhich...
INFO Finished installing bandwhich!
INFO Output:
0 line
1 line
2 line
3 line
4 line
5 line
6 line
7 line
8 line
9 line
10 line
11 line
12 line
13 line
14 line
15 line
16 line
17 line
18 line
19 line
20 line
21 line
22 line
23 line
24 line
25 line
26 line
27 line
28 line
29 line
30 line

31
main.go
View file

@ -12,6 +12,7 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"golang.org/x/term"
)
@ -27,6 +28,7 @@ type menu struct {
sub chan string
output *string
viewport viewport.Model
logger *log.Logger
}
const (
@ -34,23 +36,28 @@ const (
softwareGroup = "_Full-Desktop"
)
func initialModel() menu {
func initialModel(logFile *os.File) menu {
s := spinner.New()
s.Spinner = spinner.MiniDot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("3"))
width, height, _ := term.GetSize(int(os.Stdout.Fd()))
logger := log.New(logFile)
logger.SetLevel(log.InfoLevel)
logger.SetFormatter(log.TextFormatter)
m := menu{
current: 0,
keys: keys,
help: help.New(),
spinner: s,
quitting: false,
width: width,
height: height,
sub: make(chan string),
output: new(string),
viewport: viewport.New(0, 30),
width: width,
height: height,
spinner: s,
keys: keys,
help: help.New(),
logger: logger,
quitting: false,
}
return m
@ -135,7 +142,7 @@ func (m menu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Height = lipgloss.Height(m.mainView())
case errMsg:
m.appendOutput("Error: " + msg.Error())
m.logger.Error("Error: " + msg.Error())
}
m.viewport, cmd = m.viewport.Update(msg)
@ -159,8 +166,14 @@ func (m menu) View() string {
}
func main() {
l, err := os.Create("log.txt")
if err != nil {
panic(err)
}
defer l.Close()
p := tea.NewProgram(
initialModel(),
initialModel(l),
tea.WithAltScreen(),
)