Skip to content

Commit

Permalink
Merge pull request #5 from vorban/master
Browse files Browse the repository at this point in the history
Remove git hook from pliz
  • Loading branch information
Biptaste committed Jul 13, 2022
2 parents e0ac987 + 57b67f7 commit e8dbaad
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 130 deletions.
94 changes: 1 addition & 93 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main

import (
"embed"
"fmt"
"io/ioutil"
"os"
"regexp"
"sort"
Expand All @@ -18,9 +16,6 @@ import (
cli "github.com/jawher/mow.cli"
)

//go:embed resources/*
var resources embed.FS

func main() {

app := cli.App("pliz", "Manage projects building")
Expand Down Expand Up @@ -199,13 +194,7 @@ func main() {
}

/*
* 5. Plug Git precommit Hook
*/

cmd = domain.NewCommand([]string{"pliz", "git:plug-hook", "-i"})
cmd.Execute()
/*
* 6. The end
* 5. The end
*/

fmt.Printf("\n\n%s You may now run '%s' to launch your project\n", color.GreenString("✓"), color.MagentaString("pliz start"))
Expand Down Expand Up @@ -333,90 +322,9 @@ func main() {
}
})

app.Command("git:plug-hook", "Register hooks in the Git repository", func(cmd *cli.Cmd) {
include := cmd.BoolOpt("i include-phpcs-config", false, "Includes the default phpcs config, if not already setup in the repo")
force := cmd.BoolOpt("f force", false, "Forces the include of the default phpcs config, even if already setup in the repo")

cmd.Action = func() {
fmt.Printf("%s git:plug-hook\n\n", color.MagentaString("Executing:"))

plugPhpcsConfig(*include, *force)
plugHook()
}
})

app.Run(os.Args)
}

func plugPhpcsConfig(include bool, force bool) {
if !include {
return
}

_, err := ioutil.ReadFile(".phpcs.xml.dist")

// if file already exists and there is no force flag: return
if err == nil && !force {
fmt.Printf("%s .phpcs.xml.dist already exists.\n", color.YellowString("Skipping include:"))
return
}

config, err := resources.ReadFile("resources/.phpcs.xml.dist")
if err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to read default config:"), err)
return
}

// write +wr for everybody
if err := ioutil.WriteFile(".phpcs.xml.dist", config, 0555); err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to write file:"), err)
return
}

fmt.Printf("%s ./.phpcs.xml.dist\n", color.GreenString("Successfully plugged:"))
}

func plugHook() {
// open the hook code from the project config
script, err := ioutil.ReadFile("ops/git/hooks/pre-commit")

// if it cannot be read, plug the default hook instead
if err != nil {
plugDefaultHook()
return
}
plugProjectHook(script)
}

func plugProjectHook(script []byte) {
// write +wr for everybody, +x for user
err := ioutil.WriteFile(".git/hooks/pre-commit", script, 0755)

if err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to write file:"), err)
return
}
fmt.Printf("%s .git/hooks/pre-commit\n", color.GreenString("Successfully plugged:"))
}

func plugDefaultHook() {
fmt.Printf("./ops/git/hooks/pre-commit not found, fallback to default hook\n")

script, err := resources.ReadFile("resources/git-hooks/pre-commit")

if err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to read default hook:"), err)
return
}

if err := ioutil.WriteFile(".git/hooks/pre-commit", script, 0755); err != nil {
fmt.Printf("%s %v\n", color.RedString("Unable to write file:"), err)
return
}

fmt.Printf("%s .git/hooks/pre-commit\n", color.GreenString("Successfully plugged:"))
}

func parseAndCheckConfig() {
err := config.Check()
if err != nil {
Expand Down
17 changes: 0 additions & 17 deletions resources/.phpcs.xml.dist

This file was deleted.

20 changes: 0 additions & 20 deletions resources/git-hooks/pre-commit

This file was deleted.

0 comments on commit e8dbaad

Please sign in to comment.