Skip to content

Commit

Permalink
Replace the computer client with jenkins-zh/jenkins-client (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Aug 27, 2021
1 parent 99d81bf commit db917cf
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 483 deletions.
11 changes: 6 additions & 5 deletions app/cmd/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"github.com/jenkins-zh/jenkins-client/pkg/core"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -21,13 +22,13 @@ var computerCmd = &cobra.Command{
}

// GetComputerClient returns the client of computer
func GetComputerClient(option common.Option) (*client.ComputerClient, *appCfg.JenkinsServer) {
jClient := &client.ComputerClient{
JenkinsCore: client.JenkinsCore{
func GetComputerClient(option common.Option) (*computer.Client, *appCfg.JenkinsServer) {
jClient := &computer.Client{
JenkinsCore: core.JenkinsCore{
RoundTripper: option.RoundTripper,
},
}
return jClient, getCurrentJenkinsAndClient(&(jClient.JenkinsCore))
return jClient, getCurrentJenkinsAndClientV2(&(jClient.JenkinsCore))
}

// ValidAgentNames autocomplete with agent names
Expand Down
7 changes: 3 additions & 4 deletions app/cmd/computer_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)

var _ = Describe("create list command", func() {
Expand Down Expand Up @@ -57,7 +56,7 @@ var _ = Describe("create list command", func() {
It("should success", func() {
name := "fake-name"

client.PrepareForComputerCreateRequest(roundTripper, "http://localhost:8080/jenkins",
computer.PrepareForComputerCreateRequest(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9", name)

rootCmd.SetArgs([]string{"computer", "create", name})
Expand Down
7 changes: 3 additions & 4 deletions app/cmd/computer_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)

var _ = Describe("create delete command", func() {
Expand Down Expand Up @@ -57,7 +56,7 @@ var _ = Describe("create delete command", func() {
It("should success", func() {
name := "fake-name"

client.PrepareForComputerDeleteRequest(roundTripper, "http://localhost:8080/jenkins",
computer.PrepareForComputerDeleteRequest(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9", name)

rootCmd.SetArgs([]string{"computer", "delete", name})
Expand Down
6 changes: 3 additions & 3 deletions app/cmd/computer_launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
appCfg "github.com/jenkins-zh/jenkins-cli/app/config"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
httpdownloader "github.com/linuxsuren/http-downloader/pkg"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand All @@ -25,7 +25,7 @@ type ComputerLaunchOption struct {
ShowProgress bool

/** share info between inner functions */
ComputerClient *client.ComputerClient
ComputerClient *computer.Client
CurrentJenkins *appCfg.JenkinsServer
Output string

Expand Down Expand Up @@ -270,7 +270,7 @@ func (o *ComputerLaunchOption) LaunchJnlp(name string) (err error) {
env := os.Environ()
agentArgs := []string{"java", "-jar", computerLaunchOption.Output,
"-jnlpUrl", fmt.Sprintf("%s/computer/%s/slave-agent.jnlp", o.ComputerClient.URL, name),
"-secret", secret, "-workDir", client.GetDefaultAgentWorkDir()}
"-secret", secret, "-workDir", computer.GetDefaultAgentWorkDir()}

if o.CurrentJenkins.ProxyAuth != "" {
proxyURL, _ := url.Parse(o.CurrentJenkins.Proxy)
Expand Down
10 changes: 5 additions & 5 deletions app/cmd/computer_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io"
"io/ioutil"
"net/http"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)

var _ = Describe("computer launch command", func() {
Expand Down Expand Up @@ -55,7 +55,7 @@ var _ = Describe("computer launch command", func() {
Context("launch a default type of agent", func() {
It("should success", func() {

client.PrepareForLaunchComputer(roundTripper, "http://localhost:8080/jenkins",
computer.PrepareForLaunchComputer(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9", name)

rootCmd.SetArgs([]string{"computer", "launch", name})
Expand Down Expand Up @@ -83,7 +83,7 @@ var _ = Describe("computer launch command", func() {
RoundTrip(client.NewRequestMatcher(request)).Return(response, nil)

secret := "fake-secret"
client.PrepareForComputerAgentSecretRequest(roundTripper,
computer.PrepareForComputerAgentSecretRequest(roundTripper,
"http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", name, secret)
})

Expand Down
4 changes: 2 additions & 2 deletions app/cmd/computer_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
cobra_ext "github.com/linuxsuren/cobra-extension"

"github.com/jenkins-zh/jenkins-cli/app/i18n"
Expand Down Expand Up @@ -35,7 +35,7 @@ var computerListCmd = &cobra.Command{
return
}

var computers client.ComputerList
var computers computer.List
if computers, err = jClient.List(); err == nil {
computerListOption.Writer = cmd.OutOrStdout()
computerListOption.CellRenderMap = map[string]cobra_ext.RenderCell{
Expand Down
7 changes: 3 additions & 4 deletions app/cmd/computer_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)

var _ = Describe("computer list command", func() {
Expand Down Expand Up @@ -54,7 +53,7 @@ var _ = Describe("computer list command", func() {
})

It("should success", func() {
client.PrepareForComputerListRequest(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9")
computer.PrepareForComputerListRequest(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9")

rootCmd.SetArgs([]string{"computer", "list"})
_, err = rootCmd.ExecuteC()
Expand Down
7 changes: 3 additions & 4 deletions app/cmd/computer_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"bytes"
"github.com/jenkins-zh/jenkins-client/pkg/computer"
"io/ioutil"
"os"

"github.com/jenkins-zh/jenkins-cli/client"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
"github.com/jenkins-zh/jenkins-client/pkg/mock/mhttp"
)

var _ = Describe("computer log command", func() {
Expand Down Expand Up @@ -56,7 +55,7 @@ var _ = Describe("computer log command", func() {
It("should success", func() {
name := "fake"

client.PrepareForComputerLogRequest(roundTripper, "http://localhost:8080/jenkins",
computer.PrepareForComputerLogRequest(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9", name)

rootCmd.SetArgs([]string{"computer", "log", name})
Expand Down
14 changes: 14 additions & 0 deletions app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/google/go-github/v29/github"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-client/pkg/core"
alias "github.com/linuxsuren/go-cli-alias/pkg"
"github.com/linuxsuren/go-cli-alias/pkg/cmd"
goPlugin "github.com/linuxsuren/go-cli-plugin/pkg"
Expand Down Expand Up @@ -452,6 +453,19 @@ func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *appCfg.Je
return
}

// this is a temporary function to adopt github.com/jenkins-zh/jenkins-client
func getCurrentJenkinsAndClientV2(jClient *core.JenkinsCore) (jenkins *appCfg.JenkinsServer) {
if jenkins = GetCurrentJenkinsFromOptions(); jenkins != nil {
jClient.URL = jenkins.URL
jClient.UserName = jenkins.UserName
jClient.Token = jenkins.Token
jClient.Proxy = jenkins.Proxy
jClient.ProxyAuth = jenkins.ProxyAuth
jClient.InsecureSkipVerify = jenkins.InsecureSkipVerify
}
return
}

// GetCurrentJenkinsAndClient returns the client
func GetCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *appCfg.JenkinsServer) {
if jenkins = GetCurrentJenkinsFromOptions(); jenkins != nil {
Expand Down
Loading

0 comments on commit db917cf

Please sign in to comment.