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

Commit

Permalink
build botway telegram python poetry handler to botway CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Jul 2, 2022
1 parent a8643d2 commit c3ddc56
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/pipes/new/telegram_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/abdfnx/botway/templates/telegram/nodejs"
"github.com/abdfnx/botway/templates/telegram/python/pip"
"github.com/abdfnx/botway/templates/telegram/python/pipenv"
"github.com/abdfnx/botway/templates/telegram/python/poetry"
"github.com/abdfnx/botway/templates/telegram/ruby"
"github.com/abdfnx/botway/templates/telegram/rust"
)
Expand All @@ -15,6 +16,8 @@ func TelegramHandler(m model) {
pip.TelegramPythonPip(opts.BotName)
} else if m.PlatformChoice == 1 && m.LangChoice == 0 && m.PMCoice == 1 {
pipenv.TelegramPythonPipenv(opts.BotName)
} else if m.PlatformChoice == 1 && m.LangChoice == 0 && m.PMCoice == 2 {
poetry.TelegramPythonPoetry(opts.BotName)
} else if m.PlatformChoice == 1 && m.LangChoice == 1 {
tgo.TelegramGo(opts.BotName)
} else if m.PlatformChoice == 1 && m.LangChoice == 2 && m.PMCoice == 0 {
Expand Down
81 changes: 81 additions & 0 deletions templates/telegram/python/poetry/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package poetry

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/templates"
"github.com/abdfnx/botway/templates/telegram/python"
"github.com/abdfnx/looker"
)

func PyProjectContent(botName string) string {
return templates.Content("pyproject.toml", "discord-python", botName)
}

func TelegramPythonPoetry(botName string) {
pythonPath := "python3"

if runtime.GOOS == "windows" {
pythonPath = "python"
}

_, err := looker.LookPath(pythonPath)
poetry, perr := looker.LookPath("poetry")

if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" python is not installed"))
} else if perr != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" poetry is not installed"))
} else {
dockerFileContent := templates.Content("poetry.dockerfile", "dockerfiles", botName)

mainFile := os.WriteFile(filepath.Join(botName, "src", "main.py"), []byte(python.MainPyContent()), 0644)
pyprojectFile := os.WriteFile(filepath.Join(botName, "pyproject.toml"), []byte(PyProjectContent(botName)), 0644)
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(dockerFileContent), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(python.Resources()), 0644)

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

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

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

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

poetryAdd := poetry + " add python-telegram-bot botway.py pyyaml cryptography PySocks ujson"

cmd := exec.Command("bash", "-c", poetryAdd)

if runtime.GOOS == "windows" {
cmd = exec.Command("powershell.exe", poetryAdd)
}

cmd.Dir = botName
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()

if err != nil {
log.Printf("error: %v\n", err)
}

templates.CheckProject(botName, "discord")
}
}

0 comments on commit c3ddc56

Please sign in to comment.