提交 6e94b16d 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Import the jcli-ishell-plugin project

上级
name: Pull Request Build
on:
pull_request:
branches:
- master
jobs:
build:
name: Build
runs-on: macos-10.15
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: check
- name: Build
run: |
make build
- name: Test
run: |
make test
name: Release
on:
push:
tags:
- '*'
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.13.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
.idea
jcli-ishell-plugin
# Official documentation at http://goreleaser.com
project_name: jcli-ishell-plugin
builds:
- env:
- CGO_ENABLED=0
binary: jcli-ishell-plugin
goarch:
- amd64
- arm64
goos:
- freebsd
- windows
- linux
- darwin
ignore:
- goos: freebsd
goarch: arm64
ldflags:
- -X github.com/jenkins-zh/jenkins-cli/app.version={{.Version}}
- -X github.com/jenkins-zh/jenkins-cli/app.commit={{.ShortCommit}}
- -X github.com/jenkins-zh/jenkins-cli/app.date={{.Date}}
dist: release
archives:
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
replacements:
darwin: darwin
linux: linux
windows: windows
amd64: amd64
arm64: arm64
format_overrides:
- goos: windows
format: zipREADME.md
files:
-
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next-{{.ShortCommit}}"
changelog:
skip: false
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
NAME := jcli-ishell-plugin
build:
go build
chmod u+x $(NAME)
copy: build
cp $(NAME) ~/.jenkins-cli/plugins
test:
go test ./...
fmt:
go fmt .
gofmt -s -w .
/**
* These code lines should be moved into github.com/jenkins-zh/jenkins-cli at sometime
*/
package cmd
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/keyring"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
)
var config *appCfg.Config
func getCurrentJenkins() (cfg *appCfg.JenkinsServer, err error) {
if err = loadDefaultConfig(); err == nil {
cfg = findJenkinsByName(config.Current)
}
return
}
func getClient(jenkins *appCfg.JenkinsServer, jClient *client.JenkinsCore) {
jClient.URL = jenkins.URL
jClient.UserName = jenkins.UserName
jClient.Token = jenkins.Token
jClient.Proxy = jenkins.Proxy
jClient.ProxyAuth = jenkins.ProxyAuth
jClient.InsecureSkipVerify = jenkins.InsecureSkipVerify
}
func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *appCfg.JenkinsServer, err error) {
if jenkins, err = getCurrentJenkins(); err == nil && jenkins != nil {
getClient(jenkins, jClient)
}
return
}
func findJenkinsByName(name string) (jenkinsServer *appCfg.JenkinsServer) {
if config == nil {
return
}
for _, cfg := range config.JenkinsServers {
if cfg.Name == name {
jenkinsServer = &cfg
break
}
}
return
}
func getDefaultConfigPath() (configPath string, err error) {
var userHome string
userHome, err = homedir.Dir()
if err == nil {
configPath = fmt.Sprintf("%s/.jenkins-cli.yaml", userHome)
}
return
}
func loadDefaultConfig() (err error) {
var configPath string
if configPath, err = getDefaultConfigPath(); err == nil {
if _, err = os.Stat(configPath); err == nil {
err = loadConfig(configPath)
}
}
return
}
func loadConfig(path string) (err error) {
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
err = yaml.Unmarshal([]byte(content), &config)
keyring.LoadTokenFromKeyring(config)
}
return
}
package cmd
import (
"fmt"
"github.com/abiosoft/ishell"
"github.com/jenkins-zh/jenkins-cli/app"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
jCLI "github.com/jenkins-zh/jenkins-cli/client"
)
const (
Client = "client"
JobName = "jobName"
)
// NewJobCmd create a command to deal with the job
func NewJobCmd(args []string) (shell *ishell.Shell) {
shell = ishell.New()
jclient := &jCLI.JobClient{
JenkinsCore: jCLI.JenkinsCore{
RoundTripper: nil,
},
}
if _, err := getCurrentJenkinsAndClient(&(jclient.JenkinsCore)); err != nil {
shell.Println(fmt.Errorf("cannot get the Jenkins Client: %v", err))
return
}
shell.Set(Client, jclient)
shell.Println("interactive Jenkins job shell")
shell.AddCmd(&ishell.Cmd{
Name: "job",
Help: "set or print current job name",
Func: func(c *ishell.Context) {
currentJobName := shell.Get(JobName)
if len(c.Args) > 0 {
shell.Set(JobName, c.Args[0])
} else if currentJobName == nil {
c.Println("job name cannot be empty")
} else {
c.Printf("current job name: %v\n", shell.Get(JobName))
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "search",
Help: "search all jobs",
Func: func(c *ishell.Context) {
client := shell.Get(Client).(*jCLI.JobClient)
var items []jCLI.JenkinsItem
var err error
if items, err = client.Search("", "", 0, 10); err == nil {
output := common.OutputOption{
Writer: &ShellWriter{Shell:c,},
Columns: "Name,DisplayName,Type,URL",
}
err = output.OutputV2(items)
}
if err != nil {
c.Println(err)
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "build",
Help: "trigger current job",
Func: func(c *ishell.Context) {
client := shell.Get(Client).(*jCLI.JobClient)
if err := client.Build(fmt.Sprintf("%v", shell.Get(JobName))); err != nil {
c.Println(err)
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "context",
Help: "switch context between different Jenkins",
Func: func(c *ishell.Context) {
jenkinsList := []string{}
for _, cfg := range config.JenkinsServers {
jenkinsList = append(jenkinsList, cfg.Name)
}
choice := c.MultiChoice(jenkinsList, "Which Jenkins do you want to choose?")
jclient := &jCLI.JobClient{
JenkinsCore: jCLI.JenkinsCore{
RoundTripper: nil,
},
}
getClient(&config.JenkinsServers[choice], &(jclient.JenkinsCore))
shell.Set(Client, jclient)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "current",
Help: "show the current Jenkins",
Func: func(c *ishell.Context) {
c.Println("current Jenkins is", config.Current)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "history",
Help: "show the history of job builds",
Func: func(c *ishell.Context) {
client := shell.Get(Client).(*jCLI.JobClient)
var builds []*jCLI.JobBuild
var err error
if builds, err = client.GetHistory(fmt.Sprintf("%v", shell.Get(JobName))); err == nil {
output := common.OutputOption{
Writer: &ShellWriter{Shell:c,},
Columns: "DisplayName,Building,Result",
}
err = output.OutputV2(builds)
}
if err != nil {
c.Print(err)
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "version",
Help: "show the version of this plugin",
Func: func(c *ishell.Context) {
c.Printf("Version: %s\n", app.GetVersion())
c.Printf("Last Commit: %s\n", app.GetCommit())
c.Printf("Build Date: %s\n", app.GetDate())
},
})
return
}
// ShellWriter combines ishell and io.Writer
type ShellWriter struct {
Shell *ishell.Context
}
// Write outputs the data
func (s * ShellWriter) Write(p []byte) (n int, err error) {
s.Shell.Print(string(p))
return
}
\ No newline at end of file
module github.com/jenkins-zh/jcli-ishell-plugin
go 1.15
require (
github.com/abiosoft/ishell v2.0.0+incompatible
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/ghodss/yaml v1.0.0
github.com/jenkins-zh/jenkins-cli v0.0.31
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.1
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.3.0
)
此差异已折叠。
package main
import (
inner "github.com/jenkins-zh/jcli-ishell-plugin/cmd"
"os"
)
func main() {
cmd := inner.NewJobCmd(os.Args[1:])
cmd.Run()
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册