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

Commit

Permalink
add botway config command, add auto-sync feature, update pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Sep 20, 2022
1 parent df3ec2b commit f20b1fa
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 12 deletions.
107 changes: 107 additions & 0 deletions cmd/app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package app

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

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/pipes/initx"
"github.com/botwayorg/git"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ConfigCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Update, Sync, Push changes, or Clone your botway configuration settings",
}

cmd.AddCommand(ConfigUpdateCMD())
cmd.AddCommand(ConfigSyncCMD())
cmd.AddCommand(ConfigCloneCMD())

return cmd
}

func ConfigUpdateCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Aliases: []string{"set"},
Short: "Update botway configuration",
RunE: func(cmd *cobra.Command, args []string) error {
viper.SetConfigType("json")

viper.ReadConfig(bytes.NewBuffer(constants.BotwayConfig))

var newValue interface{} = args[1]

if args[1] == "true" {
newValue = true
} else if args[1] == "false" {
newValue = false
}

// set new key value but keep existing values
viper.Set("botway.settings."+args[0], newValue)

// write config to file
err := viper.WriteConfigAs(constants.BotwayConfigFile)

if err != nil {
return err
}

fmt.Print(constants.SUCCESS_BACKGROUND.Render("SUCCESS"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" Updated botway configuration"))

return nil
},
}

return cmd
}

func ConfigSyncCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "sync",
Short: "Sync your config file at your private repo (" + git.GitConfig() + "/.botway)",
Run: func(cmd *cobra.Command, args []string) {
initx.UpdateConfig()
},
}

return cmd
}

func ConfigCloneCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "clone",
Short: "Clone your .botway repo",
Run: func(cmd *cobra.Command, args []string) {
clone := `botway gh-repo clone .botway ` + constants.HomeDir

cloneCommand := exec.Command("bash", "-c", clone)

if runtime.GOOS == "windows" {
cloneCommand = exec.Command("powershell.exe", "-Command", clone)
}

cloneCommand.Dir = constants.BotwayDirPath
cloneCommand.Stdin = os.Stdin
cloneCommand.Stdout = os.Stdout
cloneCommand.Stderr = os.Stderr
err := cloneCommand.Run()

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

return cmd
}
2 changes: 1 addition & 1 deletion cmd/app/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func ExecCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "exec",
Short: "Execute a local command using variables from the active environment",
Short: "Execute a local command using variables from the active environment (Only for bots that hosted on Railway)",
PreRun: func(cmd *cobra.Command, args []string) { tools.CheckDir() },
RunE: Contextualize(handler.Exec, handler.Panic),
DisableFlagParsing: true,
Expand Down
1 change: 1 addition & 0 deletions cmd/botway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Execute(f *factory.Factory, version string, buildDate string) *cobra.Comman
app.DBCMD(),
app.DockerCMD(),
app.DockerInitCMD(),
app.ConfigCMD(),
app.ComposeCMD(),
app.NewCMD(),
app.TokenCMD(),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/abdfnx/resto v0.1.6
github.com/abdfnx/tran v0.1.43
github.com/botwayorg/gh v0.0.9
github.com/botwayorg/git v0.0.0-20220813023031-a7a261511635
github.com/botwayorg/railway-api v0.0.4
github.com/briandowns/spinner v1.19.0
github.com/charmbracelet/bubbles v0.14.0
Expand All @@ -34,7 +35,6 @@ require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/botwayorg/git v0.0.0-20220813023031-a7a261511635 // indirect
github.com/charmbracelet/glamour v0.5.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cli/browser v1.1.0 // indirect
Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+Ei
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -356,8 +355,6 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
github.com/yuin/goldmark v1.4.14 h1:jwww1XQfhJN7Zm+/a1ZA/3WUiEBEroYFNTiV3dKwM8U=
github.com/yuin/goldmark v1.4.14/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.4.15 h1:CFa84T0goNn/UIXYS+dmjjVxMyTAvpOmzld40N/nfK0=
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark-emoji v1.0.1 h1:ctuWEyzGBwiucEqxzwe0SOYDXPAucOrE9NQC18Wa1os=
Expand Down Expand Up @@ -444,8 +441,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI=
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20220920152717-4a395b0a80a1 h1:KPlMURVqlGj7IS5s1RR3RyiiiKAgGMrh3O4A0tpOQOg=
golang.org/x/net v0.0.0-20220920152717-4a395b0a80a1/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -519,16 +514,12 @@ 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-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc=
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/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=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220919170432-7a66f970e087 h1:tPwmk4vmvVCMdr98VgL4JH+qZxPL8fqlUOHnyOM8N3w=
golang.org/x/term v0.0.0-20220919170432-7a66f970e087/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
30 changes: 30 additions & 0 deletions internal/pipes/initx/setup-git.go → internal/pipes/initx/git.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package initx

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

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/config"
)

