Skip to content

Commit

Permalink
Merge pull request #89 from srijanone/Brownfield-Add-hooks-cmd
Browse files Browse the repository at this point in the history
Added vega hooks install command for existing projects
  • Loading branch information
vs4vijay committed Jul 6, 2020
2 parents 236a8f4 + a6f2a08 commit 15c55d4
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 69 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Several options to install:
| `vega create [path] --starterkit <name>` | Creates the starter kit at provided directory | \--starterkit <name><br>\--repo <repo> | |
| `vega repo add <repo-name> <url>` | Add another starterkit repo, Can choose local folder as well | | |
| `vega repo list` | Lists all the repo available | \--repo <repo> | |
| `vega hooks install [path]` | Installs git hooks to specified path | | |
| `vega up` | Runs the application | \--port <log-port><br>\--watch<br>\--no-browser | |
| `vega down` | Stops the application and deletes the resources | | |

Expand Down
6 changes: 3 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cCmd *createCmd) execute() error {
// TODO: Check if starterkits files are already there or not properly
dockerfileExists, err := common.Exists(filepath.Join(cCmd.dest, vega.DockerfileName))
if err != nil {
return fmt.Errorf("there was an error checking if %s exists: %v", vega.DockerfileName, err)
return fmt.Errorf("couldn't check if starterkit already exists: %v", err)
}

if dockerfileExists {
Expand All @@ -75,9 +75,9 @@ func (cCmd *createCmd) execute() error {
starterkit.Create(cCmd.dest)
} else if len(starterkits) > 0 {
// TODO: display proper list of matching kits
return fmt.Errorf("Multiple starterkit named %s found: %v", cCmd.starterkit, starterkits)
return fmt.Errorf("multiple starterkit named %s found: %v", cCmd.starterkit, starterkits)
} else {
return fmt.Errorf("No starterkit found with name %s in %s repo", cCmd.starterkit, cCmd.repo)
return fmt.Errorf("no starterkit found with name %s in %s repo", cCmd.starterkit, cCmd.repo)
}

fmt.Fprintln(cCmd.out, "Ready for development")
Expand Down
9 changes: 2 additions & 7 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ func newDownCmd(out io.Writer) *cobra.Command {
Short: downDesc,
Long: downDesc,
Run: func(cmd *cobra.Command, args []string) {
if tilt.IsInstalled() {
fmt.Fprintln(out, "Stopping the application")
tilt.Down(out, args...)
} else {
fmt.Fprintf(out, tilt.RequiredText)
fmt.Fprintf(out, tilt.InstallInstructions)
}
fmt.Fprintln(out, "Stopping the application")
tilt.Down(out, args...)
},
}

Expand Down
21 changes: 21 additions & 0 deletions cmd/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"io"

"github.com/spf13/cobra"
)

func newHooksCmd(out io.Writer) *cobra.Command {
const repoDesc = "manage git hooks"

repoCmd := &cobra.Command{
Use: "hooks",
Short: repoDesc,
Long: repoDesc,
}

repoCmd.AddCommand(newHooksInstallCmd(out))

return repoCmd
}
53 changes: 53 additions & 0 deletions cmd/hooks_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"fmt"
"io"

vega "github.com/srijanone/vega/pkg/core"

"github.com/spf13/cobra"
)

type hooksInstallCmd struct {
out io.Writer
home vega.Home
path string
}

func newHooksInstallCmd(out io.Writer) *cobra.Command {
hInstallCmd := hooksInstallCmd{out: out}

const hooksInstallDesc = "install git hooks in existing git repository"

installCmd := &cobra.Command{
Use: "install [project-path]",
Short: hooksInstallDesc,
Long: hooksInstallDesc,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
// If no arguments are passed, then choose current directory as path
hInstallCmd.path = "."
} else {
hInstallCmd.path = args[0]
}
hInstallCmd.home = vega.Home(homePath())
hInstallCmd.execute()
},
}

return installCmd
}

