Skip to content

Commit 1405e9b

Browse files
authored
Initial commit
0 parents  commit 1405e9b

File tree

16 files changed

+934
-0
lines changed

16 files changed

+934
-0
lines changed

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
.idea
8+
.vscode
9+
.token
10+
11+
logs
12+
eryajfctl
13+
14+
# Test binary, built with `go test -c`
15+
*.test
16+
17+
# Output of the go coverage tool, specifically when used with LiteIDE
18+
*.out
19+
20+
# Dependency directories (remove the comment below to include it)
21+
# vendor/
22+
tmp

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM golang:1.18.10-alpine3.16 AS builder
2+
3+
ENV GOPROXY https://goproxy.io
4+
5+
RUN mkdir /app
6+
ADD . /app/
7+
WORKDIR /app
8+
RUN go build -o eryajfctl .
9+
10+
FROM alpine:3.16
11+
12+
ARG TZ="Asia/Shanghai"
13+
14+
ENV TZ ${TZ}
15+
16+
RUN mkdir /app && apk upgrade \
17+
&& apk add bash tzdata \
18+
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
19+
&& echo ${TZ} > /etc/timezone
20+
21+
WORKDIR /app
22+
COPY --from=builder /app/ .
23+
RUN chmod +x eryajfctl && cp config.example.yml config.yml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 二丫讲梵
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
default: build
2+
3+
build:
4+
go build -o eryajfctl main.go
5+
6+
build-linux:
7+
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o eryajfctl main.go
8+
9+
lint:
10+
env GOGC=25 golangci-lint run --fix -j 8 -v ./...

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<div align="center">
2+
<h1>eryajfctl</h1>
3+
4+
[![Auth](https://img.shields.io/badge/Auth-eryajf-ff69b4)](https://github.com/eryajf)
5+
[![GitHub Pull Requests](https://img.shields.io/github/stars/eryajf/eryajfctl)](https://github.com/eryajf/eryajfctl/stargazers)
6+
[![HitCount](https://views.whatilearened.today/views/github/eryajf/eryajfctl.svg)](https://github.com/eryajf/eryajfctl)
7+
[![GitHub license](https://img.shields.io/github/license/eryajf/eryajfctl)](https://github.com/eryajf/eryajfctl/blob/main/LICENSE)
8+
[![](https://img.shields.io/badge/Awesome-MyStarList-c780fa?logo=Awesome-Lists)](https://github.com/eryajf/awesome-stars-eryajf#readme)
9+
10+
<p> 🌉 基于Cobra库快速开发类似kubectl一样的命令行工具框架 🌉</p>
11+
12+
<img src="https://camo.githubusercontent.com/82291b0fe831bfc6781e07fc5090cbd0a8b912bb8b8d4fec0696c881834f81ac/68747470733a2f2f70726f626f742e6d656469612f394575424971676170492e676966" width="800" height="3">
13+
14+
</div>
15+
16+
运维也可以如此优雅!快用这个框架打造一个专属于你的工具箱吧!
17+
18+
通过这个框架,你可以快速上手,直接构建你想要的运维工具,而不必再考虑配置,框架设计等内容。
19+
20+
## 如何使用
21+
22+
先拷贝配置文件:
23+
24+
```sh
25+
cp config.example.yml config.yml
26+
```
27+
28+
执行如下指令,运行示例参数:
29+
30+
```sh
31+
$ go run main.go ex getconfig -w "hello, This is eryajfctl"
32+
通过配置文件获取到的用户名: eryajf
33+
通过配置文件获取到的密码: 123456
34+
通过命令行获取到的内容是: hello, This is eryajfctl
35+
```
36+
37+
> 其中ex为一级参数,getconfig为二级参数,大多数场景下,分两个层级就够用了,你可以把一级参数当做归类,比如 jenkins, gitlab,二级参数当做功能参数,再往后的参数则是该二级参数所需要的运行时参数。
38+
39+
也可以编译成二进制,然后通过如下方式查看帮助信息:
40+
41+
```
42+
# 编译
43+
$ make build
44+
45+
#运行测试
46+
$ ./eryajfctl ex getconfig -h
47+
通过命令行获取配置信息
48+
49+
Usage:
50+
eryajfctl ex getconfig [flags]
51+
52+
Flags:
53+
-h, --help help for getconfig
54+
-w, --word string 测试参数 (default "你好,这是测试")
55+
```
56+
57+
## 开始开发
58+
59+
你可以直接参考ex参数的流程,开发新的参数,从而满足实际使用需求。
60+
61+
如果你的配置文件中有敏感数据,可以考虑结合go-bindata来使用,通过执行:
62+
63+
```
64+
go-bindata -o=./public/bindata_config.go -pkg=public config.yml
65+
```
66+
67+
然后更改 [public/config.go](https://github.com/eryajf/eryajfctl/blob/4cd30714062e5b65746bdb5f100f19bfe38ed52e/public/config.go#L28) 中的配置信息读取方式,接着进入开发即可。
68+
69+
如果后续本地的config.yml配置有更新,则再次执行上边的命令,将配置文件注入到 `bindata_config.go` 即可。
70+
71+
这样做的一个好处是,你的二进制放到服务器等地运行的时候,既不需要添加config.yml文件,也能防止文件中的敏感信息暴漏出去。
72+
73+
## 感谢开源
74+
75+
此框架建立在如下几个优秀的考原项目之上:
76+
77+
- [gopkg.in/yaml.v3 v3.0.1](https://github.com/go-yaml/yaml)
78+
- [github.com/spf13/cobra v1.2.1](https://github.com/spf13/cobra)
79+
80+
## 其他参考
81+
82+
如果你想熟悉了解此框架的详细用法,还可以参考我的如下两篇文章:
83+
84+
- [使用go-bindata将文件编译进二进制](https://wiki.eryajf.net/pages/2bf6c3/)
85+
- [利用cobra库快速开发类似kubectl一样的命令行工具](https://wiki.eryajf.net/pages/5c4163/)

api/ex/cmd.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ex
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/eryajf/eryajfctl/public"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var GetConfigCmd = &cobra.Command{
11+
Use: "getconfig",
12+
Short: "获取配置信息,这是一个示例",
13+
Long: `通过命令行获取配置信息`,
14+
Run: func(cmd *cobra.Command, args []string) {
15+
word, _ := cmd.Flags().GetString("word")
16+
fmt.Println("通过配置文件获取到的用户名:", public.Config.UserName)
17+
fmt.Println("通过配置文件获取到的密码:", public.Config.PassWord)
18+
fmt.Println("通过命令行获取到的内容是:", word)
19+
},
20+
}

cmd/ex.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"github.com/eryajf/eryajfctl/api/ex"
20+
"github.com/spf13/cobra"
21+
)
22+
23+
// exCmd represents the jenkins command
24+
var exCmd = &cobra.Command{
25+
Use: "ex",
26+
Long: `这是一个示例,你可以参考帮助信息使用`,
27+
}
28+
29+
func init() {
30+
rootCmd.AddCommand(exCmd)
31+
// 获取配置文件
32+
exCmd.AddCommand(ex.GetConfigCmd)
33+
cset := ex.GetConfigCmd.Flags()
34+
cset.StringP("word", "w", "你好,这是测试", "测试参数")
35+
_ = ex.GetConfigCmd.MarkFlagRequired("word")
36+
}

cmd/root.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright © 2021 eryajf
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"github.com/spf13/cobra"
20+
)
21+
22+
// var cfgFile string
23+
24+
// rootCmd represents the base command when called without any subcommands
25+
var rootCmd = &cobra.Command{
26+
Use: "eryajfctl",
27+
Short: "eryajf goscript",
28+
Long: `利用cobra制作运维日常工具箱的框架。`,
29+
// Uncomment the following line if your bare application
30+
// has an action associated with it:
31+
// Run: func(cmd *cobra.Command, args []string) { },
32+
}
33+
34+
// Execute adds all child commands to the root command and sets flags appropriately.
35+
// This is called by main.main(). It only needs to happen once to the rootCmd.
36+
func Execute() {
37+
cobra.CheckErr(rootCmd.Execute())
38+
}
39+
40+
func init() {
41+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
42+
}

config.example.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
username: "eryajf"
2+
password: "123456"

config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
username: "eryajf"
2+
password: "123456"

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/eryajf/eryajfctl
2+
3+
go 1.18
4+
5+
require (
6+
github.com/spf13/cobra v1.2.1
7+
gopkg.in/yaml.v3 v3.0.1
8+
)
9+
10+
require (
11+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
12+
github.com/spf13/pflag v1.0.5 // indirect
13+
)

0 commit comments

Comments
 (0)