style: code organization

This commit is contained in:
Allyedge
2022-05-31 21:24:41 +02:00
parent 34c27bd910
commit f5a4771c37
6 changed files with 92 additions and 58 deletions

2
go.mod
View File

@@ -13,6 +13,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/gookit/color v1.5.0 // indirect
@@ -25,6 +26,7 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect

3
go.sum
View File

@@ -22,6 +22,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
@@ -46,6 +48,7 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=

View File

@@ -4,69 +4,11 @@ import (
"errors"
"os"
"os/exec"
"strings"
"github.com/go-git/go-git/v5"
"github.com/gookit/color"
"github.com/sern-handler/cli/pkg/util"
)
func cloneRepository(name string, language string) error {
_, err := git.PlainClone("templates", false, &git.CloneOptions{
URL: "https://github.com/sern-handler/templates",
Progress: os.Stdout,
})
if err != nil {
color.Error.Prompt("Couldn't install the template.")
return err
}
err = os.Rename("templates/templates/"+strings.ToLower(language), name)
if err != nil {
color.Error.Prompt("Couldn't rename the template to the project's name.")
color.Warn.Prompt("The project was generated, but it wasn't renamed.\n\nYou can still use the project, but you will have to rename it manually.")
return err
}
err = os.RemoveAll("templates")
if err != nil {
color.Error.Prompt("Couldn't remove the templates folder.")
return err
}
return nil
}
func renameFolders(name string, main string, commands string) error {
if main != "src" {
err := os.Rename(name+"/src", name+"/"+main)
if err != nil {
color.Warn.Prompt("Couldn't rename the main folder.")
return err
}
}
if commands != "commands" {
err := os.Rename(name+"/"+main+"/commands", name+"/"+main+"/"+commands)
if err != nil {
color.Warn.Prompt("Couldn't rename the commands folder.")
return err
}
}
return nil
}
func installDependencies(name string, packageManager string) error {
err := os.Chdir(name)

View File

@@ -74,5 +74,14 @@ func Initialize() {
os.Exit(1)
}
err = renamePackageJson(answers.Name)
if err != nil {
color.Error.Prompt("Couldn't rename the package.json file, exiting.")
color.Warn.Prompt("The project was generated, but the package.json file wasn't updated.\n\nYou can still use the project, but you will have to update the package.json file manually.")
os.Exit(1)
}
color.Success.Prompt("Project successfully initialized.")
}

37
pkg/initialize/rename.go Normal file
View File

@@ -0,0 +1,37 @@
package initialize
import (
"os"
"github.com/gookit/color"
)
func renameFolders(name string, main string, commands string) error {
if main != "src" {
err := os.Rename(name+"/src", name+"/"+main)
if err != nil {
color.Warn.Prompt("Couldn't rename the main folder.")
return err
}
}
if commands != "commands" {
err := os.Rename(name+"/"+main+"/commands", name+"/"+main+"/"+commands)
if err != nil {
color.Warn.Prompt("Couldn't rename the commands folder.")
return err
}
}
return nil
}
func renamePackageJson(name string) error {
color.Warn.Prompt("Work in progress...")
return nil
}

View File

@@ -0,0 +1,41 @@
package initialize
import (
"os"
"strings"
"github.com/go-git/go-git/v5"
"github.com/gookit/color"
)
func cloneRepository(name string, language string) error {
_, err := git.PlainClone("templates", false, &git.CloneOptions{
URL: "https://github.com/sern-handler/templates",
Progress: os.Stdout,
})
if err != nil {
color.Error.Prompt("Couldn't install the template.")
return err
}
err = os.Rename("templates/templates/"+strings.ToLower(language), name)
if err != nil {
color.Error.Prompt("Couldn't rename the template to the project's name.")
color.Warn.Prompt("The project was generated, but it wasn't renamed.\n\nYou can still use the project, but you will have to rename it manually.")
return err
}
err = os.RemoveAll("templates")
if err != nil {
color.Error.Prompt("Couldn't remove the templates folder.")
return err
}
return nil
}