From 9a843027a8465297fc8d09c37d7e196c91a24895 Mon Sep 17 00:00:00 2001 From: bambangDeny <35832972+bambangDeny@users.noreply.github.com> Date: Tue, 8 Oct 2019 15:01:55 +0700 Subject: [PATCH] feature: created cli command to install required backend tools (#2) * add lint and fix code * feature: created cli command to install change-log * feature: created cli command to install change-log * change code structure in generator, add comment * fix some comment * renaming variable and fix code * feature: add all need Contributing to Backend Projects * add error handler and remove unused code * add err handler in command flag, * fix err handler lint * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: db migrationfile generator (#3) * create db migrationfile generator * change 'migrate' to 'migration' * repair code with lint * fix error message * bump readme * add Test in migration generator, change project name into args * feature: add all need Contributing to Backend Projects * feature: created cli command to install change-log * feature: created cli command to install change-log * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: repository and model generator (#4) * create generator for repository and model * add generate command, refactor naming, fix makefile * move generate command to generate.go * renaming cacher and fix readme * feature: created cli command to install change-log * feature: created cli command to install change-log * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: created cli command to install change-log * feature: created cli command to install change-log * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects * feature: add all need Contributing to Backend Projects --- config/default.go | 22 +++++ console/install.go | 128 ++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 + installer/goutils_installer.go | 19 +++++ installer/helper.go | 139 +++++++++++++++++++++++++++++++ installer/protobuf_installer.go | 69 +++++++++++++++ installer/watchmedo_installer.go | 39 +++++++++ 8 files changed, 419 insertions(+) create mode 100644 config/default.go create mode 100644 console/install.go create mode 100644 installer/goutils_installer.go create mode 100644 installer/helper.go create mode 100644 installer/protobuf_installer.go create mode 100644 installer/watchmedo_installer.go diff --git a/config/default.go b/config/default.go new file mode 100644 index 0000000..1fc47bd --- /dev/null +++ b/config/default.go @@ -0,0 +1,22 @@ +package config + +const ( + // GoVersion :nodoc: + GoVersion = "1.12.7" + // ChangeLogInstallerURL :nodoc: + ChangeLogInstallerURL = "github.com/git-chglog/git-chglog/cmd/git-chglog" + // ProtobufInstallerURL :nodoc: + ProtobufInstallerURL = "github.com/golang/protobuf/protoc-gen-go" + // MockgenInstallerURL :nodoc: + MockgenInstallerURL = "github.com/golang/mock/mockgen" + // RichgoInstallerURL :nodoc: + RichgoInstallerURL = "github.com/kyoh86/richgo" + // GolintInstallerURL :nodoc: + GolintInstallerURL = "github.com/golangci/golangci-lint/cmd/golangci-lint" + // ProtobufOSXInstallerURL :nodoc: + ProtobufOSXInstallerURL = "https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protoc-3.7.1-osx-x86_64.zip" + // ProtobufLinuxInstallerURL :nodoc: + ProtobufLinuxInstallerURL = "https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protoc-3.7.1-linux-x86_64.zip" + // ProtocZipFileName :nodoc: + ProtocZipFileName = "protobuf.zip" +) diff --git a/console/install.go b/console/install.go new file mode 100644 index 0000000..9fe52a4 --- /dev/null +++ b/console/install.go @@ -0,0 +1,128 @@ +package console + +import ( + "fmt" + "strings" + + "github.com/kumparan/fer/config" + "github.com/kumparan/fer/installer" + "github.com/spf13/cobra" +) + +var installCmd = &cobra.Command{ + Use: "install", + Short: "install all dependencies", + Long: "install all dependencies for contributing to backend projects", + Run: installAllCmd, +} + +func installAllCmd(_ *cobra.Command, _ []string) { + installAll() +} + +func installAll() { + installer.CheckExistenceOfGolang() + installer.CheckGolangVersion() + var messages = []string{} + + message := installer.InstallGoUtils("protoc-gen-go", config.ProtobufInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("mockgen", config.MockgenInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("richgo", config.RichgoInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("golint", config.GolintInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("git-chglog", config.ChangeLogInstallerURL) + messages = append(messages, message) + + message = installer.CheckedInstallerPath("make") + messages = append(messages, message) + + message = installer.InstallWatchmedo() + messages = append(messages, message) + + message = installer.ProtobufInstaller() + messages = append(messages, message) + fmt.Printf("%s", strings.Join(messages, "/n")) + +} + +func init() { + installCmd.AddCommand(goUtilsCmd) + installCmd.AddCommand(watchmedoCmd) + installCmd.AddCommand(protobufCmd) + rootCmd.AddCommand(installCmd) +} + +var goUtilsCmd = &cobra.Command{ + Use: "goutils", + Short: "fer install goutils", + Long: "This subcommand to install git go utils ", + Run: installGoUtilsCmd, +} + +func installGoUtilsCmd(_ *cobra.Command, _ []string) { + installGoUtils() +} + +func installGoUtils() { + installer.CheckExistenceOfGolang() + installer.CheckGolangVersion() + var messages = []string{} + message := installer.InstallGoUtils("protoc-gen-go", config.ProtobufInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("mockgen", config.MockgenInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("richgo", config.RichgoInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("golint", config.GolintInstallerURL) + messages = append(messages, message) + + message = installer.InstallGoUtils("git-chglog", config.ChangeLogInstallerURL) + messages = append(messages, message) + + message = installer.CheckedInstallerPath("make") + messages = append(messages, message) + + fmt.Printf("%s", strings.Join(messages, "/n")) +} + +var watchmedoCmd = &cobra.Command{ + Use: "watchmedo", + Short: "fer install watchmedo", + Long: "This subcommand to install watchmedo", + Run: installWatchmedoCmd, +} + +func installWatchmedoCmd(cmd *cobra.Command, args []string) { + installWatchmedo() +} + +func installWatchmedo() { + message := installer.InstallWatchmedo() + fmt.Println(message) +} + +var protobufCmd = &cobra.Command{ + Use: "protobuf", + Short: "fer install protobuf", + Long: "This subcommand to install protobuf", + Run: installProtobufCmd, +} + +func installProtobufCmd(cmd *cobra.Command, args []string) { + installProtobuf() +} + +func installProtobuf() { + message := installer.ProtobufInstaller() + fmt.Println(message) +} diff --git a/go.mod b/go.mod index f7dd5b7..29f709a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/dave/jennifer v1.3.0 + github.com/hashicorp/go-version v1.2.0 github.com/spf13/cobra v0.0.5 github.com/stretchr/testify v1.3.0 ) diff --git a/go.sum b/go.sum index eda44fe..5e2a92f 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,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/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= +github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= diff --git a/installer/goutils_installer.go b/installer/goutils_installer.go new file mode 100644 index 0000000..11087eb --- /dev/null +++ b/installer/goutils_installer.go @@ -0,0 +1,19 @@ +package installer + +import ( + "fmt" + "os/exec" +) + +// InstallGoUtils :nodoc: +func InstallGoUtils(installername, URL string) string { + cmd := exec.Command("go", "get", "-u", URL) + err := cmd.Run() + if err != nil { + fmt.Println(err) + return "fail installed " + installername + } + message := CheckedInstallerPath(installername) + return message + +} diff --git a/installer/helper.go b/installer/helper.go new file mode 100644 index 0000000..a2141ad --- /dev/null +++ b/installer/helper.go @@ -0,0 +1,139 @@ +package installer + +import ( + "archive/zip" + "fmt" + "io" + "log" + "net/http" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + + "github.com/kumparan/fer/config" + + version "github.com/hashicorp/go-version" +) + +// CheckExistenceOfGolang :nodoc: +func CheckExistenceOfGolang() { + cmdGetGolangLocation := exec.Command("which", "go") + err := cmdGetGolangLocation.Run() + if err != nil { + fmt.Println("You should install golang first") + os.Exit(1) + } +} + +// CheckGolangVersion :nodoc: +func CheckGolangVersion() { + cmdGetGolangVersion, err := exec.Command("go", "version").Output() + if err != nil { + log.Fatal(err) + } + var goLocalversion = string(cmdGetGolangVersion) + var regexVersion, _ = regexp.Compile(`(\d+\.\d+\.\d+)`) + v1, err := version.NewVersion(config.GoVersion) + if err != nil { + log.Fatal(err) + } + v2, err := version.NewVersion(regexVersion.FindString(goLocalversion)) + if err != nil { + log.Fatal(err) + } + if v2.LessThan(v1) { + fmt.Printf("Go version must be %s or latest\n", config.GoVersion) + os.Exit(1) + } +} + +// CheckedInstallerPath :nodoc: +func CheckedInstallerPath(installer string) string { + cmdGetInstallerPath := exec.Command("which", installer) + err := cmdGetInstallerPath.Run() + if err != nil { + return "fail installed " + installer + } + return "Success installed " + installer +} + +// DownloadFile :nodoc: +func DownloadFile(filepath string, url string) error { + + // Get the data + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + // Create the file + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + // Write the body to file + _, err = io.Copy(out, resp.Body) + return err +} + +// Unzip :nodoc: +func Unzip(src string, dest string) ([]string, error) { + + var filenames []string + + r, err := zip.OpenReader(src) + if err != nil { + return filenames, err + } + defer r.Close() + + for _, f := range r.File { + + // Store filename/path for returning and using later on + fpath := filepath.Join(dest, f.Name) + + // Check for ZipSlip. + if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) { + return filenames, fmt.Errorf("%s: illegal file path", fpath) + } + + filenames = append(filenames, fpath) + + if f.FileInfo().IsDir() { + // Make Folder + os.MkdirAll(fpath, os.ModePerm) + continue + } + + // Make File + if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil { + return filenames, err + } + + outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) + if err != nil { + return filenames, err + } + + rc, err := f.Open() + if err != nil { + return filenames, err + } + + _, err = io.Copy(outFile, rc) + + // Close the file without defer to close before next iteration of loop + outFile.Close() + rc.Close() + + if err != nil { + return filenames, err + } + } + return filenames, nil +} diff --git a/installer/protobuf_installer.go b/installer/protobuf_installer.go new file mode 100644 index 0000000..4384ac0 --- /dev/null +++ b/installer/protobuf_installer.go @@ -0,0 +1,69 @@ +package installer + +import ( + "fmt" + "io/ioutil" + "log" + "os/exec" + "runtime" + + "github.com/kumparan/fer/config" +) + +var ( + errorDownload = "fail downloaded protobuf" + errorUnzip = "fail unzip protobuf" + errorInstall = "fail installeded protobuf" + successDownload = "success downloaded protobuf" +) + +func protobufDownloadInstaller() (filePath, message string) { + downloadURL := config.ProtobufLinuxInstallerURL + tmp, err := ioutil.TempDir("", "") + if err != nil { + log.Fatal(err) + } + + if runtime.GOOS == "darwin" { + downloadURL = config.ProtobufOSXInstallerURL + } + err = DownloadFile(tmp+"/"+config.ProtocZipFileName, downloadURL) + if err != nil { + fmt.Println(err) + return "", errorDownload + } + return tmp, successDownload +} + +// ProtobufInstaller :nodoc: +func ProtobufInstaller() string { + tmp, message := protobufDownloadInstaller() + protobufZipFile := config.ProtocZipFileName + filePath := tmp + "/" + protobufZipFile + if runtime.GOOS == "darwin" { + protobufZipFile = config.ProtocZipFileName + } + if message == successDownload { + _, err := Unzip(filePath, "/usr/local/bin/") + if err != nil { + log.Fatal(err) + return errorUnzip + } + + _, err = Unzip(filePath, "include/*") + if err != nil { + log.Fatal(err) + return errorUnzip + } + + cmdRemoveProtocZip := exec.Command("rm", "-f", filePath) + err = cmdRemoveProtocZip.Run() + if err != nil { + fmt.Println(err) + return errorUnzip + } + checkedMessage := CheckedInstallerPath("protoc") + return checkedMessage + } + return errorInstall +} diff --git a/installer/watchmedo_installer.go b/installer/watchmedo_installer.go new file mode 100644 index 0000000..689a078 --- /dev/null +++ b/installer/watchmedo_installer.go @@ -0,0 +1,39 @@ +package installer + +import ( + "fmt" + "os" + "os/exec" +) + +type ( + // WatchmedoInstaller :nodoc: + WatchmedoInstaller interface { + InstallWatchmedo() + } +) + +// InstallWatchmedo :nodoc: +func InstallWatchmedo() string { + pipCmd := "pip3" + cmdGetPip3Location := exec.Command("which", pipCmd) + err := cmdGetPip3Location.Run() + if err != nil { + pipCmd = "pip" + cmdGetPipLocation := exec.Command("which", pipCmd) + err = cmdGetPipLocation.Run() + if err != nil { + fmt.Println("you must install python-pip first") + os.Exit(1) + } + + } + cmdInstallWachmedoByPip := exec.Command(pipCmd, "install", "watchdog") + err = cmdInstallWachmedoByPip.Run() + if err != nil { + fmt.Println(err) + return "fail installed watchmedo" + } + message := CheckedInstallerPath("watchmedo") + return message +}