-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
97dee66
commit 9a84302
Showing
8 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.