Skip to content

Commit 3819185

Browse files
authored
Merge branch 'master' into dependabot-go_modules-github.com-spf13-cobra-1.1.3
2 parents 14e600e + 44d1bbf commit 3819185

File tree

13 files changed

+541
-40
lines changed

13 files changed

+541
-40
lines changed

.github/settings.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repository:
2-
name: github-go
3-
description: This is a template project
2+
name: jcli-ks-plugin
3+
description: This is a KubeSphere plugin for Jenkins CLI
44
homepage: https://github.com/linuxsuren
55
private: false
66
has_issues: true

.github/workflows/backup.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
uses: jenkins-zh/[email protected]
1616
env:
1717
GIT_DEPLOY_KEY: ${{ secrets.GIT_DEPLOY_KEY }}
18-
TARGET_GIT: "[email protected]:linuxsuren/github-go.git"
18+
TARGET_GIT: "[email protected]:linuxsuren/jcli-ks-plugin.git"

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
version: latest
2424
args: release --rm-dist
2525
env:
26-
GITHUB_TOKEN: ${{ secrets.GH_PUBLISH_SECRETS }}
26+
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}

.goreleaser.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Official documentation at http://goreleaser.com
2-
project_name: github-go
2+
project_name: jcli-ks-plugin
33
builds:
44
- env:
55
- CGO_ENABLED=0
6-
binary: github-go
6+
binary: jcli-ks-plugin
77
goarch:
88
- amd64
99
- arm64
@@ -46,28 +46,28 @@ changelog:
4646
- '^test:'
4747
brews:
4848
-
49-
name: github-go
49+
name: jcli-ks-plugin
5050
tap:
5151
owner: linuxsuren
5252
name: homebrew-linuxsuren
5353
folder: Formula
54-
homepage: "https://github.com/linuxsuren/github-go"
54+
homepage: "https://github.com/linuxsuren/jcli-ks-plugin"
5555
description: cgit is a tiny tool for Chinese developers.
5656
dependencies:
5757
- name: vim
5858
type: optional
5959
- name: bash-completion
6060
type: optional
6161
test: |
62-
version_output = shell_output("#{bin}/github-go version")
62+
version_output = shell_output("#{bin}/jcli-ks-plugin version")
6363
assert_match version.to_s, version_output
6464
install: |
6565
bin.install name
6666
6767
prefix.install_metafiles
6868
nfpms:
6969
- file_name_template: "{{ .Binary }}-{{.Os}}-{{.Arch}}"
70-
homepage: https://github.com/linuxsuren/github-go
70+
homepage: https://github.com/linuxsuren/jcli-ks-plugin
7171
description: cgit is a tiny tool for Chinese developers.
7272
maintainer: rick <[email protected]>
7373
license: MIT

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
NAME := jcli-ks-plugin
2+
CGO_ENABLED = 0
3+
BUILD_GOOS=$(shell go env GOOS)
4+
GO := go
5+
BUILD_TARGET = build
6+
COMMIT := $(shell git rev-parse --short HEAD)
7+
VERSION := dev-$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
8+
BUILDFLAGS = -ldflags "-X github.com/linuxsuren/cobra-extension/version.version=$(VERSION) \
9+
-X github.com/linuxsuren/cobra-extension/version.commit=$(COMMIT) \
10+
-X github.com/linuxsuren/cobra-extension/version.date=$(shell date +'%Y-%m-%d')"
11+
COVERED_MAIN_SRC_FILE=./main
12+
PATH := $(PATH):$(PWD)/bin
13+
114
build: fmt
2-
CGO_ENABLE=0 go build -ldflags "-w -s" -o bin/github-go
15+
GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED) GOOS=$(BUILD_GOOS) GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) \
16+
-o bin/$(BUILD_GOOS)/$(NAME) $(MAIN_SRC_FILE)
17+
chmod +x bin/$(BUILD_GOOS)/$(NAME)
318

419
run:
520
go run main.go
@@ -8,4 +23,5 @@ fmt:
823
go fmt ./...
924

1025
copy: build
11-
sudo cp bin/github-go /usr/local/bin/github-go
26+
cp bin/$(BUILD_GOOS)/$(NAME) ~/.jcli-plugins/plugins-repo/$(NAME)
27+

