Skip to content

Commit

Permalink
A few more improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 10, 2022
1 parent 6e50396 commit eca1068
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 19 deletions.
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@ 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"`
}

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)
}
return
}
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)
}
}
Expand Down
7 changes: 6 additions & 1 deletion config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions config/platform.go
Original file line number Diff line number Diff line change
@@ -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) != ""
}
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 2 additions & 11 deletions modules/prog.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions utils/system.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit eca1068

Please sign in to comment.