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

Commit

Permalink
add some new ideas to fix botway config file issue on railway
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Sep 19, 2022
1 parent bbf041d commit cddc7ad
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
26 changes: 26 additions & 0 deletions cmd/app/docker-init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app

import (
"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/pipes/initx"
"github.com/abdfnx/botway/tools"
"github.com/spf13/cobra"
)

func DockerInitCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "docker-init",
Short: "Initialize ~/.botway for docker containers",
Run: func(cmd *cobra.Command, args []string) {
if opts.CopyFile {
tools.Copy("botway.json", constants.BotwayDirPath)
} else {
initx.DockerInit()
}
},
}

cmd.Flags().BoolVarP(&opts.CopyFile, "copy-file", "", false, "Copy config file")

return cmd
}
21 changes: 4 additions & 17 deletions cmd/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,28 @@ package app
import (
"github.com/abdfnx/botway/internal/options"
"github.com/abdfnx/botway/internal/pipes/initx"
"github.com/abdfnx/botwaygo"
"github.com/spf13/cobra"
)

var opts = options.InitOptions{
Docker: false,
NoRepo: false,
CopyFile: false,
NoRepo: false,
}

func InitCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize ~/.botway",
Aliases: []string{"."},
}

if opts.Docker {
if botwaygo.GetBotInfo("bot.host") == "railway.app" {
cmd.RunE = Contextualize(handler.DockerInit, handler.Panic)
} else if botwaygo.GetBotInfo("bot.host") == "render.com" {
cmd.Run = func(cmd *cobra.Command, args []string) {
initx.DockerInit()
}
}
} else {
cmd.Run = func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
initx.Init()

if !opts.NoRepo {
initx.SetupGitRepo()
}
}
},
}

cmd.Flags().BoolVarP(&opts.Docker, "docker", "", false, "Initialize botway config in docker")
cmd.Flags().BoolVarP(&opts.NoRepo, "no-repo", "", false, "Don't create a private git repo under my account")

return cmd
Expand Down
1 change: 1 addition & 0 deletions cmd/botway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func Execute(f *factory.Factory, version string, buildDate string) *cobra.Comman
app.InitCMD(),
app.DBCMD(),
app.DockerCMD(),
app.DockerInitCMD(),
app.ComposeCMD(),
app.NewCMD(),
app.TokenCMD(),
Expand Down
4 changes: 2 additions & 2 deletions internal/options/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ type RootOptions struct {
}

type InitOptions struct {
Docker bool
NoRepo bool
CopyFile bool
NoRepo bool
}

type CommonOptions struct {
Expand Down
14 changes: 3 additions & 11 deletions internal/railway/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botwaygo"
"github.com/abdfnx/tran/dfs"
"github.com/botwayorg/railway-api/entity"
CLIErrors "github.com/botwayorg/railway-api/errors"
"github.com/botwayorg/railway-api/ui"
Expand All @@ -24,12 +22,6 @@ import (
)

func (h *Handler) DockerInit(ctx context.Context, req *entity.CommandRequest) error {
err := dfs.CreateDirectory(filepath.Join(constants.HomeDir, ".botway"))

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

envs, err := h.ctrl.GetEnvsForCurrentEnvironment(ctx, nil)
if err != nil {
return err
Expand All @@ -45,7 +37,7 @@ func (h *Handler) DockerInit(ctx context.Context, req *entity.CommandRequest) er
botEnv.SetConfigType("json")
botEnv.ReadConfig(bytes.NewBuffer(encoded))

viper.AddConfigPath(constants.BotwayDirPath)
viper.AddConfigPath(".")
viper.SetConfigName("botway")
viper.SetConfigType("json")

Expand Down Expand Up @@ -112,8 +104,6 @@ func (h *Handler) DockerInit(ctx context.Context, req *entity.CommandRequest) er

fmt.Println(constants.HEADING + constants.BOLD.Render("Done 🐋️"))

os.RemoveAll("botway.json")

return nil
}

Expand Down Expand Up @@ -288,5 +278,7 @@ func (h *Handler) Delpoy(ctx context.Context, req *entity.CommandRequest) error
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" ☁️ Deployment is live"))
}

os.RemoveAll("botway.json")

return nil
}
39 changes: 39 additions & 0 deletions tools/copy-file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tools

import (
"fmt"
"io"
"os"
)

func Copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)

if err != nil {
return 0, err
}

if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}

source, err := os.Open(src)

if err != nil {
return 0, err
}

defer source.Close()

destination, err := os.Create(dst)

if err != nil {
return 0, err
}

defer destination.Close()

nBytes, err := io.Copy(destination, source)

return nBytes, err
}

0 comments on commit cddc7ad

Please sign in to comment.