Skip to content

Commit

Permalink
feature: created cli command to install required backend tools (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bambangDeny authored and zhenqianz committed Oct 8, 2019
1 parent 97dee66 commit 9a84302
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
@@ -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"
)
128 changes: 128 additions & 0 deletions console/install.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 19 additions & 0 deletions installer/goutils_installer.go
Original file line number Diff line number Diff line change
@@ -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

}
139 changes: 139 additions & 0 deletions installer/helper.go
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 69 additions & 0 deletions installer/protobuf_installer.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 9a84302

Please sign in to comment.