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

Commit

Permalink
update botway init --docker and botway vars set commands api
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Sep 16, 2022
1 parent 52dec61 commit b4e1351
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 127 deletions.
7 changes: 1 addition & 6 deletions cmd/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/abdfnx/botway/internal/options"
"github.com/abdfnx/botway/internal/pipes/initx"
"github.com/abdfnx/botway/tools"
"github.com/abdfnx/botwaygo"
"github.com/spf13/cobra"
)

Expand All @@ -22,11 +21,7 @@ func InitCMD() *cobra.Command {
if opts.Docker {
initx.DockerInit()

if botwaygo.GetBotInfo("bot.host_service") == "render.com" {
tools.SetupTokensInDockerRender()
} else {
tools.SetupTokensInDocker()
}
tools.SetupTokensInDocker()
} else {
initx.Init()

Expand Down
2 changes: 2 additions & 0 deletions cmd/app/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func VarsCMD() *cobra.Command {

variablesGetCmd.Flags().StringP("service", "s", "", desc)
variablesSetCmd.Flags().StringP("service", "s", "", desc)
variablesSetCmd.Flags().BoolP("no-redeploy-hint", "", false, "Don't show re-deploy hints after setting a new variable")
variablesSetCmd.Flags().BoolP("hidden", "", false, "Hide variable value")
variablesRemoveCmd.Flags().StringP("service", "s", "", desc)
} else if botwaygo.GetBotInfo("bot.host_service") == "render.com" {
cmd.Run = func(cmd *cobra.Command, args []string) {
Expand Down
3 changes: 1 addition & 2 deletions dockerfiles/npm.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ RUN botway init --docker

FROM node:alpine

ENV NODE_ENV "production"
ENV PACKAGES "build-dependencies libtool autoconf automake gcc gcc-doc g++ make py3-pip py-pip zlib-dev python3 python3-dev libffi-dev build-base gcc git ffmpeg binutils openssl-dev zlib-dev boost boost-dev"

COPY --from=bw /root/.botway /root/.botway
Expand All @@ -19,6 +18,6 @@ RUN apk update && \
# Add packages you want
# RUN apk add PACKAGE_NAME

RUN npm i --production
RUN npm i

ENTRYPOINT ["npm", "start"]
3 changes: 2 additions & 1 deletion dockerfiles/pnpm.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ RUN botway init --docker

FROM node:alpine

ENV NODE_ENV "production"
ENV PACKAGES "build-dependencies libtool autoconf automake gcc gcc-doc g++ make py3-pip py-pip zlib-dev python3 python3-dev libffi-dev build-base gcc git ffmpeg binutils openssl-dev zlib-dev boost boost-dev"

RUN apk update && \
Expand All @@ -19,6 +18,8 @@ COPY --from=bw /root/.botway /root/.botway

COPY . .

RUN npm i -g pnpm

RUN pnpm fetch --prod
RUN pnpm install

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.18
require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/MakeNowJust/heredoc v1.0.0
github.com/abdfnx/botwaygo v0.0.9
github.com/abdfnx/botwaygo v0.1.0
github.com/abdfnx/looker v0.1.0
github.com/abdfnx/resto v0.1.6
github.com/abdfnx/tran v0.1.43
github.com/botwayorg/gh v0.0.9
github.com/botwayorg/railway-api v0.0.3
github.com/botwayorg/railway-api v0.0.4
github.com/briandowns/spinner v1.19.0
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.22.1
Expand All @@ -26,7 +26,7 @@ require (
github.com/spf13/viper v1.13.0
github.com/tidwall/gjson v1.14.3
github.com/tidwall/sjson v1.2.5
golang.org/x/sys v0.0.0-20220913175220-63ea55921009
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/abdfnx/botwaygo v0.0.9 h1:Y1ZZst9Y97CYUvGfVQy7xOAKVtUYjU9qcmGlmq3UCV8=
github.com/abdfnx/botwaygo v0.0.9/go.mod h1:cSnH5z3N9YTIYlzOXwglI+gChnRFSvtNDkWJ7KMoqso=
github.com/abdfnx/botwaygo v0.1.0 h1:ypoen9U63lTWQjt7KfGSOyIFjs9ROUW7JjDFdYIKI3o=
github.com/abdfnx/botwaygo v0.1.0/go.mod h1:H1WMCWGuQ8Du9PF1/h+MJWI/FwEnmO40GKwjbKJ79iI=
github.com/abdfnx/looker v0.1.0 h1:tMN7E0wKIgbydAPPQ1RkppJ1bGHn+B+y9PZy7mwa+3U=
github.com/abdfnx/looker v0.1.0/go.mod h1:QVfPHnredPBUg4R+MtEkZbMBbqrgtoaj0JHO3KYkvyE=
github.com/abdfnx/resto v0.1.6 h1:yOM9O9bpMP4lb2ox0U7/gcFXO78P5eUZBxWuKrfdrFA=
Expand All @@ -62,8 +62,8 @@ github.com/botwayorg/gh v0.0.9 h1:jHXglsDdAup+wNAhLvqeeXNILTR4DEL12gubyDcxEWs=
github.com/botwayorg/gh v0.0.9/go.mod h1:E3dTBPLt3L9wogY7e4QPHqUYHAbWrlNnumKgu9tcglA=
github.com/botwayorg/git v0.0.0-20220813023031-a7a261511635 h1:AJLUr0Ef6avA5S2SXua7XG4yXI7vAjsZLBrZD5zCOk4=
github.com/botwayorg/git v0.0.0-20220813023031-a7a261511635/go.mod h1:XdqqKSxdEZz88SByHfT4Vwpl/b9gQK9tjZ/FQuwrM0g=
github.com/botwayorg/railway-api v0.0.3 h1:7PIB7gkuswiG/iqLU1VtE8sHHIfiajtDRM6MEKrP82Y=
github.com/botwayorg/railway-api v0.0.3/go.mod h1:pc0Ko0nhKt++RMk0O3+cmMZQtpNOlCqQDIVgjN95nos=
github.com/botwayorg/railway-api v0.0.4 h1:vAvbBan3CZXN82X7zofdt6ix/9VpWboiuq15rSMTGQM=
github.com/botwayorg/railway-api v0.0.4/go.mod h1:oJjlseKXKSc2A7dlPTLtEfm42MIElTivzC8+uWmduAM=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -513,8 +513,8 @@ golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220913175220-63ea55921009 h1:PuvuRMeLWqsf/ZdT1UUZz0syhioyv1mzuFZsXs4fvhw=
golang.org/x/sys v0.0.0-20220913175220-63ea55921009/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc=
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
Expand Down
19 changes: 7 additions & 12 deletions internal/railway/check-buildkit.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
package railway

import (
"bytes"
"log"
"os"
"os/exec"
"runtime"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/config"
"github.com/abdfnx/botway/tools"
"github.com/abdfnx/botwaygo"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)

func CheckBuildKit() {
tools.CheckDir()

viper.SetConfigType("yaml")
checkBuildKit := botwaygo.GetBotInfo("docker.enable_buildkit")

viper.ReadConfig(bytes.NewBuffer(constants.BotConfig))
setVarCmd := "botway vars set --no-redeploy-hint DOCKER_BUILDKIT=0"

checkBuildKit := viper.GetBool("docker.enable_buildkit")

setVarCmd := "botway vars set DOCKER_BUILDKIT=0"

if checkBuildKit {
setVarCmd = "botway vars set DOCKER_BUILDKIT=1"
if checkBuildKit == "true" {
setVarCmd = "botway vars set --no-redeploy-hint DOCKER_BUILDKIT=1"
}

botPath := gjson.Get(string(constants.BotwayConfig), "botway.bots."+viper.GetString("bot.name")+".path").String()
botPath := config.Get("botway.bots." + viper.GetString("bot.name") + ".path")

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

Expand Down
5 changes: 4 additions & 1 deletion internal/railway/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"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"
"github.com/botwayorg/railway-api/ui"
Expand Down Expand Up @@ -49,8 +50,10 @@ func (h *Handler) Delpoy(ctx context.Context, req *entity.CommandRequest) error
src = "."
}

UpdateTokens(src, botwaygo.GetBotInfo("bot.type"))

fmt.Print(constants.INFO_BACKGROUND.Render("INFO"))
fmt.Println(constants.INFO_FOREGROUND.Render("Uploading directory " + constants.BOLD.Render(src)))
fmt.Println(constants.INFO_FOREGROUND.Render(" Uploading directory " + constants.BOLD.Render(src)))

if isVerbose {
fmt.Print(constants.INFO_BACKGROUND.Render("INFO"))
Expand Down
69 changes: 61 additions & 8 deletions internal/railway/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botwaygo"
"github.com/botwayorg/railway-api/entity"
"github.com/botwayorg/railway-api/ui"
"github.com/briandowns/spinner"
Expand All @@ -31,7 +36,7 @@ func (h *Handler) Variables(ctx context.Context, req *entity.CommandRequest) err
}

fmt.Print(constants.HEADING + (fmt.Sprintf("%s Environment Variables", environment.Name)))
fmt.Print(ui.KeyValues(*envs))
fmt.Print(ui.KeyValues(*envs, false))

return nil
}
Expand Down Expand Up @@ -76,6 +81,18 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest)
replace = false
}

noRedeployHint, err := req.Cmd.Flags().GetBool("no-redeploy-hint")

if err != nil {
noRedeployHint = false
}

hidden, err := req.Cmd.Flags().GetBool("hidden")

if err != nil {
hidden = false
}

yes, err := req.Cmd.Flags().GetBool("yes")

if err != nil {
Expand Down Expand Up @@ -131,8 +148,8 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest)
operation = "Replaced existing variables with"
}

fmt.Print(constants.HEADING + (fmt.Sprintf("%s %s for \"%s\"", operation, strings.Join(updatedEnvNames, ", "), environment.Name)))
fmt.Print(ui.KeyValues(*variables))
fmt.Println(constants.HEADING + (fmt.Sprintf("%s %s for \"%s\"", operation, strings.Join(updatedEnvNames, ", "), environment.Name)))
fmt.Print(ui.KeyValues(*variables, hidden))

if !skipRedeploy {
serviceID, err := h.ctrl.GetServiceIdByName(ctx, &serviceName)
Expand All @@ -141,7 +158,7 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest)
return err
}

err = h.redeployAfterVariablesChange(ctx, environment, serviceID)
err = h.redeployAfterVariablesChange(ctx, environment, serviceID, noRedeployHint)

if err != nil {
return err
Expand All @@ -163,6 +180,12 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques
skipRedeploy = false
}

noRedeployHint, err := req.Cmd.Flags().GetBool("no-redeploy-hint")

if err != nil {
noRedeployHint = false
}

err = h.ctrl.DeleteEnvs(ctx, req.Args, &serviceName)
if err != nil {
return err
Expand All @@ -181,7 +204,7 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques
return err
}

err = h.redeployAfterVariablesChange(ctx, environment, serviceID)
err = h.redeployAfterVariablesChange(ctx, environment, serviceID, noRedeployHint)
if err != nil {
return err
}
Expand All @@ -190,7 +213,7 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques
return nil
}

