Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
update botway docker-init command
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Sep 21, 2022
1 parent f20b1fa commit 5212cfd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 88 deletions.
5 changes: 1 addition & 4 deletions cmd/app/docker-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ func DockerInitCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "docker-init",
Short: "Initialize ~/.botway for docker containers",
Run: func(cmd *cobra.Command, args []string) {
initx.SetupTokensInDocker()
initx.DockerInit()
},
Run: func(cmd *cobra.Command, args []string) { initx.DockerInit() },
}

return cmd
Expand Down
83 changes: 4 additions & 79 deletions internal/pipes/initx/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,6 @@ import (
"github.com/tidwall/gjson"
)

func SetupTokensInDocker() {
tools.CheckDir()

var (
botType = botwaygo.GetBotInfo("bot.type")

bot_token = ""
app_token = ""
signing_secret = "SLACK_SIGNING_SECRET"
)

if botType == "discord" {
bot_token = "DISCORD_TOKEN"
app_token = "DISCORD_CLIENT_ID"
} else if botType == "slack" {
bot_token = "SLACK_TOKEN"
app_token = "SLACK_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 CopyConfig() {
tools.Copy(constants.BotwayConfigFile, "botway.json")

Expand Down Expand Up @@ -122,18 +50,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" {
Expand All @@ -147,7 +72,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))
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions internal/pipes/new/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,21 @@ func NewBot(m model, l string, platform, lang int) {
}

dotGitIgnoreFile := os.WriteFile(filepath.Join(opts.BotName, ".gitignore"), []byte(dotGitIgnoreFileContent), 0644)
dotGitKeepFile := os.WriteFile(filepath.Join(opts.BotName, "config", ".gitkeep"), []byte(""), 0644)
readmeFile := os.WriteFile(filepath.Join(opts.BotName, "README.md"), []byte(templates.Content("bot-readme.md", "resources", "")), 0644)

if dotGitIgnoreFile != nil {
log.Fatal(dotGitIgnoreFile)
}

readmeFile := os.WriteFile(filepath.Join(opts.BotName, "README.md"), []byte(templates.Content("bot-readme.md", "resources", "")), 0644)

if readmeFile != nil {
log.Fatal(readmeFile)
}

if dotGitKeepFile != nil {
log.Fatal(dotGitKeepFile)
}

pwd, _ := os.Getwd()

pwd = filepath.Join(pwd, opts.BotName)
Expand Down
9 changes: 6 additions & 3 deletions internal/render/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/pipes/initx"
"github.com/abdfnx/botwaygo"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
Expand Down Expand Up @@ -80,22 +81,24 @@ func ConnectService() {
serviceSlug := gjson.Get(string(body), "0.service.slug").String()
serviceRepo := gjson.Get(string(body), "0.service.repo").String()

renderPath := "projects." + serviceSlug
renderPath := "projects." + serviceName

service, _ := sjson.Set(string(constants.RenderConfig), renderPath+".id", serviceId)
addRepoToservice, _ := sjson.Set(service, renderPath+".repo", serviceRepo)
addSlug, _ := sjson.Set(service, renderPath+".slug", serviceSlug)
addRepoToService, _ := sjson.Set(addSlug, renderPath+".repo", serviceRepo)

remove := os.Remove(constants.RenderConfigFile)

if remove != nil {
log.Fatal(remove)
}

newBotConfig := os.WriteFile(constants.RenderConfigFile, []byte(addRepoToservice), 0644)
newBotConfig := os.WriteFile(constants.RenderConfigFile, []byte(addRepoToService), 0644)

if newBotConfig != nil {
panic(newBotConfig)
}

UpdateTokens(serviceId)
initx.UpdateConfig()
}

0 comments on commit 5212cfd

Please sign in to comment.