Skip to content

Commit f0def12

Browse files
committed
Clean up all dep of jcli
1 parent ad9ecd3 commit f0def12

File tree

10 files changed

+485
-76
lines changed

10 files changed

+485
-76
lines changed

go.mod

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ module github.com/linuxsuren/go-cli-plugin
33
go 1.15
44

55
require (
6+
github.com/golang/mock v1.4.4
7+
github.com/gosuri/uilive v0.0.3 // indirect
68
github.com/gosuri/uiprogress v0.0.1
7-
github.com/jenkins-zh/jenkins-cli v0.0.32
9+
github.com/mattn/go-isatty v0.0.12 // indirect
810
github.com/mitchellh/go-homedir v1.1.0
11+
github.com/onsi/ginkgo v1.14.2
12+
github.com/onsi/gomega v1.10.3
913
github.com/pkg/errors v0.9.1
1014
github.com/spf13/cobra v1.1.1
11-
go.uber.org/zap v1.16.0
15+
github.com/stretchr/testify v1.6.1 // indirect
1216
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
17+
golang.org/x/text v0.3.4 // indirect
1318
gopkg.in/src-d/go-git.v4 v4.13.1
1419
gopkg.in/yaml.v2 v2.4.0
1520
)

go.sum

Lines changed: 0 additions & 62 deletions
Large diffs are not rendered by default.

pkg/cmd/fetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error)
5656
return
5757
}
5858

59-
pluginRepo := fmt.Sprintf("%s/.jenkins-cli/plugins-repo", userHome)
59+
pluginRepo := fmt.Sprintf("%s/.%s/plugins-repo", userHome, c.PluginRepo)
6060
c.output = cmd.OutOrStdout()
6161

6262
var r *git.Repository

pkg/cmd/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *jcliPluginInstallCmd) Run(cmd *cobra.Command, args []string) (err error
4747
}
4848

