From 862c3a7cc05ac0c85521150d4e73230d53cd0028 Mon Sep 17 00:00:00 2001 From: abdfnx Date: Sat, 17 Sep 2022 15:05:01 +0300 Subject: [PATCH] fix tokens handler --- cmd/app/docker.go | 9 ++-- cmd/app/init.go | 3 -- internal/pipes/initx/docker.go | 11 ++-- internal/railway/deploy.go | 2 - internal/render/auth.go | 8 +++ internal/render/deploy.go | 3 -- tools/check-dir.go | 5 ++ tools/setup-tokens-in-docker.go | 93 --------------------------------- 8 files changed, 20 insertions(+), 114 deletions(-) delete mode 100644 tools/setup-tokens-in-docker.go diff --git a/cmd/app/docker.go b/cmd/app/docker.go index ec650c38..f9194b0a 100644 --- a/cmd/app/docker.go +++ b/cmd/app/docker.go @@ -24,12 +24,9 @@ func DockerCMD() *cobra.Command { func DockerBuildCMD() *cobra.Command { cmd := &cobra.Command{ - Use: "build", - Short: "Build your bot docker image", - PreRun: func(cmd *cobra.Command, args []string) { - tools.CheckDir() - tools.SetupTokensInDocker() - }, + Use: "build", + Short: "Build your bot docker image", + PreRun: func(cmd *cobra.Command, args []string) { tools.CheckDir() }, Run: func(cmd *cobra.Command, args []string) { build_image.DockerBuildImage() }, PostRun: func(cmd *cobra.Command, args []string) { tools.RemoveConfig() }, } diff --git a/cmd/app/init.go b/cmd/app/init.go index 489f8f21..eb727c2a 100755 --- a/cmd/app/init.go +++ b/cmd/app/init.go @@ -3,7 +3,6 @@ package app import ( "github.com/abdfnx/botway/internal/options" "github.com/abdfnx/botway/internal/pipes/initx" - "github.com/abdfnx/botway/tools" "github.com/spf13/cobra" ) @@ -19,8 +18,6 @@ func InitCMD() *cobra.Command { Aliases: []string{"."}, Run: func(cmd *cobra.Command, args []string) { if opts.Docker { - tools.SetupTokensInDocker() - initx.DockerInit() } else { initx.Init() diff --git a/internal/pipes/initx/docker.go b/internal/pipes/initx/docker.go index f5472f8a..8d261f20 100755 --- a/internal/pipes/initx/docker.go +++ b/internal/pipes/initx/docker.go @@ -43,18 +43,15 @@ func DockerInit() { bot_token = "TELEGRAM_TOKEN" } - env := viper.New() - env.SetConfigType("env") - - viper.SetDefault("botway.bots."+botwaygo.GetBotInfo("bot.name")+".bot_token", env.GetString(bot_token)) + viper.SetDefault("botway.bots."+botwaygo.GetBotInfo("bot.name")+".bot_token", os.Getenv(bot_token)) viper.SetDefault("botway.bots_names", []string{botwaygo.GetBotInfo("bot.name")}) if botType != "telegram" { - viper.SetDefault("botway.bots."+botwaygo.GetBotInfo("bot.name")+"."+cid, env.GetString(app_token)) + viper.SetDefault("botway.bots."+botwaygo.GetBotInfo("bot.name")+"."+cid, os.Getenv(app_token)) } if botType == "slack" { - viper.SetDefault("botway.bots."+botwaygo.GetBotInfo("bot.name")+".signing_secret", env.GetString(signing_secret)) + viper.SetDefault("botway.bots."+botwaygo.GetBotInfo("bot.name")+".signing_secret", os.Getenv(signing_secret)) } if botType == "discord" { @@ -68,7 +65,7 @@ func DockerInit() { sgi := strings.ToUpper(server) + "_GUILD_ID" - viper.Set("botway.bots."+botwaygo.GetBotInfo("bot.name")+".guilds."+server+".server_id", env.GetString(sgi)) + viper.Set("botway.bots."+botwaygo.GetBotInfo("bot.name")+".guilds."+server+".server_id", os.Getenv(sgi)) } } } diff --git a/internal/railway/deploy.go b/internal/railway/deploy.go index b23e9a4b..c7dee603 100644 --- a/internal/railway/deploy.go +++ b/internal/railway/deploy.go @@ -7,7 +7,6 @@ import ( "time" "github.com/abdfnx/botway/constants" - "github.com/abdfnx/botway/tools" "github.com/abdfnx/botwaygo" "github.com/botwayorg/railway-api/entity" CLIErrors "github.com/botwayorg/railway-api/errors" @@ -17,7 +16,6 @@ import ( func (h *Handler) Delpoy(ctx context.Context, req *entity.CommandRequest) error { CheckBuildKit() - tools.SetupTokensInDocker() isVerbose, err := req.Cmd.Flags().GetBool("verbose") if err != nil { diff --git a/internal/render/auth.go b/internal/render/auth.go index c8645859..98c98b47 100644 --- a/internal/render/auth.go +++ b/internal/render/auth.go @@ -17,6 +17,14 @@ import ( ) func (m model) Auth() { + if _, err := os.Stat(constants.RenderConfigFile); os.IsNotExist(err) { + renderConfigFile := os.WriteFile(constants.RenderConfigFile, []byte("{}"), 0644) + + if renderConfigFile != nil { + log.Fatal(renderConfigFile) + } + } + email := strings.ReplaceAll(m.inputs[1].Value(), "@", "%40") url := fmt.Sprintf("https://api.render.com/v1/owners?name=%s&email=%s&limit=20", m.inputs[0].Value(), email) diff --git a/internal/render/deploy.go b/internal/render/deploy.go index 8f9a2303..af8f36e3 100644 --- a/internal/render/deploy.go +++ b/internal/render/deploy.go @@ -7,12 +7,9 @@ import ( "strings" "github.com/abdfnx/botway/constants" - "github.com/abdfnx/botway/tools" ) func Deploy() { - tools.SetupTokensInDocker() - UpdateTokens(serviceId) url := fmt.Sprintf("https://api.render.com/v1/services/%s/deploys", serviceId) diff --git a/tools/check-dir.go b/tools/check-dir.go index 34d8fea9..a8055b03 100644 --- a/tools/check-dir.go +++ b/tools/check-dir.go @@ -3,6 +3,7 @@ package tools import ( "fmt" "os" + "path/filepath" "github.com/abdfnx/botway/constants" ) @@ -14,3 +15,7 @@ func CheckDir() { panic(constants.FAIL_FOREGROUND.Render("You need to run this command in your bot directory")) } } + +func RemoveConfig() { + os.Remove(filepath.Join("config", "botway-tokens.env")) +} diff --git a/tools/setup-tokens-in-docker.go b/tools/setup-tokens-in-docker.go deleted file mode 100644 index 2ca7a957..00000000 --- a/tools/setup-tokens-in-docker.go +++ /dev/null @@ -1,93 +0,0 @@ -package tools - -import ( - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/abdfnx/botway/constants" - "github.com/abdfnx/botwaygo" - "github.com/spf13/viper" - "github.com/tidwall/gjson" -) - -var ( - botName = botwaygo.GetBotInfo("bot.name") - botType = botwaygo.GetBotInfo("bot.type") - - bot_token = "" - app_token = "" - signing_secret = "SLACK_SIGNING_SECRET" - cid = "" -) - -func SetupTokensInDocker() { - CheckDir() - - if botType == "discord" { - bot_token = "DISCORD_TOKEN" - app_token = "DISCORD_CLIENT_ID" - cid = "bot_app_id" - } else if botType == "slack" { - bot_token = "SLACK_TOKEN" - app_token = "SLACK_APP_TOKEN" - cid = "bot_app_token" - } else if botType == "telegram" { - bot_token = "TELEGRAM_TOKEN" - } - - env := viper.New() - - pwd, _ := os.Getwd() - - env.AddConfigPath(filepath.Join(pwd, "config")) - env.SetConfigName("botway-tokens") - env.SetConfigType("env") - - env.SetDefault(bot_token, os.Getenv(bot_token)) - - if botType != "telegram" { - env.SetDefault(app_token, os.Getenv(bot_token)) - } - - if botType == "discord" { - if constants.Gerr != nil { - panic(constants.Gerr) - } else { - guilds := gjson.Get(string(constants.Guilds), "guilds.#") - - for x := 0; x < int(guilds.Int()); x++ { - server := gjson.Get(string(constants.Guilds), "guilds."+fmt.Sprint(x)).String() - - sgi := strings.ToUpper(server) + "_GUILD_ID" - - env.Set(sgi, os.Getenv(sgi)) - } - } - } - - if botType == "slack" { - env.SetDefault(signing_secret, os.Getenv(signing_secret)) - } - - if err := env.SafeWriteConfig(); err != nil { - if os.IsNotExist(err) { - err = env.WriteConfig() - - if err != nil { - panic(err) - } - } - } - - if err := env.ReadInConfig(); err != nil { - if _, ok := err.(viper.ConfigFileNotFoundError); ok { - panic(err) - } - } -} - -func RemoveConfig() { - os.Remove(filepath.Join("config", "botway-tokens.env")) -}