Skip to content

Commit

Permalink
Merge pull request #114 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.1.4
  • Loading branch information
andyone authored Jun 22, 2024
2 parents e1122d9 + ab876ce commit f07fc1c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
- name: Download dependencies
run: make deps

- name: Build CLI
run: go build cmd/zabbix-jmx-get/zabbix-jmx-get.go

- name: Run tests
run: go test -covermode=count -coverprofile=cover.out

Expand Down
2 changes: 1 addition & 1 deletion beans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jmx

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
128 changes: 82 additions & 46 deletions cmd/zabbix-jmx-get/zabbix-jmx-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import (

"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/options"
"github.com/essentialkaos/ek/v12/terminal"
"github.com/essentialkaos/ek/v12/terminal/tty"
"github.com/essentialkaos/ek/v12/usage"
"github.com/essentialkaos/ek/v12/usage/completion/bash"
"github.com/essentialkaos/ek/v12/usage/completion/fish"
"github.com/essentialkaos/ek/v12/usage/completion/zsh"
"github.com/essentialkaos/ek/v12/usage/man"

jmx "github.com/essentialkaos/go-zabbix-jmx"
)
Expand All @@ -24,7 +30,7 @@ import (

const (
APP = "zabbix-jmx-get"
VER = "1.1.2"
VER = "1.2.0"
DESC = "Tool for fetching data from Zabbix Java Gateway"
)

Expand All @@ -40,6 +46,9 @@ const (
OPT_NO_COLOR = "nc:no-color"
OPT_HELP = "help"
OPT_VER = "v:version"

OPT_COMPLETION = "completion"
OPT_GENERATE_MAN = "generate-man"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -52,64 +61,80 @@ var optMap = options.Map{
OPT_USERNAME: {},
OPT_PASSWORD: {},
OPT_NO_COLOR: {Type: options.BOOL},
OPT_HELP: {Type: options.BOOL, Alias: "u:usage"},
OPT_VER: {Type: options.BOOL, Alias: "ver"},
OPT_HELP: {Type: options.BOOL},
OPT_VER: {Type: options.BOOL},

OPT_COMPLETION: {},
OPT_GENERATE_MAN: {Type: options.BOOL},
}

// ////////////////////////////////////////////////////////////////////////////////// //

func main() {
keys, errs := options.Parse(optMap)
preConfigureUI()

if len(errs) != 0 {
for _, err := range errs {
printError(err.Error())
}
args, errs := options.Parse(optMap)

if !errs.IsEmpty() {
terminal.Error("Options parsing errors:")
terminal.Error(errs.String())
os.Exit(1)
}

configureUI()

if options.GetB(OPT_VER) {
showAbout()
return
switch {
case options.Has(OPT_COMPLETION):
os.Exit(printCompletion())
case options.Has(OPT_GENERATE_MAN):
printMan()
os.Exit(0)
case options.GetB(OPT_VER):
genAbout().Print(options.GetS(OPT_VER))
os.Exit(0)
case options.GetB(OPT_HELP) || options.GetS(OPT_GATEWAY_HOST) == "true" || len(args) == 0:
genUsage().Print()
os.Exit(0)
}

if options.Has(OPT_GATEWAY_HOST) && options.GetS(OPT_GATEWAY_HOST) == "true" {
showUsage()
return
}
process(args.Strings())
}

if options.GetB(OPT_HELP) || len(keys) == 0 {
showUsage()
return
// preConfigureUI preconfigures UI based on information about user terminal
func preConfigureUI() {
if !tty.IsTTY() {
fmtc.DisableColors = true
}
}

process(keys)
// configureUI configures user interface
func configureUI() {
if options.GetB(OPT_NO_COLOR) {
fmtc.DisableColors = true
}
}

// checkOptions checks required options
func checkOptions() {
// validateOptions checks for required options
func validateOptions() {
optionsSet := true

if !options.Has(OPT_GATEWAY_HOST) {
printError("Option --%s is required", "host")
terminal.Error("Option %s is required", options.F(OPT_GATEWAY_HOST))
optionsSet = false
}

if !options.Has(OPT_GATEWAY_PORT) {
printError("Option --%s is required", "port")
terminal.Error("Option %s is required", options.F(OPT_GATEWAY_PORT))
optionsSet = false
}

if !options.Has(OPT_SERVER_HOST) {
printError("Option --%s is required", "server-host")
terminal.Error("Option %s is required", options.F(OPT_SERVER_HOST))
optionsSet = false
}

if !options.Has(OPT_SERVER_PORT) {
printError("Option --%s is required", "server-port")
terminal.Error("Option %s is required", options.F(OPT_SERVER_PORT))
optionsSet = false
}

Expand Down Expand Up @@ -158,7 +183,7 @@ func renderBeansData(data string) {
beans, err := jmx.ParseBeans(data)

if err != nil {
printError(err.Error())
terminal.Error(err)
return
}

Expand Down Expand Up @@ -196,28 +221,39 @@ func makeRequest(keys []string) *jmx.Request {
return r
}

// configureUI configure UI on start
func configureUI() {
if options.GetB(OPT_NO_COLOR) {
fmtc.DisableColors = true
}
}

// printError prints error message to console
func printError(f string, a ...interface{}) {
fmtc.Fprintf(os.Stderr, "{r}"+f+"{!}\n", a...)
}

// printErrorAndExit print error message and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
printError(f, a...)
terminal.Error(f, a...)
os.Exit(1)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// showUsage prints usage info
func showUsage() {
// printCompletion prints completion for given shell
func printCompletion() int {
info := genUsage()

switch options.GetS(OPT_COMPLETION) {
case "bash":
fmt.Printf(bash.Generate(info, APP))
case "fish":
fmt.Printf(fish.Generate(info, APP))
case "zsh":
fmt.Printf(zsh.Generate(info, optMap, APP))
default:
return 1
}

return 0
}

// printMan prints man page
func printMan() {
fmt.Println(man.Generate(genUsage(), genAbout()))
}

// genUsage generates usage info
func genUsage() *usage.Info {
info := usage.NewInfo("", "key…")

info.AddOption(OPT_GATEWAY_HOST, "Java gateway host", "host")
Expand All @@ -240,19 +276,19 @@ func showUsage() {
"Request discovery info",
)

info.Render()
return info
}

// showAbout shows info about version
func showAbout() {
// genAbout generates info about version
func genAbout() *usage.About {
about := &usage.About{
App: APP,
Version: VER,
Desc: DESC,
Year: 2006,
Year: 2009,
Owner: "ESSENTIAL KAOS",
License: "Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>",
}

about.Render()
return about
}
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jmx

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jmx

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion zabbix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jmx

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion zabbix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jmx

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down

0 comments on commit f07fc1c

Please sign in to comment.