Skip to content

Commit

Permalink
Merge pull request #1 from LinuxSuRen/clean-up-jcli-dep
Browse files Browse the repository at this point in the history
Clean up all dep of jcli
  • Loading branch information
LinuxSuRen committed Dec 4, 2020
2 parents ad9ecd3 + f0def12 commit 03bead8
Show file tree
Hide file tree
Showing 10 changed files with 485 additions and 76 deletions.
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ module github.com/linuxsuren/go-cli-plugin
go 1.15

require (
github.com/golang/mock v1.4.4
github.com/gosuri/uilive v0.0.3 // indirect
github.com/gosuri/uiprogress v0.0.1
github.com/jenkins-zh/jenkins-cli v0.0.32
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.1
go.uber.org/zap v1.16.0
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
golang.org/x/text v0.3.4 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.4.0
)
62 changes: 0 additions & 62 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *jcliPluginFetchCmd) Run(cmd *cobra.Command, args []string) (err error)
return
}

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

var r *git.Repository
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *jcliPluginInstallCmd) Run(cmd *cobra.Command, args []string) (err error
}

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

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

downloader := pkg.HTTPDownloader{
RoundTripper: c.RoundTripper,
Expand Down
16 changes: 7 additions & 9 deletions pkg/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package pkg
import (
"encoding/json"
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io"
Expand Down Expand Up @@ -76,7 +74,7 @@ func (o *OutputOption) OutputV2(obj interface{}) (err error) {
case YAMLOutputFormat:
data, err = yaml.Marshal(obj)
case TableOutputFormat, "":
table := util.CreateTableWithHeader(o.Writer, o.WithoutHeaders)
table := CreateTableWithHeader(o.Writer, o.WithoutHeaders)
table.AddHeader(strings.Split(o.Columns, ",")...)
items := reflect.ValueOf(obj)
for i := 0; i < items.Len(); i++ {
Expand Down Expand Up @@ -122,7 +120,7 @@ func (o *OutputOption) Match(item reflect.Value) bool {
key := arr[0]
val := arr[1]

if !strings.Contains(util.ReflectFieldValueAsString(item, key), val) {
if !strings.Contains(ReflectFieldValueAsString(item, key), val) {
return false
}
}
Expand All @@ -139,7 +137,7 @@ func (o *OutputOption) GetLine(obj reflect.Value) []string {
}

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

// SetFlagWithHeaders set the flags of output
func (o *OutputOption) SetFlagWithHeaders(cmd *cobra.Command, headers string) {
o.SetFlag(cmd)
cmd.Flags().StringVarP(&o.Columns, "columns", "", headers,
i18n.T("The columns of table"))
"The columns of table")
}
127 changes: 127 additions & 0 deletions pkg/padding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package pkg

import (
"math"
"strings"
"unicode"
"unicode/utf8"
)

const (
// AlignLeft align left
AlignLeft = 0
// AlignCenter align center
AlignCenter = 1
// AlignRight align right
AlignRight = 2
)

// Pad give a pad
func Pad(s, pad string, width int, align int) string {
switch align {
case AlignCenter:
return PadCenter(s, pad, width)
case AlignRight:
return PadLeft(s, pad, width)
default:
return PadRight(s, pad, width)
}
}

// PadRight pas as right
func PadRight(s, pad string, width int) string {
gap := widthValue(s, width)
if gap > 0 {
return s + strings.Repeat(string(pad), gap)
}
return s
}

// PadLeft pad as left
func PadLeft(s, pad string, width int) string {
gap := widthValue(s, width)
if gap > 0 {
return strings.Repeat(string(pad), gap) + s
}
return s
}

// PadCenter pad as center
func PadCenter(s, pad string, width int) string {
gap := widthValue(s, width)
if gap > 0 {
gapLeft := int(math.Ceil(float64(gap / 2)))
gapRight := gap - gapLeft
return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight)
}
return s
}

func isHan(s string) (isHan bool) {
wh := []rune(s)
for _, r := range wh {
if unicode.Is(unicode.Han, r) {
isHan = true
} else if unicode.Is(unicode.Hiragana, r) {
isHan = true
} else if unicode.Is(unicode.Katakana, r) {
isHan = true
} else if unicode.Is(unicode.Common, r) {
isHan = true
} else {
isHan = false
break
}
}
return
}

func countCN(s string) (count int) {
wh := []rune(s)
for _, r := range wh {
if unicode.Is(unicode.Han, r) {
count++
} else if unicode.Is(unicode.Hiragana, r) {
count++
} else if unicode.Is(unicode.Katakana, r) {
count++
} else if unicode.Is(unicode.Common, r) && len(string(r)) != 1 {
count++
}
}
return
}

func widthValue(s string, width int) (gap int) {
l := utf8.RuneCountInString(s)
ln := len(s)
isHan := isHan(s)
count := countCN(s)
if ln != l {
if isHan {
gap = width - (ln - l)
} else {
gap = width - (ln - count)
}
} else {
gap = width - l
}
return
}

// Lenf counts the number
func Lenf(han string) (l int) {
ln := len(han)
l = utf8.RuneCountInString(han)
isHan := isHan(han)
count := countCN(han)
if ln != l {
if isHan {
l = ln - l
} else {
l = ln - count
}

}
return
}
93 changes: 93 additions & 0 deletions pkg/padding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package pkg

import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Padding util test", func() {
var (
ctrl *gomock.Controller
)

BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
})

AfterEach(func() {
ctrl.Finish()
})

Context("isHan test", func() {
var han string
It("english test", func() {
han = "ZSOMZAYNHG"
Expect(isHan(han)).To(Equal(false))
})
It("chinese test", func() {
han = "构建一个自由风格的软件项目"
Expect(isHan(han)).To(Equal(true))
})
It("hk test", func() {
han = "建置 Free-Style 軟體專案"
Expect(isHan(han)).To(Equal(false))
})
It("japanese test", func() {
han = "フリースタイル・プロジェクトのビルド"
Expect(isHan(han)).To(Equal(true))
})
It("japanese and chinese and english test", func() {
han = "フリースタイル・プ中ロジasdェクトのビルド"
Expect(isHan(han)).To(Equal(false))
})
})

Context("countCN test", func() {
var han string
It("english test", func() {
han = "ZSOMZAYNHG"
Expect(countCN(han)).To(Equal(0))
})
It("chinese test", func() {
han = "构建一个自由风格的软件项目"
Expect(countCN(han)).To(Equal(13))
})
It("hk test", func() {
han = "建置 Free-Style 軟體專案"
Expect(countCN(han)).To(Equal(6))
})
It("japanese test", func() {
han = "フリースタイル・プロジェクトのビルド"
Expect(countCN(han)).To(Equal(18))
})
It("japanese and chinese and english test", func() {
han = "フリースタイル・プ中ロジasdェクトのビルド"
Expect(countCN(han)).To(Equal(19))
})
})

Context("Lenf test", func() {
var han string
It("english test", func() {
han = "ZSOMZAYNHG"
Expect(Lenf(han) == len(han)).To(Equal(true))
})
It("chinese test", func() {
han = "构建一个自由风格的软件项目"
Expect(Lenf(han) == len(han)).To(Equal(false))
})
It("hk test", func() {
han = "建置 Free-Style 軟體專案"
Expect(Lenf(han) == len(han)).To(Equal(false))
})
It("japanese test", func() {
han = "フリースタイル・プロジェクトのビルド"
Expect(Lenf(han) == len(han)).To(Equal(false))
})
It("japanese and chinese and english test", func() {
han = "フリースタイル・プ中ロジasdェクトのビルド"
Expect(Lenf(han) == len(han)).To(Equal(false))
})
})
})
11 changes: 11 additions & 0 deletions pkg/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg

import (
"fmt"
"reflect"
)

// ReflectFieldValueAsString returns the value of a field
func ReflectFieldValueAsString(v reflect.Value, field string) string {
return fmt.Sprint(reflect.Indirect(v).FieldByName(field))
}
Loading

0 comments on commit 03bead8

Please sign in to comment.