From cddc7adbf47c627e998f69356bba7c72ed4bb226 Mon Sep 17 00:00:00 2001 From: abdfnx Date: Mon, 19 Sep 2022 16:11:43 +0300 Subject: [PATCH] add some new ideas to fix botway config file issue on railway --- cmd/app/docker-init.go | 26 +++++++++++++++++++++++++ cmd/app/init.go | 21 ++++---------------- cmd/botway/root.go | 1 + internal/options/opts.go | 4 ++-- internal/railway/deploy.go | 14 +++----------- tools/copy-file.go | 39 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 cmd/app/docker-init.go create mode 100644 tools/copy-file.go diff --git a/cmd/app/docker-init.go b/cmd/app/docker-init.go new file mode 100644 index 00000000..f56cb919 --- /dev/null +++ b/cmd/app/docker-init.go @@ -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 +} diff --git a/cmd/app/init.go b/cmd/app/init.go index 0a4ef0ce..683b9da4 100755 --- a/cmd/app/init.go +++ b/cmd/app/init.go @@ -3,13 +3,12 @@ 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 { @@ -17,27 +16,15 @@ func InitCMD() *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 diff --git a/cmd/botway/root.go b/cmd/botway/root.go index 93bc5109..ff8a2acc 100644 --- a/cmd/botway/root.go +++ b/cmd/botway/root.go @@ -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(), diff --git a/internal/options/opts.go b/internal/options/opts.go index d67463d3..00785a30 100755 --- a/internal/options/opts.go +++ b/internal/options/opts.go @@ -5,8 +5,8 @@ type RootOptions struct { } type InitOptions struct { - Docker bool - NoRepo bool + CopyFile bool + NoRepo bool } type CommonOptions struct { diff --git a/internal/railway/deploy.go b/internal/railway/deploy.go index 3df553d0..c56f3ad1 100644 --- a/internal/railway/deploy.go +++ b/internal/railway/deploy.go @@ -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" @@ -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 @@ -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") @@ -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 } @@ -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 } diff --git a/tools/copy-file.go b/tools/copy-file.go new file mode 100644 index 00000000..9dfb6cfa --- /dev/null +++ b/tools/copy-file.go @@ -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 +}