Skip to content

Commit 12bbfe9

Browse files
refactor(cmd): transform into a cli app
BREAKING CHANGE: The behavior of running the executable was changed.
1 parent cacca98 commit 12bbfe9

File tree

7 files changed

+180
-58
lines changed

7 files changed

+180
-58
lines changed

Diff for: Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ RUN CGO_ENABLED=0 go build -o /app/fsb -ldflags="-w -s" ./cmd/fsb
77
FROM scratch
88
COPY --from=builder /app/fsb /app/fsb
99
EXPOSE ${PORT}
10-
ENTRYPOINT ["/app/fsb"]
10+
ENTRYPOINT ["/app/fsb", "run"]

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
- Extract the zip file to a folder.
6969
- Create an a file named `fsb.env` and add all the variables there (see `fsb.sample.env` file for reference).
7070
- Give the executable file permission to execute using the command `chmod +x fsb` (Not required for windows).
71-
- Run the bot using `./fsb` command. ( `./fsb.exe` for windows)
71+
- Run the bot using `./fsb run` command. ( `./fsb.exe run` for windows)
7272

7373
<hr>
7474

@@ -124,7 +124,7 @@ chmod +x fsb
124124
mv fsb.sample.env fsb.env
125125
nano fsb.env
126126
# (add your environment variables, see the next section for more info)
127-
./fsb
127+
./fsb run
128128
```
129129

130130
and to stop the program,
@@ -142,7 +142,7 @@ go build ./cmd/fsb/
142142
Rename-Item -LiteralPath ".\fsb.sample.env" -NewName ".\fsb.env"
143143
notepad fsb.env
144144
# (add your environment variables, see the next section for more info)
145-
.\fsb
145+
.\fsb run
146146
```
147147

148148
and to stop the program,

Diff for: cmd/fsb/main.go

+21-50
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,35 @@ package main
22

33
import (
44
"EverythingSuckz/fsb/config"
5-
"EverythingSuckz/fsb/internal/bot"
6-
"EverythingSuckz/fsb/internal/cache"
7-
"EverythingSuckz/fsb/internal/routes"
8-
"EverythingSuckz/fsb/internal/types"
9-
"EverythingSuckz/fsb/internal/utils"
105
"fmt"
11-
"net/http"
12-
"time"
6+
"os"
137

14-
"github.com/gin-gonic/gin"
15-
"go.uber.org/zap"
8+
"github.com/spf13/cobra"
169
)
1710

1811
const versionString = "3.0.0-alpha1"
1912