4949
var data []byte
50-
pluginsMetadataFile := fmt.Sprintf("%s/.jenkins-cli/plugins-repo/%s.yaml", userHome, name)
50+
pluginsMetadataFile := fmt.Sprintf("%s/.%s/plugins-repo/%s.yaml", c.PluginRepo, userHome, name)
5151
if data, err = ioutil.ReadFile(pluginsMetadataFile); err == nil {
5252
plugin := pkg.Plugin{}
5353
if err = yaml.Unmarshal(data, &plugin); err == nil {
@@ -69,7 +69,7 @@ func (c *jcliPluginInstallCmd) download(plu pkg.Plugin) (err error) {
6969
}
7070

7171
link := c.getDownloadLink(plu)
72-
output := fmt.Sprintf("%s/.jenkins-cli/plugins/%s.tar.gz", userHome, plu.Main)
72+
output := fmt.Sprintf("%s/.%s/plugins/%s.tar.gz", userHome, c.PluginOrg,plu.Main)
7373

7474
downloader := pkg.HTTPDownloader{
7575
RoundTripper: c.RoundTripper,

pkg/output.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package pkg
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/jenkins-zh/jenkins-cli/app/i18n"
7-
"github.com/jenkins-zh/jenkins-cli/util"
86
"github.com/spf13/cobra"
97
"gopkg.in/yaml.v2"
108
"io"
@@ -76,7 +74,7 @@ func (o *OutputOption) OutputV2(obj interface{}) (err error) {
7674
case YAMLOutputFormat:
7775
data, err = yaml.Marshal(obj)
7876
case TableOutputFormat, "":
79-
table := util.CreateTableWithHeader(o.Writer, o.WithoutHeaders)
77+
table := CreateTableWithHeader(o.Writer, o.WithoutHeaders)
8078
table.AddHeader(strings.Split(o.Columns, ",")...)
8179
items := reflect.ValueOf(obj)
8280
for i := 0; i < items.Len(); i++ {
@@ -122,7 +120,7 @@ func (o *OutputOption) Match(item reflect.Value) bool {
122120
key := arr[0]
123121
val := arr[1]
124122

125-
if !strings.Contains(util.ReflectFieldValueAsString(item, key), val) {
123+
if !strings.Contains(ReflectFieldValueAsString(item, key), val) {
126124
return false
127125
}
128126
}
@@ -139,7 +137,7 @@ func (o *OutputOption) GetLine(obj reflect.Value) []string {
139137
}
140138

141139
for _, col := range columns {
142-
cell := util.ReflectFieldValueAsString(obj, col)
140+
cell := ReflectFieldValueAsString(obj, col)
143141
if renderCell, ok := o.CellRenderMap[col]; ok && renderCell != nil {
144142
cell = renderCell(cell)
145143
}
@@ -153,16 +151,16 @@ func (o *OutputOption) GetLine(obj reflect.Value) []string {
153151
// Deprecated, see also SetFlagWithHeaders
154152
func (o *OutputOption) SetFlag(cmd *cobra.Command) {
155153
cmd.Flags().StringVarP(&o.Format, "output", "o", TableOutputFormat,
156-
i18n.T("Format the output, supported formats: table, json, yaml"))
154+
"Format the output, supported formats: table, json, yaml")
157155
cmd.Flags().BoolVarP(&o.WithoutHeaders, "no-headers", "", false,
158-
i18n.T(`When using the default output format, don't print headers (default print headers)`))
156+
`When using the default output format, don't print headers (default print headers)`)
159157
cmd.Flags().StringArrayVarP(&o.Filter, "filter", "", []string{},
160-
i18n.T("Filter for the list by fields"))
158+
"Filter for the list by fields")
161159
}
162160

163161
// SetFlagWithHeaders set the flags of output
164162
func (o *OutputOption) SetFlagWithHeaders(cmd *cobra.Command, headers string) {
165163
o.SetFlag(cmd)
166164
cmd.Flags().StringVarP(&o.Columns, "columns", "", headers,
167-
i18n.T("The columns of table"))
165+
"The columns of table")
168166
}

pkg/padding.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package pkg
2+
3+
import (
4+
"math"
5+
"strings"
6+
"unicode"
7+
"unicode/utf8"
8+
)
9+
10+
const (
11+
// AlignLeft align left
12+
AlignLeft = 0
13+
// AlignCenter align center
14+
AlignCenter = 1
15+
// AlignRight align right
16+
AlignRight = 2
17+
)
18+
19+
// Pad give a pad
20+
func Pad(s, pad string, width int, align int) string {
21+
switch align {
22+
case AlignCenter:
23+
return PadCenter(s, pad, width)
24+
case AlignRight:
25+
return PadLeft(s, pad, width)
26+
default:
27+
return PadRight(s, pad, width)
28+
}
29+
}
30+
31+
// PadRight pas as right
32+
func PadRight(s, pad string, width int) string {
33+
gap := widthValue(s, width)
34+
if gap > 0 {
35+
return s + strings.Repeat(string(pad), gap)
36+
}
37+
return s
38+
}
39+
40+
// PadLeft pad as left
41+
func PadLeft(s, pad string, width int) string {
42+
gap := widthValue(s, width)
43+
if gap > 0 {
44+
return strings.Repeat(string(pad), gap) + s
45+
}
46+
return s
47+
}
48+
49+
// PadCenter pad as center
50+
func PadCenter(s, pad string, width int) string {
51+
gap := widthValue(s, width)
52+
if gap > 0 {
53+
gapLeft := int(math.Ceil(float64(gap / 2)))
54+
gapRight := gap - gapLeft
55+
return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight)
56+
}
57+
return s
58+
}
59+
60+
func isHan(s string) (isHan bool) {
61+
wh := []rune(s)
62+
for _, r := range wh {
63+
if unicode.Is(unicode.Han, r) {
64+
isHan = true
65+
} else if unicode.Is(unicode.Hiragana, r) {
66+
isHan = true
67+
} else if unicode.Is(unicode.Katakana, r) {
68+
isHan = true
69+
} else if unicode.Is(unicode.Common, r) {
70+
isHan = true
71+
} else {
72+
isHan = false
73+
break
74+
}
75+
}
76+
return
77+
}
78+
79+
func countCN(s string) (count int) {
80+
wh := []rune(s)
81+
for _, r := range wh {
82+
if unicode.Is(unicode.Han, r) {
83+
count++
84+
} else if unicode.Is(unicode.Hiragana, r) {
85+
count++
86+
} else if unicode.Is(unicode.Katakana, r) {
87+
count++
88+
} else if unicode.Is(unicode.Common, r) && len(string(r)) != 1 {
89+
count++
90+
}
91+
}
92+
return
93+
}
94+
95+
func widthValue(s string, width int) (gap int) {
96+
l := utf8.RuneCountInString(s)
97+
ln := len(s)
98+
isHan := isHan(s)
99+
count := countCN(s)
100+
if ln != l {
101+
if isHan {
102+
gap = width - (ln - l)
103+
} else {
104+
gap = width - (ln - count)
105+
}
106+
} else {
107+
gap = width - l
108+
}
109+
return
110+
}
111+
112+
// Lenf counts the number
113+
func Lenf(han string) (l int) {
114+
ln := len(han)
115+
l = utf8.RuneCountInString(han)
116+
isHan := isHan(han)
117+
count := countCN(han)
118+
if ln != l {
119+
if isHan {
120+
l = ln - l
121+
} else {
122+
l = ln - count
123+
}
124+
125+
}
126+
return
127+
}

pkg/padding_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package pkg
2+
3+
import (
4+
"github.com/golang/mock/gomock"
5+
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/gomega"
7+
)
8+
9+
var _ = Describe("Padding util test", func() {
10+
var (
11+
ctrl *gomock.Controller
12+
)
13+
14+
BeforeEach(func() {
15+
ctrl = gomock.NewController(GinkgoT())
16+
})
17+
18+
AfterEach(func() {
19+
ctrl.Finish()
20+
})
21+
22+
Context("isHan test", func() {
23+
var han string
24+
It("english test", func() {
25+
han = "ZSOMZAYNHG"
26+
Expect(isHan(han)).To(Equal(false))
27+
})
28+
It("chinese test", func() {
29+
han = "构建一个自由风格的软件项目"
30+
Expect(isHan(han)).To(Equal(true))
31+
})
32+
It("hk test", func() {
33+
han = "建置 Free-Style 軟體專案"
34+
Expect(isHan(han)).To(Equal(false))
35+
})
36+
It("japanese test", func() {
37+
han = "フリースタイル・プロジェクトのビルド"
38+
Expect(isHan(han)).To(Equal(true))
39+
})
40+
It("japanese and chinese and english test", func() {
41+
han = "フリースタイル・プ中ロジasdェクトのビルド"
42+
Expect(isHan(han)).To(Equal(false))
43+
})
44+
})
45+
46+
Context("countCN test", func() {
47+
var han string
48+
It("english test", func() {
49+
han = "ZSOMZAYNHG"
50+
Expect(countCN(han)).To(Equal(0))
51+
})
52+
It("chinese test", func() {
53+
han = "构建一个自由风格的软件项目"
54+
Expect(countCN(han)).To(Equal(13))
55+
})
56+
It("hk test", func() {
57+
han = "建置 Free-Style 軟體專案"
58+
Expect(countCN(han)).To(Equal(6))
59+
})
60+
It("japanese test", func() {
61+
han = "フリースタイル・プロジェクトのビルド"
62+
Expect(countCN(han)).To(Equal(18))
63+
})
64+
It("japanese and chinese and english test", func() {
65+
han = "フリースタイル・プ中ロジasdェクトのビルド"
66+
Expect(countCN(han)).To(Equal(19))
67+
})
68+
})
69+
70+
Context("Lenf test", func() {
71+
var han string
72+
It("english test", func() {
73+
han = "ZSOMZAYNHG"
74+
Expect(Lenf(han) == len(han)).To(Equal(true))
75+
})
76+
It("chinese test", func() {
77+
han = "构建一个自由风格的软件项目"
78+
Expect(Lenf(han) == len(han)).To(Equal(false))
79+
})
80+
It("hk test", func() {
81+
han = "建置 Free-Style 軟體專案"
82+
Expect(Lenf(han) == len(han)).To(Equal(false))
83+
})
84+
It("japanese test", func() {
85+
han = "フリースタイル・プロジェクトのビルド"
86+
Expect(Lenf(han) == len(han)).To(Equal(false))
87+
})
88+
It("japanese and chinese and english test", func() {
89+
han = "フリースタイル・プ中ロジasdェクトのビルド"
90+
Expect(Lenf(han) == len(han)).To(Equal(false))
91+
})
92+
})
93+
})

pkg/reflect.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pkg
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
// ReflectFieldValueAsString returns the value of a field
9+
func ReflectFieldValueAsString(v reflect.Value, field string) string {
10+
return fmt.Sprint(reflect.Indirect(v).FieldByName(field))
11+
}

0 commit comments

Comments
 (0)