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+ }
0 commit comments