CLI refactor
This commit is contained in:
83
cmd/cli/commands_accounts.go
Normal file
83
cmd/cli/commands_accounts.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.warky.dev/wdevs/whatshooked/internal/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: ")
|
||||
fmt.Scanln(&account.ID)
|
||||
|
||||
fmt.Print("Phone Number (with country code): ")
|
||||
fmt.Scanln(&account.PhoneNumber)
|
||||
|
||||
fmt.Print("Session Path: ")
|
||||
fmt.Scanln(&account.SessionPath)
|
||||
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user