func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment *entity.Environment, serviceID *string) error {
func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment *entity.Environment, serviceID *string, noRedeployHint bool) error {
deployments, err := h.ctrl.GetDeployments(ctx)
if err != nil {
return err
Expand All @@ -204,8 +227,10 @@ func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment
// Don't redeploy if the latest deploy for environment came from up
latestDeploy := deployments[0]
if latestDeploy.Meta == nil || latestDeploy.Meta.Repo == "" {
fmt.Print(constants.INFO_BACKGROUND.Render("INFO"))
fmt.Println(constants.INFO_FOREGROUND.Render(fmt.Sprintf(" Run %s to redeploy your project ", constants.COMMAND_FOREGROUND.Render("botway deploy"))))
if !noRedeployHint {
fmt.Print(constants.INFO_BACKGROUND.Render("INFO"))
fmt.Println(constants.INFO_FOREGROUND.Render(fmt.Sprintf(" Run %s to redeploy your project ", constants.COMMAND_FOREGROUND.Render("botway deploy"))))
}

return nil
}
Expand All @@ -227,3 +252,31 @@ func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment

return nil
}

func UpdateTokens(botPath, botType string) {
setVarCmd := "botway vars set --no-redeploy-hint --hidden "

if botType == "discord" {
setVarCmd += "DISCORD_TOKEN=" + botwaygo.GetToken() + " DISCORD_CLIENT_ID=" + botwaygo.GetAppId()
} else if botType == "slack" {
setVarCmd += "SLACK_TOKEN=" + botwaygo.GetToken() + " SLACK_APP_TOKEN=" + botwaygo.GetAppId() + " SIGNING_SECRET=" + botwaygo.GetSigningSecret()
} else if botType == "telegram" {
setVarCmd += "TELEGRAM_TOKEN=" + botwaygo.GetToken()
}

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

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

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

if err != nil {
log.Printf("error: %v\n", err)
}
}
2 changes: 1 addition & 1 deletion packages/botwaygo
Loading

0 comments on commit b4e1351

Please sign in to comment.