Skip to content

Commit 6e94b16

Browse files
committed
Import the jcli-ishell-plugin project
0 parents  commit 6e94b16

File tree

11 files changed

+867
-0
lines changed

11 files changed

+867
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Pull Request Build
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build:
10+
name: Build
11+
runs-on: macos-10.15
12+
steps:
13+
- name: Set up Go 1.13
14+
uses: actions/setup-go@v1
15+
with:
16+
go-version: 1.13
17+
id: go
18+
- name: Check out code into the Go module directory
19+
uses: actions/checkout@v1
20+
- name: Run GoReleaser
21+
uses: goreleaser/goreleaser-action@v1
22+
with:
23+
version: latest
24+
args: check
25+
- name: Build
26+
run: |
27+
make build
28+
- name: Test
29+
run: |
30+
make test

.github/workflows/release.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
goreleaser:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
- name: Unshallow
15+
run: git fetch --prune --unshallow
16+
- name: Set up Go
17+
uses: actions/setup-go@v1
18+
with:
19+
go-version: 1.13.x
20+
- name: Run GoReleaser
21+
uses: goreleaser/goreleaser-action@v1
22+
with:
23+
version: latest
24+
args: release --rm-dist
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
jcli-ishell-plugin

.goreleaser.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Official documentation at http://goreleaser.com
2+
project_name: jcli-ishell-plugin
3+
builds:
4+
- env:
5+
- CGO_ENABLED=0
6+
binary: jcli-ishell-plugin
7+
goarch:
8+
- amd64
9+
- arm64
10+
goos:
11+
- freebsd
12+
- windows
13+
- linux
14+
- darwin
15+
ignore:
16+
- goos: freebsd
17+
goarch: arm64
18+
ldflags:
19+
- -X github.com/jenkins-zh/jenkins-cli/app.version={{.Version}}
20+
- -X github.com/jenkins-zh/jenkins-cli/app.commit={{.ShortCommit}}
21+
- -X github.com/jenkins-zh/jenkins-cli/app.date={{.Date}}
22+
dist: release
23+
archives:
24+
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
25+
replacements:
26+
darwin: darwin
27+
linux: linux
28+
windows: windows
29+
amd64: amd64
30+
arm64: arm64
31+
format_overrides:
32+
- goos: windows
33+
format: zipREADME.md
34+
files:
35+
-
36+
checksum:
37+
name_template: 'checksums.txt'
38+
snapshot:
39+
name_template: "{{ .Tag }}-next-{{.ShortCommit}}"
40+
changelog:
41+
skip: false
42+
sort: asc
43+
filters:
44+
exclude:
45+
- '^docs:'
46+
- '^test:'

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NAME := jcli-ishell-plugin
2+
3+
build:
4+
go build
5+
chmod u+x $(NAME)
6+
7+
copy: build
8+
cp $(NAME) ~/.jenkins-cli/plugins
9+
10+
test:
11+
go test ./...
12+
13+
fmt:
14+
go fmt .
15+
gofmt -s -w .

README.md

Whitespace-only changes.

