@@ -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
3233var 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" )
0 commit comments