func (hInstallCmd *hooksInstallCmd) execute() {
fmt.Fprintf(hInstallCmd.out, "Installing git hooks to %s\n", hInstallCmd.path)

// Adding Git Hooks to Vega Home
gitHooks := vega.GitHooks{
Home: hInstallCmd.home,
Out: hInstallCmd.out,
}

// Installing Git Hooks to project
gitHooks.Install(hInstallCmd.path)
}
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ func (iCmd *initCmd) setupVegaHome() error {
gitHooks.Add()

// Installing Git Hooks as Global hooks
gitHooks.Install()
gitHooks.InstallGlobally()
return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func newRootCmd(out io.Writer, in io.Reader) *cobra.Command {
cmd.AddCommand(newUpCmd(out))
cmd.AddCommand(newDownCmd(out))
cmd.AddCommand(newRepoCmd(out))
cmd.AddCommand(newHooksCmd(out))

return cmd
}
Expand Down
21 changes: 8 additions & 13 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@ func newUpCmd(out io.Writer) *cobra.Command {
Short: upDesc,
Long: upDesc,
Run: func(cmd *cobra.Command, args []string) {
if tilt.IsInstalled() {
fmt.Fprintln(out, "Running the application")
args := []string{"--port", port}
if noBrowser {
args = append(args, "--no-browser")
}
if watch == false {
args = append(args, "--watch", "false")
}
tilt.Up(out, args...)
} else {
fmt.Fprintf(out, tilt.RequiredText)
fmt.Fprintf(out, tilt.InstallInstructions)
fmt.Fprintln(out, "Running the application")
upArgs := []string{"--port", port}
if noBrowser {
upArgs = append(upArgs, "--no-browser")
}
if watch == false {
upArgs = append(upArgs, "--watch", "false")
}
tilt.Up(out, upArgs...)
},
}

Expand Down
25 changes: 0 additions & 25 deletions hooks/generic/pre-commit/check-aws-keys.sh

This file was deleted.

28 changes: 28 additions & 0 deletions pkg/common/common_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
)

Expand Down Expand Up @@ -62,3 +63,30 @@ func CopyFile(sourceFile string, destFile string) error {
}
return nil
}

// ShellScriptOnly returns a walk function which only processes shell scripts
func ShellScriptOnly(files *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".sh" {
return nil
}
*files = append(*files, path)
return nil
}
}

// ListFiles returns the list of file names in given directory
func ListFiles(dir string) []string {
var files []string

// TODO: make walkFunc parameterized
err := filepath.Walk(dir, ShellScriptOnly(&files))
if err != nil {
panic(err)
}
return files
}

63 changes: 48 additions & 15 deletions pkg/core/git_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package vega
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

common "github.com/srijanone/vega/pkg/common"
downloader "github.com/srijanone/vega/pkg/downloader"
git "github.com/srijanone/vega/pkg/git"
)

const (
scriptHeader = "#!/usr/bin/env bash"
)

type GitHooks struct {
Path string // local absolute path to repo
Home Home
URL string
Dir string // hooks directory name at source/remote
Expand All @@ -26,34 +31,62 @@ func (gitHook *GitHooks) Add() {
}
sourceRepo := fmt.Sprintf("%s//%s", gitHook.URL, gitHook.Dir)
fmt.Println("Downloading git hooks...")
if gitHook.Path == "" {
gitHook.Path = gitHook.Home.GitHooks()
}
d.Download(sourceRepo, gitHook.Path)
d.Download(sourceRepo, gitHook.Home.GitHooks())
}

