Skip to content

Commit ad57409

Browse files
committed
[lint] fix issues
1 parent fbb0eb3 commit ad57409

22 files changed

Lines changed: 487 additions & 374 deletions

File tree

‎.golangci.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ linters:
251251
- ^net/url.URL$
252252
- ^os/exec.Cmd$
253253
- ^reflect.StructField$
254+
- ^crypto/x509.CertificateRequest$
255+
- ^crypto/x509/pkix.Name$
254256
# public libs
255257
- ^firebase.google.com/go/v4/messaging.AndroidConfig$
256258
- ^firebase.google.com/go/v4/messaging.Message$
@@ -284,6 +286,7 @@ linters:
284286
- ^gopkg.in/telebot.v4.Settings$
285287
- ^gopkg.in/yaml.v3.Node$
286288
- ^gorm.io/gorm/clause.+$
289+
- ^github.com/urfave/cli/v2.+$
287290
# Allows empty structures in return statements.
288291
# Default: false
289292
allow-empty-returns: true

‎cmd/smsgate-ca/smsgate-ca.go‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
"github.com/android-sms-gateway/cli/internal/commands/ca"
89
"github.com/android-sms-gateway/cli/internal/core/codes"
@@ -22,11 +23,27 @@ func main() {
2223
os.Exit(codes.ParamsError)
2324
}
2425

