Skip to content

Commit

Permalink
Ability to add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Feb 19, 2024
1 parent 876818e commit 269f7cc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
66 changes: 66 additions & 0 deletions app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (opts *Opts) Execute() error {
dispatcher.AddHandler(handlers.NewCommand("start", start))
dispatcher.AddHandler(handlers.NewCommand("bal", opts.bal))
dispatcher.AddHandler(handlers.NewCommand("version", opts.vesrion))
dispatcher.AddHandler(handlers.NewCommand("/", opts.comment))
dispatcher.AddHandler(handlers.NewMessage(nil, message))

// Start receiving updates.
Expand Down Expand Up @@ -124,6 +125,71 @@ func (opts *Opts) bal(b *gotgbot.Bot, ctx *ext.Context) error {
return nil
}

func (opts *Opts) comment(b *gotgbot.Bot, ctx *ext.Context) error {
msg := ctx.EffectiveMessage

rs, err := repo.NewInMemoryRepo(opts.Github.URL, opts.Github.Token)

if err != nil {
slog.Error("unable to init repo", "error", err, "user", msg.From.Username)
_, ierr := b.SendMessage(msg.Chat.Id, fmt.Sprintf("unable to init repo: %v", err), nil)
if ierr != nil {
return fmt.Errorf("unable to send message: %w", ierr)
}
return nil
}

f, err := rs.OpenForAppend(opts.Github.MainLedgerFile)

if err != nil {
slog.Error("unable to open main ledger file", "error", err, "user", msg.From.Username)
_, ierr := b.SendMessage(msg.Chat.Id, fmt.Sprintf("unable to open main ledger file: %v", err), nil)
if ierr != nil {
return fmt.Errorf("unable to send message: %w", ierr)
}
return nil
}

comment := fmt.Sprintf(";; %s\n", msg.Text)
_, err = fmt.Fprint(f, comment)

if err != nil {
slog.Error("unable to write main ledger file", "error", err, "user", msg.From.Username)
_, ierr := b.SendMessage(msg.Chat.Id, fmt.Sprintf("unable to write main ledger file: %v", err), nil)
if ierr != nil {
return fmt.Errorf("unable to send message: %w", ierr)
}
return nil
}

err = f.Close()

if err != nil {
slog.Error("unable to close main ledger file", "error", err, "user", msg.From.Username)
_, ierr := b.SendMessage(msg.Chat.Id, fmt.Sprintf("unable to close main ledger file: %v", err), nil)
if ierr != nil {
return fmt.Errorf("unable to send message: %w", ierr)
}
return nil
}

err = rs.CommitPush("new comment", msg.From.Username, "teledger@example.com")

if err != nil {
slog.Error("unable to commit", "error", err)
_, ierr := b.SendMessage(msg.Chat.Id, fmt.Sprintf("unable to commit: %v", err), nil)
if ierr != nil {
return fmt.Errorf("unable to send message: %w", ierr)
}
return nil
}

if _, err := b.SendMessage(msg.Chat.Id, fmt.Sprintf("Comment added:\n```%s```", comment), &gotgbot.SendMessageOpts{ParseMode: "MarkdownV2"}); err != nil {
return fmt.Errorf("unable to send message: %w", err)
}
return nil
}


func message(b *gotgbot.Bot, ctx *ext.Context) error {
msg := ctx.EffectiveMessage
Expand Down
3 changes: 3 additions & 0 deletions app/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (w *WriteCloser) Close() error {

func (r *InMemoryRepo) OpenFile(file string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) {
wtr, err := r.repo.Worktree()
if err != nil {
return nil, fmt.Errorf("worktree receiving error: %v", err)
}
f, err := wtr.Filesystem.OpenFile(file, flag, perm)
wc := WriteCloser{
r: r,
Expand Down

0 comments on commit 269f7cc

Please sign in to comment.