Files
whatshooked/cmd/cli/commands_accounts.go
Hein a3eca09502
Some checks failed
CI / Lint (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Test (1.23) (push) Has been cancelled
CI / Test (1.22) (push) Has been cancelled
Lint fixes and testing workflow actions
2025-12-29 10:24:18 +02:00

90 lines
2.1 KiB
Go

package main
import (
"fmt"
"git.warky.dev/wdevs/whatshooked/pkg/config"
"github.com/spf13/cobra"
)
// accountsCmd is the parent command for account management
var accountsCmd = &cobra.Command{
Use: "accounts",
Short: "Manage WhatsApp accounts",
Run: func(cmd *cobra.Command, args []string) {
client := NewClient(cliConfig)
listAccounts(client)
},
}
var accountsListCmd = &cobra.Command{
Use: "list",
Short: "List all accounts",
Run: func(cmd *cobra.Command, args []string) {
client := NewClient(cliConfig)
listAccounts(client)
},
}
var accountsAddCmd = &cobra.Command{
Use: "add",
Short: "Add a new WhatsApp account",
Run: func(cmd *cobra.Command, args []string) {
client := NewClient(cliConfig)
addAccount(client)
},
}
func init() {
accountsCmd.AddCommand(accountsListCmd)
accountsCmd.AddCommand(accountsAddCmd)
}
func listAccounts(client *Client) {
resp, err := client.Get("/api/accounts")
checkError(err)
defer resp.Body.Close()
var accounts []config.WhatsAppConfig
checkError(decodeJSON(resp, &accounts))
if len(accounts) == 0 {
fmt.Println("No accounts configured")
return
}
fmt.Printf("Configured accounts (%d):\n\n", len(accounts))
for _, acc := range accounts {
fmt.Printf("ID: %s\n", acc.ID)
fmt.Printf("Phone Number: %s\n", acc.PhoneNumber)
fmt.Printf("Session Path: %s\n", acc.SessionPath)
fmt.Println()
}
}
func addAccount(client *Client) {
var account config.WhatsAppConfig
fmt.Print("Account ID: ")
if _, err := fmt.Scanln(&account.ID); err != nil {
checkError(fmt.Errorf("error reading account ID: %v", err))
}
fmt.Print("Phone Number (with country code): ")
if _, err := fmt.Scanln(&account.PhoneNumber); err != nil {
checkError(fmt.Errorf("error reading phone number: %v", err))
}
fmt.Print("Session Path: ")
if _, err := fmt.Scanln(&account.SessionPath); err != nil {
checkError(fmt.Errorf("error reading session path: %v", err))
}
resp, err := client.Post("/api/accounts/add", account)
checkError(err)
defer resp.Body.Close()
fmt.Println("Account added successfully")
fmt.Println("Check server logs for QR code to pair the device")
}