Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jenkinsci
sub-group
Jenkins Cli
提交
1e4f543a
J
Jenkins Cli
项目概览
jenkinsci
/
sub-group
/
Jenkins Cli
通知
0
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Jenkins Cli
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1e4f543a
编写于
7月 01, 2019
作者:
LinuxSuRen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add user cmd
上级
c7ea09d5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
126 addition
and
0 deletion
+126
-0
Makefile
Makefile
+1
-0
app/cmd/user.go
app/cmd/user.go
+51
-0
client/user.go
client/user.go
+74
-0
未找到文件。
Makefile
浏览文件 @
1e4f543a
...
@@ -35,3 +35,4 @@ dep: ## Clean the generated artifacts
...
@@ -35,3 +35,4 @@ dep: ## Clean the generated artifacts
go get github.com/spf13/cobra
go get github.com/spf13/cobra
go get github.com/spf13/viper
go get github.com/spf13/viper
go get gopkg.in/yaml.v2
go get gopkg.in/yaml.v2
go get github.com/Pallinder/go-randomdata
app/cmd/user.go
0 → 100644
浏览文件 @
1e4f543a
package
cmd
import
(
"fmt"
"log"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
type
UserOption
struct
{
OutputOption
}
var
userOption
UserOption
func
init
()
{
rootCmd
.
AddCommand
(
userCmd
)
userCmd
.
Flags
()
.
StringVarP
(
&
userOption
.
Format
,
"output"
,
"o"
,
"json"
,
"Format the output"
)
}
var
userCmd
=
&
cobra
.
Command
{
Use
:
"user"
,
Short
:
"Print the user of your Jenkins"
,
Long
:
`Print the user of your Jenkins`
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
jenkins
:=
getCurrentJenkins
()
jclient
:=
&
client
.
UserClient
{}
jclient
.
URL
=
jenkins
.
URL
jclient
.
UserName
=
jenkins
.
UserName
jclient
.
Token
=
jenkins
.
Token
jclient
.
Proxy
=
jenkins
.
Proxy
jclient
.
ProxyAuth
=
jenkins
.
ProxyAuth
var
tokenName
string
if
len
(
args
)
>
0
{
tokenName
=
args
[
0
]
}
if
status
,
err
:=
jclient
.
Create
(
tokenName
);
err
==
nil
{
var
data
[]
byte
if
data
,
err
=
userOption
.
Output
(
status
);
err
==
nil
{
fmt
.
Printf
(
"%s
\n
"
,
string
(
data
))
}
else
{
log
.
Fatal
(
err
)
}
}
else
{
log
.
Fatal
(
err
)
}
},
}
client/user.go
0 → 100644
浏览文件 @
1e4f543a
package
client
import
(
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/Pallinder/go-randomdata"
)
type
UserClient
struct
{
JenkinsCore
}
type
Token
struct
{
Status
string
Data
TokenData
}
type
TokenData
struct
{
TokenName
string
TokenUuid
string
TokenValue
string
}
func
(
q
*
UserClient
)
Create
(
newTokenName
string
)
(
status
*
Token
,
err
error
)
{
if
newTokenName
==
""
{
newTokenName
=
fmt
.
Sprintf
(
"jcli-%s"
,
randomdata
.
SillyName
())
}
api
:=
fmt
.
Sprintf
(
"%s/user/%s/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken"
,
q
.
URL
,
q
.
UserName
)
var
(
req
*
http
.
Request
response
*
http
.
Response
)
formData
:=
url
.
Values
{}
formData
.
Add
(
"newTokenName"
,
newTokenName
)
payload
:=
strings
.
NewReader
(
formData
.
Encode
())
req
,
err
=
http
.
NewRequest
(
"POST"
,
api
,
payload
)
if
err
==
nil
{
q
.
AuthHandle
(
req
)
}
else
{
return
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
if
err
=
q
.
CrumbHandle
(
req
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
client
:=
q
.
GetClient
()
if
response
,
err
=
client
.
Do
(
req
);
err
==
nil
{
code
:=
response
.
StatusCode
var
data
[]
byte
data
,
err
=
ioutil
.
ReadAll
(
response
.
Body
)
if
code
==
200
{
if
err
==
nil
{
status
=
&
Token
{}
err
=
json
.
Unmarshal
(
data
,
status
)
}
}
else
{
log
.
Fatal
(
string
(
data
))
}
}
else
{
log
.
Fatal
(
err
)
}
return
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录