// Install installs Git Hooks as Global Git Hooks
func (gitHook *GitHooks) Install() {
// TODO: Use code instead of bash script to accommodate windows OS

// InstallGlobally installs Git Hooks as Global Git Hooks
func (gitHook *GitHooks) InstallGlobally() {
globalHooksDir := filepath.Join(common.DefaultHome(), ".git", "hooks")

fmt.Fprintf(gitHook.Out, "Creating Global Hooks Directory\n")
if err := common.EnsureDir(globalHooksDir); err != nil {
fmt.Fprintf(gitHook.Out, "Error in global hook directory: %v\n", err)
}

args := []string{"config", "--global", "core.hooksPath", globalHooksDir}
gitHook.createHook("pre-commit", globalHooksDir)

fmt.Fprintf(gitHook.Out, "Setting Global Git Hooks: %v\n", globalHooksDir)
args := []string{"config", "--global", "core.hooksPath", globalHooksDir}
git.Execute(gitHook.Out, args...)
}

sourceDir := filepath.Join(gitHook.Path, "generic", "pre-commit")
// Install installs Git Hooks to a git based project path
func (gitHook *GitHooks) Install(path string) {
gitHooksPath := filepath.Join(path, ".git", "hooks")

fmt.Fprintf(gitHook.Out, "Installing pre-commit hooks\n")
// TODO: Make it generic to install all hooks
err := common.CopyFile(filepath.Join(sourceDir, "check-aws-credentials.sh"), filepath.Join(globalHooksDir, "pre-commit"))
exist, err := common.Exists(gitHooksPath)
if err != nil {
fmt.Fprintf(gitHook.Out, "Error: %v\n", err)
fmt.Fprintf(gitHook.Out, "couldn't check if project is git based: %v\n", err)
return
}
if !exist {
fmt.Fprintf(gitHook.Out, "Project is not git based. in order to install git hooks, please run `git init`\n")
return
}

gitHook.createHook("pre-commit", gitHooksPath)

fmt.Fprintf(gitHook.Out, "Setting Up Local Git Hooks \n")
os.Chdir(path) // change directory to project path if user is not in current directory
args := []string{"config", "core.hooksPath", ".git/hooks"}
git.Execute(gitHook.Out, args...)
}

func (gitHook *GitHooks) createHook(hookName string, path string) {
fmt.Fprintf(gitHook.Out, "Installing %v hooks\n", hookName)
var shellScripts []string

preCommitHooksDir := filepath.Join(gitHook.Home.GitHooks(), "generic", hookName)
preCommitScriptBody := scriptHeader + "\n"

shellScripts = common.ListFiles(preCommitHooksDir)
for _, shellScript := range shellScripts {
fmt.Fprintf(gitHook.Out, "Adding hook: %v\n", shellScript)
preCommitScriptBody = preCommitScriptBody + "\n" + shellScript
}

err := ioutil.WriteFile(filepath.Join(path, hookName), []byte(preCommitScriptBody), 0755)
if err != nil {
fmt.Fprintf(gitHook.Out, "couldn't create %v hook: %v\n", hookName, err)
}
}
1 change: 0 additions & 1 deletion pkg/core/vega.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const (
// CheckForLatestVersion checks for latest releases
func CheckForLatestVersion() {
currentVersionStr := version.New().FormatVersion(true)

u := updater.NewUpdater(vegaRepo, currentVersionStr)

latestVersion, available, err := u.IsLatestAvailable()
Expand Down
12 changes: 10 additions & 2 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"errors"
"fmt"
"io"
"os"
Expand All @@ -22,12 +23,19 @@ func IsInstalled() bool {
return err == nil
}

func Execute(out io.Writer, arguments ...string) {
func Execute(out io.Writer, arguments ...string) error {
if !IsInstalled() {
fmt.Fprintf(out, RequiredText)
fmt.Fprintf(out, InstallInstructions)
return errors.New("git is not installed on system")
}

command := exec.Command(commandName, arguments...)
command.Stdout = out
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
fmt.Fprintln(out, "Error in Executing", err)
return err
}
return nil
}
12 changes: 10 additions & 2 deletions pkg/tilt/tilt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"os/exec"
"errors"
)

const (
Expand Down Expand Up @@ -33,12 +34,19 @@ func Down(out io.Writer, arguments ...string) {
execute(out, arguments...)
}

func execute(out io.Writer, arguments ...string) {
func execute(out io.Writer, arguments ...string) error {
if !IsInstalled() {
fmt.Fprintf(out, RequiredText)
fmt.Fprintf(out, InstallInstructions)
return errors.New("tilt is not installed on system")
}

command := exec.Command(commandName, arguments...)
command.Stdout = out
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
fmt.Fprintln(out, "Error in Executing", err)
return err
}
return nil
}

0 comments on commit 15c55d4

Please sign in to comment.