cmd/inner.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* These code lines should be moved into github.com/jenkins-zh/jenkins-cli at sometime
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"github.com/jenkins-zh/jenkins-cli/app/cmd/keyring"
9+
"github.com/jenkins-zh/jenkins-cli/client"
10+
"github.com/mitchellh/go-homedir"
11+
"gopkg.in/yaml.v2"
12+
"io/ioutil"
13+
"os"
14+
15+
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
16+
)
17+
18+
var config *appCfg.Config
19+
func getCurrentJenkins() (cfg *appCfg.JenkinsServer, err error) {
20+
if err = loadDefaultConfig(); err == nil {
21+
cfg = findJenkinsByName(config.Current)
22+
}
23+
return
24+
}
25+
26+
func getClient(jenkins *appCfg.JenkinsServer, jClient *client.JenkinsCore) {
27+
jClient.URL = jenkins.URL
28+
jClient.UserName = jenkins.UserName
29+
jClient.Token = jenkins.Token
30+
jClient.Proxy = jenkins.Proxy
31+
jClient.ProxyAuth = jenkins.ProxyAuth
32+
jClient.InsecureSkipVerify = jenkins.InsecureSkipVerify
33+
}
34+
35+
func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *appCfg.JenkinsServer, err error) {
36+
if jenkins, err = getCurrentJenkins(); err == nil && jenkins != nil {
37+
getClient(jenkins, jClient)
38+
}
39+
return
40+
}
41+
42+
func findJenkinsByName(name string) (jenkinsServer *appCfg.JenkinsServer) {
43+
if config == nil {
44+
return
45+
}
46+
47+
for _, cfg := range config.JenkinsServers {
48+
if cfg.Name == name {
49+
jenkinsServer = &cfg
50+
break
51+
}
52+
}
53+
return
54+
}
55+
56+
func getDefaultConfigPath() (configPath string, err error) {
57+
var userHome string
58+
userHome, err = homedir.Dir()
59+
if err == nil {
60+
configPath = fmt.Sprintf("%s/.jenkins-cli.yaml", userHome)
61+
}
62+
return
63+
}
64+
65+
func loadDefaultConfig() (err error) {
66+
var configPath string
67+
if configPath, err = getDefaultConfigPath(); err == nil {
68+
if _, err = os.Stat(configPath); err == nil {
69+
err = loadConfig(configPath)
70+
}
71+
}
72+
return
73+
}
74+
75+
func loadConfig(path string) (err error) {
76+
var content []byte
77+
if content, err = ioutil.ReadFile(path); err == nil {
78+
err = yaml.Unmarshal([]byte(content), &config)
79+
80+
keyring.LoadTokenFromKeyring(config)
81+
}
82+
return
83+
}