20-
var startTime time.Time = time.Now()
21-
22-
func main() {
23-
utils.InitLogger()
24-
log := utils.Logger
25-
mainLogger := log.Named("Main")
26-
mainLogger.Info("Starting server")
27-
config.Load(log)
28-
router := getRouter(log)
29-
30-
_, err := bot.StartClient(log)
31-
if err != nil {
32-
log.Info(err.Error())
33-
return
34-
}
35-
cache.InitCache(log)
36-
bot.StartWorkers(log)
37-
bot.StartUserBot(log)
38-
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
39-
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
40-
err = router.Run(fmt.Sprintf(":%d", config.ValueOf.Port))
41-
if err != nil {
42-
mainLogger.Sugar().Fatalln(err)
43-
}
13+
var rootCmd = &cobra.Command{
14+
Use: "fsb [command]",
15+
Short: "Telegram File Stream Bot",
16+
Long: "Telegram Bot to generate direct streamable links for telegram media.",
17+
Example: "fsb run --port 8080",
18+
Version: versionString,
19+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
20+
Run: func(cmd *cobra.Command, args []string) {
21+
cmd.Help()
22+
},
23+
}
4424

25+
func init() {
26+
config.SetFlagsFromConfig(runCmd)
27+
rootCmd.AddCommand(runCmd)
28+
rootCmd.SetVersionTemplate(fmt.Sprintf(`Telegram File Stream Bot version %s`, versionString))
4529
}
4630

47-
func getRouter(log *zap.Logger) *gin.Engine {
48-
if config.ValueOf.Dev {
49-
gin.SetMode(gin.DebugMode)
50-
} else {
51-
gin.SetMode(gin.ReleaseMode)
31+
func main() {
32+
if err := rootCmd.Execute(); err != nil {
33+
fmt.Println(err)
34+
os.Exit(1)
5235
}
53-
router := gin.Default()
54-
router.Use(gin.ErrorLogger())
55-
router.GET("/", func(ctx *gin.Context) {
56-
ctx.JSON(http.StatusOK, types.RootResponse{
57-
Message: "Server is running.",
58-
Ok: true,
59-
Uptime: utils.TimeFormat(uint64(time.Since(startTime).Seconds())),
60-
Version: versionString,
61-
})
62-
})
63-
routes.Load(log, router)
64-
return router
6536
}

Diff for: cmd/fsb/run.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"EverythingSuckz/fsb/config"
5+
"EverythingSuckz/fsb/internal/bot"
6+
"EverythingSuckz/fsb/internal/cache"
7+
"EverythingSuckz/fsb/internal/routes"
8+
"EverythingSuckz/fsb/internal/types"
9+
"EverythingSuckz/fsb/internal/utils"
10+
"fmt"
11+
"net/http"
12+
"time"
13+
14+
"github.com/spf13/cobra"
15+
16+
"github.com/gin-gonic/gin"
17+
"go.uber.org/zap"
18+
)
19+
20+
var runCmd = &cobra.Command{
21+
Use: "run",
22+
Short: "Run the bot with the given configuration.",
23+
DisableSuggestions: false,
24+
Run: runApp,
25+
}
26+
27+
var startTime time.Time = time.Now()
28+
29+
func runApp(cmd *cobra.Command, args []string) {
30+
utils.InitLogger()
31+
log := utils.Logger
32+
mainLogger := log.Named("Main")
33+
mainLogger.Info("Starting server")
34+
config.Load(log, cmd)
35+
router := getRouter(log)
36+
37+
_, err := bot.StartClient(log)
38+
if err != nil {
39+
log.Info(err.Error())
40+
return
41+
}
42+
cache.InitCache(log)
43+
bot.StartWorkers(log)
44+
bot.StartUserBot(log)
45+
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
46+
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
47+
err = router.Run(fmt.Sprintf(":%d", config.ValueOf.Port))
48+
if err != nil {
49+
mainLogger.Sugar().Fatalln(err)
50+
}
51+
52+
}
53+
54+
func getRouter(log *zap.Logger) *gin.Engine {
55+
if config.ValueOf.Dev {
56+
gin.SetMode(gin.DebugMode)
57+
} else {
58+
gin.SetMode(gin.ReleaseMode)
59+
}
60+
router := gin.Default()
61+
router.Use(gin.ErrorLogger())
62+
router.GET("/", func(ctx *gin.Context) {
63+
ctx.JSON(http.StatusOK, types.RootResponse{
64+
Message: "Server is running.",
65+
Ok: true,
66+
Uptime: utils.TimeFormat(uint64(time.Since(startTime).Seconds())),
67+
Version: versionString,
68+
})
69+
})
70+
routes.Load(log, router)
71+
return router
72+
}

Diff for: config/config.go

+72-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/joho/godotenv"
1212
"github.com/kelseyhightower/envconfig"
13+
"github.com/spf13/cobra"
1314
"go.uber.org/zap"
1415
)
1516

@@ -31,7 +32,7 @@ type config struct {
3132

3233
var botTokenRegex = regexp.MustCompile(`MULTI\_TOKEN\d+=(.*)`)
3334

34-
func (c *config) setupEnvVars(log *zap.Logger) {
35+
func (c *config) loadFromEnvFile(log *zap.Logger) {
3536
envPath := filepath.Clean("fsb.env")
3637
log.Sugar().Infof("Trying to load ENV vars from %s", envPath)
3738
err := godotenv.Load(envPath)
@@ -45,7 +46,74 @@ func (c *config) setupEnvVars(log *zap.Logger) {
4546
log.Fatal("Unknown error while parsing env file.", zap.Error(err))
4647
}
4748
}
48-
err = envconfig.Process("", c)
49+
}
50+
51+
func SetFlagsFromConfig(cmd *cobra.Command) {
52+
cmd.Flags().Int32("api-id", ValueOf.ApiID, "Telegram API ID")
53+
cmd.Flags().String("api-hash", ValueOf.ApiHash, "Telegram API Hash")
54+
cmd.Flags().String("bot-token", ValueOf.BotToken, "Telegram Bot Token")
55+
cmd.Flags().Int64("log-channel", ValueOf.LogChannelID, "Telegram Log Channel ID")
56+
cmd.Flags().Bool("dev", ValueOf.Dev, "Enable development mode")
57+
cmd.Flags().IntP("port", "p", ValueOf.Port, "Server port")
58+
cmd.Flags().String("host", ValueOf.Host, "Server host that will be included in links")
59+
cmd.Flags().Int("hash-length", ValueOf.HashLength, "Hash length in links")
60+
cmd.Flags().Bool("use-session-file", ValueOf.UseSessionFile, "Use session files")
61+
cmd.Flags().String("user-session", ValueOf.UserSession, "Pyrogram user session")
62+
cmd.Flags().String("multi-token-txt-file", "", "Multi token txt file (Not implemented)")
63+
}
64+
65+
func (c *config) loadConfigFromArgs(log *zap.Logger, cmd *cobra.Command) {
66+
apiID, _ := cmd.Flags().GetInt32("api-id")
67+
if apiID != 0 {
68+
os.Setenv("API_ID", strconv.Itoa(int(apiID)))
69+
}
70+
apiHash, _ := cmd.Flags().GetString("api-hash")
71+
if apiHash != "" {
72+
os.Setenv("API_HASH", apiHash)
73+
}
74+
botToken, _ := cmd.Flags().GetString("bot-token")
75+
if botToken != "" {
76+
os.Setenv("BOT_TOKEN", botToken)
77+
}
78+
logChannelID, _ := cmd.Flags().GetString("log-channel")
79+
if logChannelID != "" {
80+
os.Setenv("LOG_CHANNEL", logChannelID)
81+
}
82+
dev, _ := cmd.Flags().GetBool("dev")
83+
if dev {
84+
os.Setenv("DEV", strconv.FormatBool(dev))
85+
}
86+
port, _ := cmd.Flags().GetInt("port")
87+
if port != 0 {
88+
os.Setenv("PORT", strconv.Itoa(port))
89+
}
90+
host, _ := cmd.Flags().GetString("host")
91+
if host != "" {
92+
os.Setenv("HOST", host)
93+
}
94+
hashLength, _ := cmd.Flags().GetInt("hash-length")
95+
if hashLength != 0 {
96+
os.Setenv("HASH_LENGTH", strconv.Itoa(hashLength))
97+
}
98+
useSessionFile, _ := cmd.Flags().GetBool("use-session-file")
99+
if useSessionFile {
100+
os.Setenv("USE_SESSION_FILE", strconv.FormatBool(useSessionFile))
101+
}
102+
userSession, _ := cmd.Flags().GetString("user-session")
103+
if userSession != "" {
104+
os.Setenv("USER_SESSION", userSession)
105+
}
106+
multiTokens, _ := cmd.Flags().GetString("multi-token-txt-file")
107+
if multiTokens != "" {
108+
os.Setenv("MULTI_TOKEN_TXT_FILE", multiTokens)
109+
// TODO: Add support for importing tokens from a separate file
110+
}
111+
}
112+
113+
func (c *config) setupEnvVars(log *zap.Logger, cmd *cobra.Command) {
114+
c.loadFromEnvFile(log)
115+
c.loadConfigFromArgs(log, cmd)
116+
err := envconfig.Process("", c)
49117
if err != nil {
50118
log.Fatal("Error while parsing env variables", zap.Error(err))
51119
}
@@ -58,10 +126,10 @@ func (c *config) setupEnvVars(log *zap.Logger) {
58126
val.FieldByName("MultiTokens").Set(reflect.ValueOf(c.MultiTokens))
59127
}
60128

61-
func Load(log *zap.Logger) {
129+
func Load(log *zap.Logger, cmd *cobra.Command) {
62130
log = log.Named("Config")
63131
defer log.Info("Loaded config")
64-
ValueOf.setupEnvVars(log)
132+
ValueOf.setupEnvVars(log, cmd)
65133
ValueOf.LogChannelID = int64(stripInt(log, int(ValueOf.LogChannelID)))
66134
if ValueOf.HashLength == 0 {
67135
log.Sugar().Info("HASH_LENGTH can't be 0, defaulting to 6")

Diff for: go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/joho/godotenv v1.5.1
1010
github.com/kelseyhightower/envconfig v1.4.0
1111
github.com/quantumsheep/range-parser v1.1.0
12+
github.com/spf13/cobra v1.8.0
1213
)
1314

1415
require (
@@ -24,12 +25,14 @@ require (
2425
github.com/google/uuid v1.4.0 // indirect
2526
github.com/gotd/ige v0.2.2 // indirect
2627
github.com/gotd/neo v0.1.5 // indirect
28+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2729
github.com/jinzhu/inflection v1.0.0 // indirect
2830
github.com/jinzhu/now v1.1.5 // indirect
2931
github.com/klauspost/compress v1.17.4 // indirect
3032
github.com/pkg/errors v0.9.1 // indirect
3133
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
3234
github.com/segmentio/asm v1.2.0 // indirect
35+
github.com/spf13/pflag v1.0.5 // indirect
3336
go.opentelemetry.io/otel v1.21.0 // indirect
3437
go.opentelemetry.io/otel/trace v1.21.0 // indirect
3538
go.uber.org/atomic v1.11.0 // indirect

Diff for: go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhD
1414
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
1515
github.com/coocood/freecache v1.2.4 h1:UdR6Yz/X1HW4fZOuH0Z94KwG851GWOSknua5VUbb/5M=
1616
github.com/coocood/freecache v1.2.4/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
17+
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1718
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1819
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1920
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -61,6 +62,8 @@ github.com/gotd/neo v0.1.5 h1:oj0iQfMbGClP8xI59x7fE/uHoTJD7NZH9oV1WNuPukQ=
6162
github.com/gotd/neo v0.1.5/go.mod h1:9A2a4bn9zL6FADufBdt7tZt+WMhvZoc5gWXihOPoiBQ=
6263
github.com/gotd/td v0.89.0 h1:qRWbTmPYk5y/u4gNAj9qIkLJlQgdMGr4HzW2ix1Q2hA=
6364
github.com/gotd/td v0.89.0/go.mod h1:NgvwaHPW8rAHPGjaKSKzwSe+N2cUWTmfaDs8P6HPp/U=
65+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
66+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6467
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
6568
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
6669
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -99,8 +102,13 @@ github.com/quantumsheep/range-parser v1.1.0 h1:k4f1F58f8FF54FBYc9dYBRM+8JkAxFo11
99102
github.com/quantumsheep/range-parser v1.1.0/go.mod h1:acv4Vt2PvpGvRsvGju7Gk2ahKluZJsIUNR69W53J22I=
100103
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
101104
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
105+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
102106
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
103107
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
108+
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
109+
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
110+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
111+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
104112
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
105113
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
106114
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

0 commit comments

Comments
 (0)