Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions internal/cmd/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
)

type ConfigCmd struct {
Get ConfigGetCmd `cmd:"" aliases:"show" help:"Get a config value"`
Keys ConfigKeysCmd `cmd:"" aliases:"list-keys,names" help:"List available config keys"`
Set ConfigSetCmd `cmd:"" aliases:"add,update" help:"Set a config value"`
Unset ConfigUnsetCmd `cmd:"" aliases:"rm,del,remove" help:"Unset a config value"`
List ConfigListCmd `cmd:"" aliases:"ls,all" help:"List all config values"`
Path ConfigPathCmd `cmd:"" aliases:"where" help:"Print config file path"`
Get ConfigGetCmd `cmd:"" aliases:"show" help:"Get a config value"`
Keys ConfigKeysCmd `cmd:"" aliases:"list-keys,names" help:"List available config keys"`
Set ConfigSetCmd `cmd:"" aliases:"add,update" help:"Set a config value"`
Unset ConfigUnsetCmd `cmd:"" aliases:"rm,del,remove" help:"Unset a config value"`
List ConfigListCmd `cmd:"" aliases:"ls,all" help:"List all config values"`
Path ConfigPathCmd `cmd:"" aliases:"where" help:"Print config file path"`
NoSend ConfigNoSendCmd `cmd:"" name:"no-send" aliases:"nosend" help:"Manage per-account send blocking"`
}

type ConfigGetCmd struct {
Expand Down
96 changes: 96 additions & 0 deletions internal/cmd/config_no_send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/steipete/gogcli/internal/config"
"github.com/steipete/gogcli/internal/outfmt"
)

type ConfigNoSendCmd struct {
Set ConfigNoSendSetCmd `cmd:"" aliases:"add,block" help:"Block send for an account"`
Remove ConfigNoSendRemoveCmd `cmd:"" aliases:"rm,unblock,allow" help:"Allow send for an account"`
List ConfigNoSendListCmd `cmd:"" aliases:"ls" help:"List accounts with send blocked"`
}

type ConfigNoSendSetCmd struct {
Account string `arg:"" help:"Account email to block send for"`
}

func (c *ConfigNoSendSetCmd) Run(ctx context.Context, flags *RootFlags) error {
if err := dryRunExit(ctx, flags, "config.no-send.set", map[string]any{
"account": c.Account,
}); err != nil {
return err
}

if err := config.UpdateConfig(func(cfg *config.File) error {
return config.SetNoSendAccount(cfg, c.Account, true)
}); err != nil {
return err
}

if outfmt.IsJSON(ctx) {
return outfmt.WriteJSON(ctx, os.Stdout, map[string]any{
"account": c.Account,
"noSend": true,
})
}
fmt.Fprintf(os.Stdout, "Send blocked for %s\n", c.Account)
return nil
}

type ConfigNoSendRemoveCmd struct {
Account string `arg:"" help:"Account email to allow send for"`
}

func (c *ConfigNoSendRemoveCmd) Run(ctx context.Context, flags *RootFlags) error {
if err := dryRunExit(ctx, flags, "config.no-send.remove", map[string]any{
"account": c.Account,
}); err != nil {
return err
}

if err := config.UpdateConfig(func(cfg *config.File) error {
return config.SetNoSendAccount(cfg, c.Account, false)
}); err != nil {
return err
}

if outfmt.IsJSON(ctx) {
return outfmt.WriteJSON(ctx, os.Stdout, map[string]any{
"account": c.Account,
"noSend": false,
})
}
fmt.Fprintf(os.Stdout, "Send allowed for %s\n", c.Account)
return nil
}

type ConfigNoSendListCmd struct{}

func (c *ConfigNoSendListCmd) Run(ctx context.Context) error {
cfg, err := config.ReadConfig()
if err != nil {
return err
}

accounts := config.ListNoSendAccounts(cfg)

if outfmt.IsJSON(ctx) {
return outfmt.WriteJSON(ctx, os.Stdout, map[string]any{
"noSendAccounts": accounts,
})
}

if len(accounts) == 0 {
fmt.Fprintln(os.Stdout, "No accounts have send blocked")
return nil
}
for _, acct := range accounts {
fmt.Fprintln(os.Stdout, acct)
}
return nil
}
4 changes: 4 additions & 0 deletions internal/cmd/gmail_autoreply.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func (c *GmailAutoReplyCmd) Run(ctx context.Context, flags *RootFlags) error {
if err != nil {
return err
}
if err = checkAccountNoSend(account); err != nil {
return err
}

summary, err := runGmailAutoReply(ctx, svc, account, input)
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/gmail_drafts.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,15 @@ func (c *GmailDraftsSendCmd) Run(ctx context.Context, flags *RootFlags) error {
return err
}

_, svc, err := requireGmailService(ctx, flags)
account, svc, err := requireGmailService(ctx, flags)
if err != nil {
return err
}

if err = checkAccountNoSend(account); err != nil {
return err
}

msg, err := svc.Users.Drafts.Send("me", &gmail.Draft{Id: draftID}).Do()
if err != nil {
return err
Expand Down
102 changes: 102 additions & 0 deletions internal/cmd/gmail_no_send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"strings"

"github.com/alecthomas/kong"

"github.com/steipete/gogcli/internal/config"
)

// gmailSendCommands lists command-path prefixes that transmit email.
var gmailSendCommands = [][]string{
{"send"}, // top-level alias → GmailSendCmd
{"gmail", "send"}, // gog gmail send
{"gmail", "drafts", "send"}, // gog gmail drafts send
{"gmail", "autoreply"}, // gog gmail autoreply
}

func enforceGmailNoSend(kctx *kong.Context, flagNoSend bool) error {
noSend := flagNoSend
source := "--gmail-no-send (GOG_GMAIL_NO_SEND)"

if !noSend {
if cfg, err := config.ReadConfig(); err == nil && cfg.GmailNoSend {
noSend = true
source = "gmail_no_send config"
}
}

if !noSend {
return nil
}

parts := strings.Fields(kctx.Command())
if len(parts) == 0 {
return nil
}

for i := range parts {
parts[i] = strings.ToLower(parts[i])
}

for _, prefix := range gmailSendCommands {
if hasCmdPrefix(parts, prefix) {
return usagef(
"send blocked by %s: use \"gog gmail drafts create\" to compose without sending",
source,
)
}
}

return nil
}

func hasCmdPrefix(parts, prefix []string) bool {
if len(parts) < len(prefix) {
return false
}

for i, p := range prefix {
if parts[i] != p {
return false
}
}

return true
}

// checkAccountNoSend reads the config and blocks send if the resolved account
// is in the no_send_accounts list. Call this after requireGmailService().
func checkAccountNoSend(account string) error {
cfg, err := config.ReadConfig()
if err != nil {
return err
}

if config.IsNoSendAccount(cfg, account) {
return usagef(
"send blocked for account %q (no_send_accounts): use \"gog gmail drafts create\" to compose without sending",
account,
)
}

return nil
}

// isGmailSendCommand reports whether the kong command string is a send path.
// Exported for testing.
func isGmailSendCommand(command string) bool {
parts := strings.Fields(strings.ToLower(command))
if len(parts) == 0 {
return false
}

for _, prefix := range gmailSendCommands {
if hasCmdPrefix(parts, prefix) {
return true
}
}

return false
}
Loading