Skip to content
Merged
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
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<br />

<div align="center">
<h3 align="center">SMS Gateway for Android™ CLI</h3>
<h3 align="center">SMSGate CLI</h3>

<p align="center">
A command-line interface for interacting with the SMS Gateway for Android API
A command-line interface for interacting with the SMSGate API
<br />
<a href="https://docs.sms-gate.app/integration/cli/"><strong>Explore the docs »</strong></a>
<br />
Expand All @@ -40,6 +40,9 @@
- [Commands](#commands)
- [Exit codes](#exit-codes)
- [Examples](#examples)
- [Sending messages](#sending-messages)
- [Getting message status](#getting-message-status)
- [Getting logs](#getting-logs)
- [Output formats](#output-formats-1)
- [👥 Contributing](#-contributing)
- [©️ License](#️-license)
Expand Down Expand Up @@ -130,10 +133,11 @@ smsgate [global options] command [command options] [arguments...]

### Commands

The CLI offers two main groups of commands:
The CLI offers three main groups of commands:

- **Messages**: Commands for sending messages and checking their status.
- **Webhooks**: Commands for managing webhooks, including creating, updating, and deleting them.
- **Logs**: Commands for retrieving logs for a specific time range.

For a complete list of available commands, you can:
- Run `smsgate help` or `smsgate --help` in your terminal.
Expand All @@ -152,6 +156,14 @@ The CLI uses exit codes to indicate the outcome of operations:

For security reasons, it is recommended to pass credentials using environment variables or a `.env` file.

Credentials can also be passed via CLI options:

```bash
smsgate -u <username> -p <password> send --phones '+12025550123' 'Hello, Dr. Turk!'
```

#### Sending messages

```bash
# Send a message
smsgate send --phones '+12025550123' 'Hello, Dr. Turk!'
Expand All @@ -160,15 +172,28 @@ smsgate send --phones '+12025550123' 'Hello, Dr. Turk!'
smsgate send --phones '+12025550123' --phones '+12025550124' 'Hello, doctors!'
# or
smsgate send --phones '+12025550123,+12025550124' 'Hello, doctors!'
```

#### Getting message status

```bash
# Get the status of a sent message
smsgate status zXDYfTmTVf3iMd16zzdBj
```

Credentials can also be passed via CLI options:
#### Getting logs

The `logs` command retrieves logs for a specific time range. Dates should be in RFC3339 format (e.g., `2024-01-15T10:30:00Z`).

```bash
smsgate -u <username> -p <password> send --phones '+12025550123' 'Hello, Dr. Turk!'
# Get logs for the last 24 hours (default)
smsgate logs

# Get logs for a specific time range
smsgate logs --from '2024-01-15T00:00:00Z' --to '2024-01-15T23:59:59Z'

# Get logs with custom time range and output format
smsgate --format json logs --from '2024-01-15T10:00:00+07:00' --to '2024-01-15T18:00:00+07:00'
```

#### Output formats
Expand Down
4 changes: 3 additions & 1 deletion cmd/smsgate/smsgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/android-sms-gateway/cli/internal/commands/logs"
"github.com/android-sms-gateway/cli/internal/commands/messages"
"github.com/android-sms-gateway/cli/internal/commands/webhooks"
"github.com/android-sms-gateway/cli/internal/config"
Expand All @@ -25,9 +26,10 @@ func main() {
os.Exit(codes.ParamsError)
}

cmds := make([]*cli.Command, 0, len(messages.Commands())+len(webhooks.Commands()))
cmds := make([]*cli.Command, 0, len(messages.Commands())+len(webhooks.Commands())+len(logs.Commands()))
cmds = append(cmds, messages.Commands()...)
cmds = append(cmds, webhooks.Commands()...)
cmds = append(cmds, logs.Commands()...)

app := &cli.App{
Name: "smsgate",
Expand Down
1 change: 1 addition & 0 deletions dev-docs
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/android-sms-gateway/cli
go 1.23.2

require (
github.com/android-sms-gateway/client-go v1.9.2
github.com/android-sms-gateway/client-go v1.12.0
github.com/joho/godotenv v1.5.1
github.com/samber/lo v1.52.0
github.com/urfave/cli/v2 v2.27.5
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/android-sms-gateway/client-go v1.9.2 h1:e9HFgvR+LRMV0dOJvFkxt998UxOMWNf8hfnXwMIc39I=
github.com/android-sms-gateway/client-go v1.9.2/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
github.com/android-sms-gateway/client-go v1.11.1-0.20260315032244-641ce286d8b5 h1:04bQhao7QKaD5QwsHI8PDpd32ZAYO7Cw1/5bRMbfuNM=
github.com/android-sms-gateway/client-go v1.11.1-0.20260315032244-641ce286d8b5/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
github.com/android-sms-gateway/client-go v1.12.0 h1:4YWnzLi4AWEQIXvvUSLiTK+ZAgQD6SV+xfvlTCjZ8pI=
github.com/android-sms-gateway/client-go v1.12.0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand Down
66 changes: 66 additions & 0 deletions internal/commands/logs/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package logs

import (
"fmt"
"os"

"github.com/android-sms-gateway/cli/internal/core/codes"
"github.com/android-sms-gateway/cli/internal/flags"
"github.com/android-sms-gateway/cli/internal/utils/metadata"
"github.com/urfave/cli/v2"
)

func Commands() []*cli.Command {
return []*cli.Command{logsCmd()}
}

func logsCmd() *cli.Command {
f := flags.Period()

return &cli.Command{
Name: "logs",
Aliases: []string{"log"},
Usage: "Get logs for a specific time range",
Category: "Logs",
Flags: f,
Before: logsBefore,
Action: logsAction,
}
}

func logsBefore(c *cli.Context) error {
period := flags.ParsePeriodFlags(c)

if period.From == nil || period.To == nil {
return cli.Exit("From and To dates are required", codes.ParamsError)
}

if period.From.After(*period.To) {
return cli.Exit("From date must be less than or equal to To date", codes.ParamsError)
}

return nil
}

func logsAction(c *cli.Context) error {
period := flags.ParsePeriodFlags(c)

from := period.From
to := period.To

client := metadata.GetClient(c.App.Metadata)
renderer := metadata.GetRenderer(c.App.Metadata)

res, err := client.GetLogs(c.Context, *from, *to)
if err != nil {
return cli.Exit(err.Error(), codes.ClientError)
}

output, err := renderer.Logs(res)
if err != nil {
return cli.Exit(err.Error(), codes.OutputError)
}
fmt.Fprintln(os.Stdout, output)

return nil
}
1 change: 1 addition & 0 deletions internal/core/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ func New(username, password, endpoint string) *smsgateway.Client {
BaseURL: endpoint,
User: username,
Password: password,
Token: "",
})
}
4 changes: 4 additions & 0 deletions internal/core/output/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (o *JSONOutput) MessageState(src smsgateway.MessageState) (string, error) {
return o.marshaler(src)
}

func (o *JSONOutput) Logs(src []smsgateway.LogEntry) (string, error) {
return o.marshaler(src)
}

func (o *JSONOutput) Webhook(src smsgateway.Webhook) (string, error) {
return o.marshaler(src)
}
Expand Down
1 change: 1 addition & 0 deletions internal/core/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (

type Renderer interface {
MessageState(src smsgateway.MessageState) (string, error)
Logs(src []smsgateway.LogEntry) (string, error)
Webhook(src smsgateway.Webhook) (string, error)
Webhooks(src []smsgateway.Webhook) (string, error)
Success() (string, error)
Expand Down
38 changes: 38 additions & 0 deletions internal/core/output/text.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package output

import (
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -69,6 +71,42 @@ func (*TextOutput) MessageState(src smsgateway.MessageState) (string, error) {
return builder.String(), nil
}

func (*TextOutput) Logs(src []smsgateway.LogEntry) (string, error) {
if len(src) == 0 {
return "Empty result", nil
}

builder := strings.Builder{}
for i, entry := range src {
builder.WriteString("ID: ")
builder.WriteString(strconv.FormatUint(entry.ID, 10))
builder.WriteString("\nPriority: ")
builder.WriteString(string(entry.Priority))
builder.WriteString("\nModule: ")
builder.WriteString(entry.Module)
builder.WriteString("\nMessage: ")
builder.WriteString(entry.Message)
builder.WriteString("\nCreated At: ")
builder.WriteString(entry.CreatedAt.Local().Format(time.RFC3339))

if len(entry.Context) > 0 {
builder.WriteString("\nContext:")
for k, v := range entry.Context {
builder.WriteString("\n\t")
builder.WriteString(k)
builder.WriteString(": ")
fmt.Fprintf(&builder, "%v", v)
}
}

if i < len(src)-1 {
builder.WriteString("\n---\n")
}
}

return builder.String(), nil
}

// Webhook formats a single smsgateway.Webhook into a string representation.
// The output includes the ID, Event, and URL of the webhook.
func (*TextOutput) Webhook(src smsgateway.Webhook) (string, error) {
Expand Down
38 changes: 38 additions & 0 deletions internal/flags/period.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package flags

import (
"time"

"github.com/urfave/cli/v2"
)

func Period() []cli.Flag {
return []cli.Flag{
&cli.TimestampFlag{
Name: "from",
Usage: "Start of time range (RFC3339 format)",
Layout: time.RFC3339,
Timezone: time.Local,
Value: cli.NewTimestamp(time.Now().Add(-24 * time.Hour)),
},
&cli.TimestampFlag{
Name: "to",
Usage: "End of time range (RFC3339 format)",
Layout: time.RFC3339,
Timezone: time.Local,
Value: cli.NewTimestamp(time.Now()),
},
}
}

type PeriodFlags struct {
From *time.Time
To *time.Time
}

func ParsePeriodFlags(c *cli.Context) PeriodFlags {
return PeriodFlags{
From: c.Timestamp("from"),
To: c.Timestamp("to"),
}
}
5 changes: 4 additions & 1 deletion tests/e2e/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package e2e

import (
"bytes"
"e2e/testutils"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHelpFlag(t *testing.T) {
binPath := testutils.RequireBinPath(t)

// Run the CLI binary with the --help flag
var stdout, stderr bytes.Buffer

cmd := exec.Command("./smsgate", "--help")
cmd := exec.Command(binPath, "--help")
cmd.Stdout = &stdout
cmd.Stderr = &stderr

Expand Down
Loading
Loading