From eca1068e44d567452da0906b5d57514556f7b15c Mon Sep 17 00:00:00 2001 From: Anony <73958752+anonyindian@users.noreply.github.com> Date: Wed, 10 Aug 2022 18:35:00 +0000 Subject: [PATCH] A few more improvements --- config/config.go | 7 +++++-- config/env.go | 7 ++++++- config/platform.go | 31 +++++++++++++++++++++++++++++++ main.go | 11 ++++++----- modules/prog.go | 13 ++----------- utils/system.go | 29 +++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 config/platform.go create mode 100644 utils/system.go diff --git a/config/config.go b/config/config.go index fd29204..5906cf7 100644 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,8 @@ type config struct { RedisPass string `json:"redis_pass"` TestSessionString string `json:"test_session_string"` SessionString string `json:"session_string"` + HerokuApiKey string `json:"heroku_api_key,omitempty"` + HerokuAppName string `json:"heroku_app_name,omitempty"` TestServer bool `json:"test_mode,omitempty"` BotToken string `json:"bot_token,omitempty"` } @@ -26,9 +28,10 @@ type config struct { func Load(l *logger.Logger) { l = l.Create("CONFIG") defer l.ChangeLevel(logger.LevelMain).Println("LOADED") + initPlatform() b, err := ioutil.ReadFile("config.json") if err != nil { - if err := ValueOf.setupEnvVars(); err != nil { + if err := ValueOf.setupEnvVars(l); err != nil { l.ChangeLevel(logger.LevelError).Println(err.Error()) os.Exit(1) } @@ -36,7 +39,7 @@ func Load(l *logger.Logger) { } err = json.Unmarshal(b, ValueOf) if err != nil { - l.ChangeLevel(logger.LevelError).Println("failed to load config:", err.Error()) + l.ChangeLevel(logger.LevelCritical).Println("failed to load config:", err.Error()) os.Exit(1) } } diff --git a/config/env.go b/config/env.go index 0556011..3b27cdb 100644 --- a/config/env.go +++ b/config/env.go @@ -8,10 +8,12 @@ import ( "strconv" "strings" + "github.com/anonyindian/logger" "github.com/joho/godotenv" ) -func (c *config) setupEnvVars() error { +func (c *config) setupEnvVars(l *logger.Logger) error { + l = l.Create("ENV") _ = godotenv.Load() val := reflect.ValueOf(c).Elem() notFoundArr := make([]string, 0) @@ -33,6 +35,9 @@ func (c *config) setupEnvVars() error { val.Field(i).SetBool(ev) } } + if Platform == Heroku && (ValueOf.HerokuApiKey == "" || ValueOf.HerokuAppName == "") { + l.ChangeLevel(logger.LevelError).Println("Please add HEROKU_API_KEY and HEROKU_APP_NAME otherwise GIGA would not work properly.") + } if len(notFoundArr) == 0 { return nil } diff --git a/config/platform.go b/config/platform.go new file mode 100644 index 0000000..fc80523 --- /dev/null +++ b/config/platform.go @@ -0,0 +1,31 @@ +package config + +import "os" + +var Platform platform + +type platform int + +const ( + Heroku platform = iota + Railway + Okteto + Local +) + +func initPlatform() { + switch { + case checkEnv("DYNO"): + Platform = Heroku + case checkEnv("RAILWAY_STATIC_URL"): + Platform = Railway + case checkEnv("OKTETO_TOKEN"): + Platform = Okteto + default: + Platform = Local + } +} + +func checkEnv(env string) bool { + return os.Getenv(env) != "" +} diff --git a/main.go b/main.go index 276b23c..0cf9c30 100644 --- a/main.go +++ b/main.go @@ -25,9 +25,10 @@ import ( ) var ( - delay = flag.Int("delay", 0, "") - restartMsgId = flag.Int("msg", 0, "") - restartChatId = flag.Int("chat", 0, "") + delay = flag.Int("delay", 0, "") + restartChatId = flag.Int("chat", 0, "") + restartMsgId = flag.Int("msg_id", 0, "") + restartMsgText = flag.String("msg_text", "", "") ) func main() { @@ -79,12 +80,12 @@ func runClient(l *logger.Logger) { } ctx := ext.NewContext(ctx, client.API(), gotgproto.Self, gotgproto.Sender, &tg.Entities{}) utils.TelegramClient = client - if *restartMsgId == 0 { + if *restartMsgId == 0 && *restartMsgText == "" { utils.StartupAutomations(l, ctx, client) } else { generic.EditMessage(ctx, *restartChatId, &tg.MessagesEditMessageRequest{ ID: *restartMsgId, - Message: "Restarted Successfully!", + Message: *restartMsgText, }) } // Modules shall not be loaded unless the setup is complete diff --git a/modules/prog.go b/modules/prog.go index 8f03398..57f2c70 100644 --- a/modules/prog.go +++ b/modules/prog.go @@ -1,16 +1,14 @@ package modules import ( - "fmt" "os" - "os/exec" - "strings" "github.com/anonyindian/gotgproto/dispatcher" "github.com/anonyindian/gotgproto/dispatcher/handlers" "github.com/anonyindian/gotgproto/ext" "github.com/anonyindian/logger" "github.com/gigauserbot/giga/bot/helpmaker" + "github.com/gigauserbot/giga/utils" "github.com/gotd/td/tg" ) @@ -43,13 +41,6 @@ func restart(ctx *ext.Context, u *ext.Update) error { ID: u.EffectiveMessage.ID, Message: "Restarting", }) - command := fmt.Sprintf("run main.go -delay=5 -chat=%d -msg=%d", chat.GetID(), u.EffectiveMessage.ID) - cmd := exec.Command("go", strings.Fields(command)...) - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin - cmd.Start() - cmd.Process.Release() - os.Exit(1) + utils.Restart(5, chat.GetID(), u.EffectiveMessage.ID, "Restarted Successfully!") return dispatcher.EndGroups } diff --git a/utils/system.go b/utils/system.go new file mode 100644 index 0000000..ab5709b --- /dev/null +++ b/utils/system.go @@ -0,0 +1,29 @@ +package utils + +import ( + "fmt" + "os" + "os/exec" + "strings" +) + +var tempDir = os.TempDir() + +func Restart(delay int, chatId int64, msgId int, msgText string) { + // command := fmt.Sprintf("run main.go -delay=5 -chat=%d -msg=%d", chat.GetID(), u.EffectiveMessage.ID) + args := []string{fmt.Sprintf("-delay=%d", delay), fmt.Sprintf("-chat=%d", chatId), fmt.Sprintf("-msg_id=%d", msgId), fmt.Sprintf("-msg_text=%s", msgText)} + command := []string{"run", "main.go"} + command = append(command, args...) + // fmt.Println(command) + executable, err := os.Executable() + if strings.Contains(executable, tempDir) || err != nil { + executable = "go" + } + cmd := exec.Command(executable, command...) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + cmd.Start() + cmd.Process.Release() + os.Exit(1) +}