func windowsCommands() string {
Expand Down Expand Up @@ -50,3 +52,31 @@ func SetupGitRepo() {
log.Printf("error: %v\n", err)
}
}

func UpdateConfig() {
if config.Get("botway.settings.auto_sync") == "true" {
cmd := `git add .
git commit -m "New changes"
git push`

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

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

updateCommand.Dir = constants.BotwayDirPath
updateCommand.Stdin = os.Stdin
updateCommand.Stdout = os.Stdout
updateCommand.Stderr = os.Stderr
err := updateCommand.Run()

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

fmt.Print(constants.SUCCESS_BACKGROUND.Render("SUCCESS"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" Configuration synced"))

}
}
3 changes: 3 additions & 0 deletions internal/pipes/new/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/abdfnx/botway/internal/config"
"github.com/abdfnx/botway/internal/pipes/initx"
"github.com/abdfnx/botway/templates"
"github.com/abdfnx/resto/core/api"
"github.com/spf13/viper"
Expand Down Expand Up @@ -164,4 +165,6 @@ func NewBot(m model, l string, platform, lang int) {
pwd = filepath.Join(pwd, opts.BotName)

AddBotToConfig(opts.BotName, BotType(m), pwd, BotLang(m), HostService(m))

initx.UpdateConfig()
}
3 changes: 3 additions & 0 deletions internal/pipes/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/options"
"github.com/abdfnx/botway/internal/pipes/initx"
token_shared "github.com/abdfnx/botway/internal/pipes/token"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -96,6 +97,8 @@ func (m model) RemoveCmd() {
panic(newBotConfig)
}

initx.UpdateConfig()

if _, err := os.Stat(constants.BotwayConfigFile); err == nil {
fmt.Print(constants.SUCCESS_BACKGROUND.Render("SUCCESS"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" " + m.botName + " Removed Successfully"))
Expand Down
3 changes: 3 additions & 0 deletions internal/pipes/token/discord/guilds/ids.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"
token_shared "github.com/abdfnx/botway/internal/pipes/token"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -70,6 +71,8 @@ func (m model) AddGuildId() {
panic(newGuilds)
}

initx.UpdateConfig()

fmt.Print(constants.SUCCESS_BACKGROUND.Render("SUCCESS"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" " + m.inputs[1].Value() + " server id is added successfully"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render("You can add more server ids by running the same command again"))
Expand Down
3 changes: 3 additions & 0 deletions internal/pipes/token/discord/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/pipes/initx"
token_shared "github.com/abdfnx/botway/internal/pipes/token"
"github.com/abdfnx/botway/tools"
"github.com/charmbracelet/bubbles/textinput"
Expand Down Expand Up @@ -54,6 +55,8 @@ func (m model) AddToken() {
tools.GenerateCConfig(m.inputs[0].Value())
}

initx.UpdateConfig()

fmt.Println(constants.SUCCESS_FOREGROUND.Render("You can add the guild ids of your discord server via the command") + token_shared.BoldStyle.Render(" "+"botway tokens add-guilds") + " 📁")
}

Expand Down
3 changes: 3 additions & 0 deletions internal/pipes/token/slack/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/pipes/initx"
token_shared "github.com/abdfnx/botway/internal/pipes/token"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -44,6 +45,8 @@ func (m model) AddToken() {
panic(newBotConfig)
}

initx.UpdateConfig()

fmt.Print(constants.SUCCESS_BACKGROUND.Render("SUCCESS"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" " + m.botName + " Slack tokens're added successfully"))
// fmt.Println("Your Secret key -> " + token_shared.BoldStyle.Render(token_shared.UserSecret) + " Keep it in a safe place")
Expand Down
3 changes: 3 additions & 0 deletions internal/pipes/token/telegram/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/pipes/initx"
token_shared "github.com/abdfnx/botway/internal/pipes/token"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -42,6 +43,8 @@ func (m model) AddToken() {
panic(newBotConfig)
}

initx.UpdateConfig()

fmt.Print(constants.SUCCESS_BACKGROUND.Render("SUCCESS"))
fmt.Println(constants.SUCCESS_FOREGROUND.Render(" " + m.botName + " Telegram token is added successfully"))
// fmt.Println("Your Secret key -> " + token_shared.BoldStyle.Render(token_shared.UserSecret) + " Keep it in a safe place")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func mainRun() exitCode {
return exitError
}

if config.Get("settings.check_updates") == "true" {
if config.Get("botway.settings.check_updates") == "true" {
checker.Check(version)
}

Expand Down

0 comments on commit f20b1fa

Please sign in to comment.