Skip to content

Commit

Permalink
Add version command to show the version
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Apr 20, 2020
1 parent a4143fe commit 9f6df1c
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jcli-account-plugin
*.iml
.idea
release
release
.DS_Store
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ builds:
ldflags:
- -X github.com/jenkins-zh/jenkins-cli/app.version={{.Version}}
- -X github.com/jenkins-zh/jenkins-cli/app.commit={{.ShortCommit}}
- -X github.com/jenkins-zh/jenkins-cli/app.date={{.Date}}
dist: release
archives:
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ copy:

test:
go test ./...

fmt:
go fmt .
gofmt -s -w .
50 changes: 44 additions & 6 deletions cmd/account.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package cmd

import (
"crypto/tls"
"fmt"
"github.com/ghodss/yaml"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)

// NewAccountCmd create a command to deal with account
func NewAccountCmd(args []string) (cmd *cobra.Command) {
accountCmd := &accountCmd{}

cmd = &cobra.Command {
Use: "jcli account",
cmd = &cobra.Command{
Use: "jcli account",
Short: "jcli config file account",
Long: "jcli config file account",
Long: "jcli config file account",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
accountCmd.output = cmd.OutOrStdout()
},
Expand All @@ -31,7 +36,7 @@ func NewAccountCmd(args []string) (cmd *cobra.Command) {
cmd.SetOut(os.Stdout)

// add flags to this command
flags := cmd.PersistentFlags();
flags := cmd.PersistentFlags()
flags.StringVarP(&accountCmd.URL, "url", "", "",
"The URL of a git repository")
flags.StringVarP(&accountCmd.Username, "username", "u", "",
Expand All @@ -45,13 +50,20 @@ func NewAccountCmd(args []string) (cmd *cobra.Command) {
flags.StringVarP(&accountCmd.SSHKeyFile, "ssh-key-file", "", sshKeyFile,
"SSH key file")

// add proxy flags
flags.StringVarP(&accountCmd.Proxy, "proxy", "", "",
"Proxy of connection")
flags.StringVarP(&accountCmd.ProxyAuth, "proxy-auth", "", "",
"Proxy auth of connection")

// add sub-commands
cmd.AddCommand(NewAccountAddCmd(args, accountCmd),
NewAccountUpdateCmd(args, accountCmd),
NewAccountSelectCmd(args, accountCmd),
NewAccountListCmd(args, accountCmd),
NewAccountRemoveCmd(args, accountCmd),
NewAccountDocCmd(cmd))
NewAccountDocCmd(cmd),
NewVersionCmd())
return
}

Expand Down Expand Up @@ -138,7 +150,7 @@ func (c *accountCmd) getDefaultConfigPath() (configPath string, err error) {
return
}

func (c* accountCmd) setName(cmd *cobra.Command, args []string) (err error) {
func (c *accountCmd) setName(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
c.Name = args[0]
}
Expand All @@ -148,3 +160,29 @@ func (c* accountCmd) setName(cmd *cobra.Command, args []string) (err error) {
}
return
}

func (c *accountCmd) installProtocol() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
ProxyConnectHeader: http.Header{},
}

if err := util.SetProxy(c.Proxy, c.ProxyAuth, tr); err != nil {
_, _ = c.output.Write([]byte(fmt.Sprintf("set proxy error %#v\n", err.Error())))
}

// Create a custom http(s) client with your config
customClient := &http.Client{
// accept any certificate (might be useful for testing)
Transport: tr,
// 15 second timeout
Timeout: 15 * time.Second,
// don't follow redirect
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

client.InstallProtocol("http", githttp.NewClient(customClient))
client.InstallProtocol("https", githttp.NewClient(customClient))
}
9 changes: 5 additions & 4 deletions cmd/account_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
func NewAccountAddCmd(args []string, accountCmd *accountCmd) (cmd *cobra.Command) {
accountAddCmd := accountAddCmd{accountCmd}
cmd = &cobra.Command{
Use: "add",
Short: "Add a account for jcli config",
Use: "add",
Short: "Add a account for jcli config",
PreRunE: accountAddCmd.preRunE,
RunE: accountAddCmd.Run,
RunE: accountAddCmd.Run,
}
return
}

func (c * accountAddCmd) preRunE(cmd *cobra.Command, args []string) (err error) {
func (c *accountAddCmd) preRunE(cmd *cobra.Command, args []string) (err error) {
err = c.setName(cmd, args)
return
}
Expand All @@ -34,6 +34,7 @@ func (c *accountAddCmd) Run(cmd *cobra.Command, args []string) (err error) {
return
}

c.installProtocol()
if _, err = git.PlainOpen(accountDir); err == nil {
err = fmt.Errorf("%s is exists", c.Name)
return
Expand Down
6 changes: 3 additions & 3 deletions cmd/account_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func NewAccountDocCmd(rootCmd *cobra.Command) (cmd *cobra.Command) {
RootCommand: rootCmd,
}
cmd = &cobra.Command{
Use: "doc",
Use: "doc",
Short: "Generate documents for jcli account plugin",
RunE: accountDocCmd.runE,
RunE: accountDocCmd.runE,
}

flags := cmd.Flags()
Expand All @@ -53,7 +53,7 @@ func NewAccountDocCmd(rootCmd *cobra.Command) (cmd *cobra.Command) {
return
}

func (c * accountDocCmd) runE(cmd *cobra.Command, args []string) (err error) {
func (c *accountDocCmd) runE(cmd *cobra.Command, args []string) (err error) {
outputDir := args[0]
if err = os.MkdirAll(outputDir, os.FileMode(0755)); err != nil {
return
Expand Down
7 changes: 3 additions & 4 deletions cmd/account_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import (
"os"
)


// NewAccountListCmd create a command to list all accounts
func NewAccountListCmd(args []string, accountCmd *accountCmd) (cmd *cobra.Command) {
accountListCmd := accountListCmd{accountCmd}
cmd = &cobra.Command{
Use: "list",
Use: "list",
Short: "List all jcli accounts",
RunE: accountListCmd.runE,
RunE: accountListCmd.runE,
}
return
}

func (c * accountListCmd) runE(cmd *cobra.Command, args []string) (err error) {
func (c *accountListCmd) runE(cmd *cobra.Command, args []string) (err error) {
var userHome string
if userHome, err = homedir.Dir(); err != nil {
return
Expand Down
11 changes: 5 additions & 6 deletions cmd/account_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import (
"os"
)


// NewAccountRemoveCmd create a command to remove a account
func NewAccountRemoveCmd(args []string, accountCmd *accountCmd) (cmd *cobra.Command) {
accountRemoveCmd := accountRemoveCmd{accountCmd}
cmd = &cobra.Command{
Use: "remove",
Short: "Remove a jcli account",
Use: "remove",
Short: "Remove a jcli account",
PreRunE: accountRemoveCmd.preRunE,
RunE: accountRemoveCmd.runE,
RunE: accountRemoveCmd.runE,
}
return
}

func (c * accountRemoveCmd) preRunE(cmd *cobra.Command, args []string) (err error) {
func (c *accountRemoveCmd) preRunE(cmd *cobra.Command, args []string) (err error) {
err = c.setName(cmd, args)
return
}

func (c * accountRemoveCmd) runE(cmd *cobra.Command, args []string) (err error) {
func (c *accountRemoveCmd) runE(cmd *cobra.Command, args []string) (err error) {
var userHome string
if userHome, err = homedir.Dir(); err != nil {
return
Expand Down
14 changes: 7 additions & 7 deletions cmd/account_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import (
func NewAccountSelectCmd(args []string, accountCmd *accountCmd) (cmd *cobra.Command) {
accountSelectCmd := accountSelectCmd{accountCmd}
cmd = &cobra.Command{
Use: "select",
Short: "Select a jcli config account as the default one",
Use: "select",
Short: "Select a jcli config account as the default one",
PreRunE: accountSelectCmd.PreRunE,
RunE: accountSelectCmd.Run,
RunE: accountSelectCmd.Run,
}
return
}

// PreRunE do the options checking
func (c* accountSelectCmd) PreRunE(cmd *cobra.Command, args []string) (err error) {
func (c *accountSelectCmd) PreRunE(cmd *cobra.Command, args []string) (err error) {
err = c.setName(cmd, args)
return
}

// Run is the main point of account select command
func (c* accountSelectCmd) Run(cmd *cobra.Command, args []string) (err error) {
func (c *accountSelectCmd) Run(cmd *cobra.Command, args []string) (err error) {
var configFile string
if configFile, err = c.getDefaultConfigPath(); err != nil {
return
Expand All @@ -38,7 +38,7 @@ func (c* accountSelectCmd) Run(cmd *cobra.Command, args []string) (err error) {
exist := true
if _, err = os.Stat(configFile); os.IsNotExist(err) {
exist = false
if err =ioutil.WriteFile(configFile, []byte{}, 0664); err != nil {
if err = ioutil.WriteFile(configFile, []byte{}, 0664); err != nil {
return
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *accountSelectCmd) getAccountConfig() (data []byte, err error) {
}

config := &accountConfig{}
if err = yaml.Unmarshal(data, &config); err != nil {
if err = yaml.Unmarshal(data, &config); err != nil {
return
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/account_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
// NewAccountUpdateCmd create a command to update the account
func NewAccountUpdateCmd(args []string, accountCmd *accountCmd) (cmd *cobra.Command) {
accountUpdateCmd := accountUpdateCmd{
Reset: true,
Reset: true,
accountCmd: accountCmd,
}
cmd = &cobra.Command{
Use: "update",
Short: "update the account",
Use: "update",
Short: "update the account",
PreRunE: accountUpdateCmd.preRunE,
RunE: accountUpdateCmd.Run,
RunE: accountUpdateCmd.Run,
}
return
}

func (c * accountUpdateCmd) preRunE(cmd *cobra.Command, args []string) (err error) {
func (c *accountUpdateCmd) preRunE(cmd *cobra.Command, args []string) (err error) {
err = c.setName(cmd, args)
return
}
Expand All @@ -45,7 +45,7 @@ func (c *accountUpdateCmd) Run(cmd *cobra.Command, args []string) (err error) {

if c.Reset {
if err = w.Reset(&git.ResetOptions{
Mode:git.HardReset,
Mode: git.HardReset,
}); err != nil {
return
}
Expand Down
18 changes: 13 additions & 5 deletions cmd/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (

type (
accountCmd struct {
URL string
Username string
Password string
URL string
Username string
Password string
SSHKeyFile string

Name string

Proxy string
ProxyAuth string

output io.Writer
}

Expand Down Expand Up @@ -46,11 +49,16 @@ type (

accountConfig struct {
jcli.Config `yaml:",inline"`
Account string `yaml:"account"`
Account string `yaml:"account"`
}

accountDocCmd struct {
RootCommand *cobra.Command
DocType string
DocType string
}
)

type (
versionCmd struct {
}
)
25 changes: 25 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/jenkins-zh/jenkins-cli/app"
"github.com/spf13/cobra"
)

// NewVersionCmd create a command to show the version
func NewVersionCmd() (cmd *cobra.Command) {
versionCmd := versionCmd{}
cmd = &cobra.Command{
Use: "version",
Short: "Show the version of this plugin",
RunE: versionCmd.RunE,
}
return
}

// RunE is the main entry point of this command
func (c *versionCmd) RunE(cmd *cobra.Command, args []string) (err error) {
cmd.Printf("Version: %s\n", app.GetVersion())
cmd.Printf("Last Commit: %s\n", app.GetCommit())
//cmd.Printf("Build Date: %s\n", app.GetDate())
return
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/ghodss/yaml v1.0.0
github.com/jenkins-zh/jenkins-cli v0.0.0-20200418150728-ec86d0fe6b13
github.com/jenkins-zh/jenkins-cli v0.0.0-20200420130654-286bf0a0a6a0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.0.0
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ github.com/jenkins-zh/jenkins-cli v0.0.0-20200418144021-0712b626da9f h1:sFFhgg4J
github.com/jenkins-zh/jenkins-cli v0.0.0-20200418144021-0712b626da9f/go.mod h1:Cci+suJ4WJiYsoguVqGvXvTd4UFHjjSXlNtrB1HLxWw=
github.com/jenkins-zh/jenkins-cli v0.0.0-20200418150728-ec86d0fe6b13 h1:E7Gd+TtJ1EAYjcHxzywVD7x5Lq5dN32fqcMWlAue/0c=
github.com/jenkins-zh/jenkins-cli v0.0.0-20200418150728-ec86d0fe6b13/go.mod h1:tNlyS+2iCz3+G3cteqHmT8evOL+TET8r56ud5qiwki0=
github.com/jenkins-zh/jenkins-cli v0.0.0-20200420130654-286bf0a0a6a0 h1:yMSwSJ+T9nPmTEBw7+IG3Nct8czoUlZBKrgiwxhIvmQ=
github.com/jenkins-zh/jenkins-cli v0.0.0-20200420130654-286bf0a0a6a0/go.mod h1:tNlyS+2iCz3+G3cteqHmT8evOL+TET8r56ud5qiwki0=
github.com/jenkins-zh/jenkins-cli v0.0.27 h1:eUAIO7knzb8s/T7HxufnS0+9Ew4jMrPu6mTLtO7//jc=
github.com/jenkins-zh/jenkins-cli v0.0.27/go.mod h1:dJsheSvotudZLOfqP2muGnYuLkzTL/tXfV46iqnTQ08=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
Loading

0 comments on commit 9f6df1c

Please sign in to comment.