cmd/job-shell.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/abiosoft/ishell"
6+
"github.com/jenkins-zh/jenkins-cli/app"
7+
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
8+
jCLI "github.com/jenkins-zh/jenkins-cli/client"
9+
)
10+
11+
const (
12+
Client = "client"
13+
JobName = "jobName"
14+
)
15+
16+
// NewJobCmd create a command to deal with the job
17+
func NewJobCmd(args []string) (shell *ishell.Shell) {
18+
shell = ishell.New()
19+
20+
jclient := &jCLI.JobClient{
21+
JenkinsCore: jCLI.JenkinsCore{
22+
RoundTripper: nil,
23+
},
24+
}
25+
if _, err := getCurrentJenkinsAndClient(&(jclient.JenkinsCore)); err != nil {
26+
shell.Println(fmt.Errorf("cannot get the Jenkins Client: %v", err))
27+
return
28+
}
29+
shell.Set(Client, jclient)
30+
31+
shell.Println("interactive Jenkins job shell")
32+
shell.AddCmd(&ishell.Cmd{
33+
Name: "job",
34+
Help: "set or print current job name",
35+
Func: func(c *ishell.Context) {
36+
currentJobName := shell.Get(JobName)
37+
if len(c.Args) > 0 {
38+
shell.Set(JobName, c.Args[0])
39+
} else if currentJobName == nil {
40+
c.Println("job name cannot be empty")
41+
} else {
42+
c.Printf("current job name: %v\n", shell.Get(JobName))
43+
}
44+
},
45+
})
46+
shell.AddCmd(&ishell.Cmd{
47+
Name: "search",
48+
Help: "search all jobs",
49+
Func: func(c *ishell.Context) {
50+
client := shell.Get(Client).(*jCLI.JobClient)
51+
var items []jCLI.JenkinsItem
52+
var err error
53+
54+
if items, err = client.Search("", "", 0, 10); err == nil {
55+
output := common.OutputOption{
56+
Writer: &ShellWriter{Shell:c,},
57+
Columns: "Name,DisplayName,Type,URL",
58+
}
59+
err = output.OutputV2(items)
60+
}
61+
62+
if err != nil {
63+
c.Println(err)
64+
}
65+
},
66+
})
67+
shell.AddCmd(&ishell.Cmd{
68+
Name: "build",
69+
Help: "trigger current job",
70+
Func: func(c *ishell.Context) {
71+
client := shell.Get(Client).(*jCLI.JobClient)
72+
if err := client.Build(fmt.Sprintf("%v", shell.Get(JobName))); err != nil {
73+
c.Println(err)
74+
}
75+
},
76+
})
77+
shell.AddCmd(&ishell.Cmd{
78+
Name: "context",
79+
Help: "switch context between different Jenkins",
80+
Func: func(c *ishell.Context) {
81+
jenkinsList := []string{}
82+
for _, cfg := range config.JenkinsServers {
83+
jenkinsList = append(jenkinsList, cfg.Name)
84+
}
85+
86+
choice := c.MultiChoice(jenkinsList, "Which Jenkins do you want to choose?")
87+
jclient := &jCLI.JobClient{
88+
JenkinsCore: jCLI.JenkinsCore{
89+
RoundTripper: nil,
90+
},
91+
}
92+
getClient(&config.JenkinsServers[choice], &(jclient.JenkinsCore))
93+
shell.Set(Client, jclient)
94+
},
95+
})
96+
shell.AddCmd(&ishell.Cmd{
97+
Name: "current",
98+
Help: "show the current Jenkins",
99+
Func: func(c *ishell.Context) {
100+
c.Println("current Jenkins is", config.Current)
101+
},
102+
})
103+
shell.AddCmd(&ishell.Cmd{
104+
Name: "history",
105+
Help: "show the history of job builds",
106+
Func: func(c *ishell.Context) {
107+
client := shell.Get(Client).(*jCLI.JobClient)
108+
var builds []*jCLI.JobBuild
109+
var err error
110+
111+
if builds, err = client.GetHistory(fmt.Sprintf("%v", shell.Get(JobName))); err == nil {
112+
output := common.OutputOption{
113+
Writer: &ShellWriter{Shell:c,},
114+
Columns: "DisplayName,Building,Result",
115+
}
116+
err = output.OutputV2(builds)
117+
}
118+
119+
if err != nil {
120+
c.Print(err)
121+
}
122+
},
123+
})
124+
shell.AddCmd(&ishell.Cmd{
125+
Name: "version",
126+
Help: "show the version of this plugin",
127+
Func: func(c *ishell.Context) {
128+
c.Printf("Version: %s\n", app.GetVersion())
129+
c.Printf("Last Commit: %s\n", app.GetCommit())
130+
c.Printf("Build Date: %s\n", app.GetDate())
131+
},
132+
})
133+
return
134+
}
135+
136+
// ShellWriter combines ishell and io.Writer
137+
type ShellWriter struct {
138+
Shell *ishell.Context
139+
}
140+
141+
// Write outputs the data
142+
func (s * ShellWriter) Write(p []byte) (n int, err error) {
143+
s.Shell.Print(string(p))
144+
return
145+
}

go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/jenkins-zh/jcli-ishell-plugin
2+
3+
go 1.15
4+
5+
require (
6+
github.com/abiosoft/ishell v2.0.0+incompatible
7+
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
8+
github.com/chzyer/logex v1.1.10 // indirect
9+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
10+
github.com/fatih/color v1.10.0 // indirect
11+
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
12+
github.com/ghodss/yaml v1.0.0
13+
github.com/jenkins-zh/jenkins-cli v0.0.31
14+
github.com/mitchellh/go-homedir v1.1.0
15+
github.com/spf13/cobra v1.1.1
16+
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9
17+
gopkg.in/src-d/go-git.v4 v4.13.1
18+
gopkg.in/yaml.v2 v2.3.0
19+
)

0 commit comments

Comments
 (0)