未验证 提交 b33eb322 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Merge pull request #117 from LinuxSuRen/prehook

Add support for cmd pre hook
...@@ -12,4 +12,6 @@ ...@@ -12,4 +12,6 @@
*.out *.out
bin/ bin/
release/ release/
\ No newline at end of file
*.xml
...@@ -33,13 +33,15 @@ pipeline { ...@@ -33,13 +33,15 @@ pipeline {
stage('Test') { stage('Test') {
steps { steps {
container('golang') { container('golang') {
sh label: 'go test', script: ''' sh label: 'go test', script: 'make test'
go test ./util/... -v
go test ./client/... -v
go test ./app/... -v
'''
} }
} }
} }
} }
post {
always {
junit allowEmptyResults: true, testResults: "*/**/*.xml"
}
}
} }
\ No newline at end of file
...@@ -31,6 +31,11 @@ release: clean build-all ...@@ -31,6 +31,11 @@ release: clean build-all
clean: ## Clean the generated artifacts clean: ## Clean the generated artifacts
rm -rf bin release rm -rf bin release
test:
go test ./util/... -v -coverprofile bin/testcover-util.log
go test ./client/... -v -coverprofile bin/testcover-client.log
go test ./app/... -v -coverprofile bin/testcover-app.log
dep: dep:
go get github.com/AlecAivazis/survey/v2 go get github.com/AlecAivazis/survey/v2
go get github.com/gosuri/uiprogress go get github.com/gosuri/uiprogress
......
...@@ -50,9 +50,15 @@ type JenkinsServer struct { ...@@ -50,9 +50,15 @@ type JenkinsServer struct {
Description string `yaml:"description"` Description string `yaml:"description"`
} }
type PreHook struct {
Path string `yaml:"path"`
Command string `yaml:"cmd"`
}
type Config struct { type Config struct {
Current string `yaml:"current"` Current string `yaml:"current"`
JenkinsServers []JenkinsServer `yaml:"jenkins_servers"` JenkinsServers []JenkinsServer `yaml:"jenkins_servers"`
PreHooks []PreHook `yaml:"preHooks"`
} }
func setCurrentJenkins(name string) { func setCurrentJenkins(name string) {
......
...@@ -40,7 +40,7 @@ var pluginUploadCmd = &cobra.Command{ ...@@ -40,7 +40,7 @@ var pluginUploadCmd = &cobra.Command{
Long: `Upload a plugin from local filesystem or remote URL to your Jenkins`, Long: `Upload a plugin from local filesystem or remote URL to your Jenkins`,
Example: ` jcli plugin upload --remote https://server/sample.hpi Example: ` jcli plugin upload --remote https://server/sample.hpi
jcli plugin upload sample.hpi`, jcli plugin upload sample.hpi`,
PreRun: func(_ *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
if pluginUploadOption.Remote != "" { if pluginUploadOption.Remote != "" {
file, err := ioutil.TempFile(".", "jcli-plugin") file, err := ioutil.TempFile(".", "jcli-plugin")
if err != nil { if err != nil {
...@@ -70,6 +70,8 @@ var pluginUploadCmd = &cobra.Command{ ...@@ -70,6 +70,8 @@ var pluginUploadCmd = &cobra.Command{
log.Fatal(err) log.Fatal(err)
} }
} else if len(args) == 0 { } else if len(args) == 0 {
executePreCmd(cmd, args)
path, _ := os.Getwd() path, _ := os.Getwd()
dirName := filepath.Base(path) dirName := filepath.Base(path)
dirName = strings.Replace(dirName, "-plugin", "", -1) dirName = strings.Replace(dirName, "-plugin", "", -1)
......
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/exec"
"strings"
"github.com/jenkins-zh/jenkins-cli/app" "github.com/jenkins-zh/jenkins-cli/app"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -62,3 +64,43 @@ func initConfig() { ...@@ -62,3 +64,43 @@ func initConfig() {
log.Fatalf("Config file is invalid: %v", err) log.Fatalf("Config file is invalid: %v", err)
} }
} }
func getCmdPath(cmd *cobra.Command) string {
current := cmd.Use
if cmd.HasParent() {
parentName := getCmdPath(cmd.Parent())
if parentName == "" {
return current
}
return fmt.Sprintf("%s.%s", parentName, current)
}
// don't need the name of root cmd
return ""
}
func executePreCmd(cmd *cobra.Command, _ []string) {
config := getConfig()
if config == nil {
log.Fatal("Cannot find config file")
return
}
path := getCmdPath(cmd)
for _, preHook := range config.PreHooks {
if path != preHook.Path {
continue
}
execute(preHook.Command)
}
}
func execute(command string) {
array := strings.Split(command, " ")
cmd := exec.Command(array[0], array[1:]...)
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
package cmd
import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
)
var _ = Describe("Root cmd test", func() {
var (
ctrl *gomock.Controller
rootCmd *cobra.Command
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
rootCmd = &cobra.Command{Use: "root"}
})
AfterEach(func() {
ctrl.Finish()
})
Context("PreHook test", func() {
It("only with root cmd", func() {
path := getCmdPath(rootCmd)
Expect(path).To(Equal(""))
})
It("one sub cmd", func() {
subCmd := &cobra.Command{
Use: "sub",
}
rootCmd.AddCommand(subCmd)
path := getCmdPath(subCmd)
Expect(path).To(Equal("sub"))
})
It("two sub cmds", func() {
sub1Cmd := &cobra.Command{
Use: "sub1",
}
sub2Cmd := &cobra.Command{
Use: "sub2",
}
rootCmd.AddCommand(sub1Cmd)
sub1Cmd.AddCommand(sub2Cmd)
path := getCmdPath(sub2Cmd)
Expect(path).To(Equal("sub1.sub2"))
})
})
})
package cmd
import (
"testing"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestApp(t *testing.T) {
RegisterFailHandler(Fail)
junitReporter := reporters.NewJUnitReporter("test-app.xml")
RunSpecsWithDefaultAndCustomReporters(t, "app/cmd", []Reporter{junitReporter})
}
...@@ -3,11 +3,14 @@ package util ...@@ -3,11 +3,14 @@ package util
import ( import (
"testing" "testing"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
func TestUtils(t *testing.T) { func TestUtils(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
RunSpecs(t, "jcli utils test") junitReporter := reporters.NewJUnitReporter("test-utils.xml")
RunSpecsWithDefaultAndCustomReporters(t, "util", []Reporter{junitReporter})
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册