26+
const defaultTimeout = 30 * time.Second
27+
28+
cmds := ca.Commands()
29+
2530
app := &cli.App{
2631
Name: "smsgate-ca",
27-
Usage: "CLI interface for interacting with Certificate Authority of SMS Gateway for Android™",
32+
Usage: "CLI interface for interacting with Certificate Authority of SMSGate",
2833
Version: version,
29-
Commands: make([]*cli.Command, 0, len(ca.Commands)),
34+
Commands: cmds,
35+
Flags: []cli.Flag{
36+
&cli.DurationFlag{
37+
Name: "timeout",
38+
Aliases: []string{"t"},
39+
Usage: "Request timeout",
40+
Required: false,
41+
Value: defaultTimeout,
42+
EnvVars: []string{
43+
"ASG_CA_TIMEOUT",
44+
},
45+
},
46+
},
3047
Authors: []*cli.Author{
3148
{
3249
Name: "Aleksandr Soloshenko",
@@ -35,12 +52,19 @@ func main() {
3552
},
3653
Before: func(c *cli.Context) error {
3754
c.App.Metadata[metadata.CAClientKey] = client.NewClient()
55+
56+
timeout := c.Duration("timeout")
57+
if timeout <= 0 {
58+
return cli.Exit(
59+
"Timeout must be greater than 0",
60+
codes.ParamsError,
61+
)
62+
}
63+
3864
return nil
3965
},
4066
}
4167

42-
app.Commands = append(app.Commands, ca.Commands...)
43-
4468
if err := app.Run(os.Args); err != nil {
4569
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
4670
os.Exit(codes.ParamsError)

‎cmd/smsgate/smsgate.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ func main() {
2525
os.Exit(codes.ParamsError)
2626
}
2727

28+
cmds := make([]*cli.Command, 0, len(messages.Commands())+len(webhooks.Commands()))
29+
cmds = append(cmds, messages.Commands()...)
30+
cmds = append(cmds, webhooks.Commands()...)
31+
2832
app := &cli.App{
2933
Name: "smsgate",
3034
Usage: "CLI interface for working with SMS Gateway for Android™",
3135
Version: version,
32-
Commands: make([]*cli.Command, 0, len(messages.Commands)+len(webhooks.Commands)),
36+
Commands: cmds,
3337
Flags: []cli.Flag{
3438
&cli.StringFlag{
3539
Name: "endpoint",
@@ -81,18 +85,19 @@ func main() {
8185
Before: func(c *cli.Context) error {
8286
renderer, err := output.New(output.Format(c.String("format")))
8387
if err != nil {
84-
return err
88+
return fmt.Errorf("failed to create renderer: %w", err)
8589
}
8690

8791
c.App.Metadata[metadata.RendererKey] = renderer
88-
c.App.Metadata[metadata.ClientKey] = client.New(c.String("username"), c.String("password"), c.String("endpoint"))
92+
c.App.Metadata[metadata.ClientKey] = client.New(
93+
c.String("username"),
94+
c.String("password"),
95+
c.String("endpoint"),
96+
)
8997
return nil
9098
},
9199
}
92100

93-
app.Commands = append(app.Commands, messages.Commands...)
94-
app.Commands = append(app.Commands, webhooks.Commands...)
95-
96101
if err := app.Run(os.Args); err != nil {
97102
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
98103
os.Exit(codes.ParamsError)

‎go.mod‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ go 1.23.2
44

55
require (
66
github.com/android-sms-gateway/client-go v1.9.2
7-
github.com/capcom6/go-helpers v0.4.0
87
github.com/joho/godotenv v1.5.1
8+
github.com/samber/lo v1.52.0
99
github.com/urfave/cli/v2 v2.27.5
1010
)
1111

1212
require (
1313
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
1414
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1515
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
16+
golang.org/x/text v0.22.0 // indirect
1617
)

‎go.sum‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
github.com/android-sms-gateway/client-go v1.9.2 h1:e9HFgvR+LRMV0dOJvFkxt998UxOMWNf8hfnXwMIc39I=
22
github.com/android-sms-gateway/client-go v1.9.2/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
3-
github.com/capcom6/go-helpers v0.4.0 h1:z2G7YFqNn3pgsdn8wVO/ZoLd/P2ckk5p97vbrGqwBAY=
4-
github.com/capcom6/go-helpers v0.4.0/go.mod h1:WDqc7HZNqHxUTisArkYIBZtqUfJBVyPWeQI+FMwEzAw=
53
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
64
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
75
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
86
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
97
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
108
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9+
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
10+
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
1111
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
1212
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
1313
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
1414
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
15+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
16+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=

‎internal/commands/ca/ca.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
package ca
22

33
import (
4+
"github.com/android-sms-gateway/cli/internal/commands/ca/common"
5+
"github.com/android-sms-gateway/client-go/ca"
46
"github.com/urfave/cli/v2"
57
)
68

7-
var Commands = []*cli.Command{
8-
webhooks,
9-
private,
9+
func Commands() []*cli.Command {
10+
return []*cli.Command{
11+
common.NewIPCertificateCommand(
12+
"webhooks",
13+
"Issue a new certificate for receiving webhooks to local IP address",
14+
[]string{"wh"},
15+
ca.CSRTypeWebhook,
16+
),
17+
common.NewIPCertificateCommand(
18+
"private",
19+
"Issue a new certificate for Private server",
20+
[]string{"p"},
21+
ca.CSRTypePrivateServer,
22+
),
23+
}
1024
}

‎internal/commands/ca/common/command.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// NewIPCertificateCommand creates a new CLI command for generating an IP certificate
15-
// of the specified type
15+
// of the specified type.
1616
func NewIPCertificateCommand(name, usage string, aliases []string, typ ca.CSRType) *cli.Command {
1717
return &cli.Command{
1818
Name: name,
@@ -59,5 +59,4 @@ func NewIPCertificateCommand(name, usage string, aliases []string, typ ca.CSRTyp
5959
return requestCertificate(c, typ, csrTemplate)
6060
},
6161
}
62-
6362
}

‎internal/commands/ca/common/utils.go‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"crypto/x509/pkix"
99
"encoding/pem"
1010
"fmt"
11-
"log"
11+
"log" //nolint:depguard // TODO: replace with logger from mariadb-backup-s3
1212
"os"
1313
"time"
1414

@@ -38,7 +38,7 @@ func newServerCertificateRequestPEM(template x509.CertificateRequest, priv *ecds
3838
return nil, fmt.Errorf("failed to create certificate request: %w", err)
3939
}
4040

41-
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes}), nil
41+
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Headers: nil, Bytes: csrBytes}), nil
4242
}
4343

4444
func requestCertificate(c *cli.Context, typ ca.CSRType, template x509.CertificateRequest) error {
@@ -54,8 +54,9 @@ func requestCertificate(c *cli.Context, typ ca.CSRType, template x509.Certificat
5454
}
5555

5656
privPemBytes := pem.EncodeToMemory(&pem.Block{
57-
Type: "EC PRIVATE KEY",
58-
Bytes: privBytes,
57+
Type: "EC PRIVATE KEY",
58+
Headers: nil,
59+
Bytes: privBytes,
5960
})
6061

6162
log.Println("Creating certificate request...")
@@ -68,14 +69,15 @@ func requestCertificate(c *cli.Context, typ ca.CSRType, template x509.Certificat
6869
client := metadata.GetCAClient(c.App.Metadata)
6970

7071
resp, err := client.PostCSR(c.Context, ca.PostCSRRequest{
71-
Type: typ,
72-
Content: string(csrPemBytes),
72+
Type: typ,
73+
Content: string(csrPemBytes),
74+
Metadata: nil,
7375
})
7476
if err != nil {
7577
return cli.Exit(err.Error(), codes.ClientError)
7678
}
7779

78-
timeout := time.After(30 * time.Second)
80+
timeout := time.After(c.Duration("timeout"))
7981
for resp.Certificate == "" {
8082
select {
8183
case <-c.Context.Done():
@@ -93,12 +95,14 @@ func requestCertificate(c *cli.Context, typ ca.CSRType, template x509.Certificat
9395
}
9496

9597
log.Println("Saving certificate...")
96-
if err := os.WriteFile(c.String("out"), []byte(resp.Certificate), 0644); err != nil {
97-
return cli.Exit(err.Error(), codes.OutputError)
98+
if wrErr := os.WriteFile(c.String("out"), []byte(resp.Certificate), 0600); wrErr != nil {
99+
return cli.Exit(wrErr.Error(), codes.OutputError)
98100
}
99-
if err := os.WriteFile(c.String("keyout"), privPemBytes, 0400); err != nil {
100-
os.Remove(c.String("out"))
101-
return cli.Exit(err.Error(), codes.OutputError)
101+
if wrErr := os.WriteFile(c.String("keyout"), privPemBytes, 0400); wrErr != nil {
102+
if rmErr := os.Remove(c.String("out")); rmErr != nil {
103+
log.Printf("Failed to remove certificate file %s: %s", c.String("out"), rmErr.Error())
104+
}
105+
return cli.Exit(wrErr.Error(), codes.OutputError)
102106
}
103107

104108
log.Printf("Certificate saved to %s\n", c.String("out"))

‎internal/commands/ca/private.go‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎internal/commands/ca/webhooks.go‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)