README.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
[![](https://goreportcard.com/badge/linuxsuren/github-go)](https://goreportcard.com/report/linuxsuren/github-go)
2-
[![](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/linuxsuren/github-go)
3-
[![Contributors](https://img.shields.io/github/contributors/linuxsuren/github-go.svg)](https://github.com/linuxsuren/github-go/graphs/contributors)
4-
[![GitHub release](https://img.shields.io/github/release/linuxsuren/github-go.svg?label=release)](https://github.com/linuxsuren/github-go/releases/latest)
5-
![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/github-go/total)
6-
7-
# Get started
8-
9-
You can use this template repository via the following command:
10-
11-
`gh repo create --public -p linuxsuren/github-go -y <project-name>`
12-
13-
# Clean
14-
15-
Please run the following command, after you created your project base on this template repository:
16-
17-
`./hack/init.sh <project-name>`
1+
[![](https://goreportcard.com/badge/linuxsuren/jcli-ks-plugin)](https://goreportcard.com/report/linuxsuren/github-go)
2+
[![](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/linuxsuren/jcli-ks-plugin)
3+
[![Contributors](https://img.shields.io/github/contributors/linuxsuren/jcli-ks-plugin.svg)](https://github.com/linuxsuren/github-go/graphs/contributors)
4+
[![GitHub release](https://img.shields.io/github/release/linuxsuren/jcli-ks-plugin.svg?label=release)](https://github.com/linuxsuren/github-go/releases/latest)
5+
![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/jcli-ks-plugin/total)

cmd/config/config_cmd.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package config
2+
3+
import "github.com/spf13/cobra"
4+
5+
func NewConfigCmd() (cmd *cobra.Command) {
6+
cmd = &cobra.Command{
7+
Use: "config",
8+
Short: "Manage the config item related to KubeSphere Jenkins",
9+
}
10+
11+
cmd.AddCommand(newUpdateCmd())
12+
return
13+
}

cmd/config/exec.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package config
2+
3+
import (
4+
"io"
5+
"os"
6+
"os/exec"
7+
"sync"
8+
)
9+
10+
func execCommand(name string, arg ...string) (err error) {
11+
command := exec.Command(name, arg...)
12+
13+
//var stdout []byte
14+
//var errStdout error
15+
stdoutIn, _ := command.StdoutPipe()
16+
stderrIn, _ := command.StderrPipe()
17+
err = command.Start()
18+
if err != nil {
19+
return err
20+
}
21+
22+
// cmd.Wait() should be called only after we finish reading
23+
// from stdoutIn and stderrIn.
24+
// wg ensures that we finish
25+
var wg sync.WaitGroup
26+
wg.Add(1)
27+
go func() {
28+
_, _ = copyAndCapture(os.Stdout, stdoutIn)
29+
wg.Done()
30+
}()
31+
32+
_, _ = copyAndCapture(os.Stderr, stderrIn)
33+
34+
wg.Wait()
35+
36+
err = command.Wait()
37+
return
38+
}
39+
40+
func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
41+
var out []byte
42+
buf := make([]byte, 1024, 1024)
43+
for {
44+
n, err := r.Read(buf[:])
45+
if n > 0 {
46+
d := buf[:n]
47+
out = append(out, d...)
48+
_, err := w.Write(d)
49+
if err != nil {
50+
return out, err
51+
}
52+
}
53+
if err != nil {
54+
// Read returns io.EOF at the end of file, which is not an error for us
55+
if err == io.EOF {
56+
err = nil
57+
}
58+
return out, err
59+
}
60+
}
61+
}

cmd/config/update.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/ghodss/yaml"
7+
kstypes "github.com/linuxsuren/ks/kubectl-plugin/types"
8+
"github.com/spf13/cobra"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11+
"k8s.io/client-go/dynamic"
12+
"k8s.io/client-go/rest"
13+
"k8s.io/client-go/tools/clientcmd"
14+
"os"
15+
"path/filepath"
16+
)
17+
18+
func newUpdateCmd() (cmd *cobra.Command) {
19+
opt := &updateOption{}
20+
21+
cmd = &cobra.Command{
22+
Use: "update",
23+
Aliases: []string{"up"},
24+
Short: "Update the config item",
25+
PreRunE: opt.preRunE,
26+
RunE: opt.runE,
27+
}
28+
return
29+
}
30+
31+
type updateOption struct {
32+
name string
33+
token string
34+
}
35+
36+
// kubeSphereConfig is the config object of KubeSphere
37+
// currently it's partial
38+
type kubeSphereConfig struct {
39+
DevOps struct {
40+
Password string `yaml:"password"`
41+
} `yaml:"devops"`
42+
}
43+
44+
func (o *updateOption) preRunE(_ *cobra.Command, args []string) (err error) {
45+
if len(args) > 0 {
46+
o.name = args[0]
47+
}
48+
49+
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
50+
var config *rest.Config
51+
var client dynamic.Interface
52+
53+
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil {
54+
fmt.Println(err)
55+
} else {
56+
if client, err = dynamic.NewForConfig(config); err != nil {
57+
return
58+
}
59+
}
60+
61+
ctx := context.TODO()
62+
var rawConfigMap *unstructured.Unstructured
63+
if rawConfigMap, err = client.Resource(kstypes.GetConfigMapSchema()).Namespace("kubesphere-system").
64+
Get(ctx, "kubesphere-config", metav1.GetOptions{}); err == nil {
65+
data := rawConfigMap.Object["data"]
66+
dataMap := data.(map[string]interface{})
67+
configStr := dataMap["kubesphere.yaml"].(string)
68+
69+
ksCfg := kubeSphereConfig{}
70+
if err = yaml.Unmarshal([]byte(configStr), &ksCfg); err == nil {
71+
o.token = ksCfg.DevOps.Password
72+
} else {
73+
err = fmt.Errorf("unable to parse KubeSphere config from configmap, error: %v", err)
74+
}
75+
}
76+
77+
if o.token == "" {
78+
err = fmt.Errorf("unable to get Jenkins token from KubeSphere, please check if it was enabled")
79+
}
80+
return
81+
}
82+
83+
func (o *updateOption) runE(_ *cobra.Command, args []string) (err error) {
84+
err = execCommand("jcli", "config", "update", "--token", o.token, o.name)
85+
return
86+
}

cmd/root.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
import (
4+
"github.com/linuxsuren/jcli-ks-plugin/cmd/config"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
// NewKSPlugin returns the command of jcli ks
9+
func NewKSPlugin() (cmd *cobra.Command) {
10+
cmd = &cobra.Command{
11+
Use: "jcli ks",
12+
Short: "jcli plugin for KubeSphere",
13+
}
14+
15+
cmd.AddCommand(config.NewConfigCmd())
16+
return
17+
}

0 commit comments

Comments
 (0)