Skip to content

Commit

Permalink
Merge pull request #97 from NETWAYS/chore/update-docs
Browse files Browse the repository at this point in the history
Add missing func docs
  • Loading branch information
martialblog authored Jul 17, 2023
2 parents 0396599 + 6535a96 commit a49af59
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
33 changes: 24 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,31 @@ import (
flag "github.com/spf13/pflag"
)

// Config represents a configuration for a monitoring plugin's CLI
type Config struct {
Name string
Readme string
Version string
Timeout int
Verbose bool
Debug bool
PrintVersion bool
DefaultFlags bool
// Name of the monitoring plugin
Name string
// README represents the help text for the CLI usage
Readme string
// Output for the --version flag
Version string
// Default for the --timeout flag
Timeout int
// Default for the --verbose flag
Verbose bool
// Default for the --debug flag
Debug bool
// Enable predefined --version output
PrintVersion bool
// Enable predefined default flags for the monitoring plugin
DefaultFlags bool
// Enable predefined default functions (e.g. Timeout handler) for the monitoring plugin
DefaultHelper bool
FlagSet *flag.FlagSet
// Additional CLI flags for the monitoring plugin
FlagSet *flag.FlagSet
}

// NewConfig returns a Config struct with some defaults
func NewConfig() *Config {
c := &Config{}
c.Name = path.Base(os.Args[0])
Expand Down Expand Up @@ -50,10 +62,12 @@ func NewConfig() *Config {
return c
}

// ParseArguments parses the command line arguments given by os.Args
func (c *Config) ParseArguments() {
c.ParseArray(os.Args[1:])
}

// ParseArray parses a list of command line arguments
func (c *Config) ParseArray(arguments []string) {
if c.DefaultFlags {
c.addDefaultFlags()
Expand All @@ -74,6 +88,7 @@ func (c *Config) ParseArray(arguments []string) {
}
}

// addDefaultFlags adds various default flags to the monitoring plugin
func (c *Config) addDefaultFlags() {
c.FlagSet.IntVarP(&c.Timeout, "timeout", "t", c.Timeout, "Abort the check after n seconds")
c.FlagSet.BoolVarP(&c.Debug, "debug", "d", false, "Enable debug mode")
Expand Down
1 change: 1 addition & 0 deletions convert/bytes_si.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (b BytesSI) HumanReadable() string {
return s + unit
}

// String returns a text representation of the current value.
func (b BytesSI) String() string {
return b.HumanReadable()
}
Expand Down
3 changes: 3 additions & 0 deletions exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func ExitRaw(rc int, output ...string) {
BaseExit(rc)
}

// BaseExit exits the process with a given return code.
//
// Can be controlled with the global AllowExit
func BaseExit(rc int) {
if AllowExit {
os.Exit(rc)
Expand Down
2 changes: 1 addition & 1 deletion perfdata/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (l PerfdataList) String() string {
return strings.Trim(out.String(), " ")
}

// Add a Perfdata to the list
// Add adds a Perfdata pointer to the list
func (l *PerfdataList) Add(p *Perfdata) {
*l = append(*l, p)
}
25 changes: 19 additions & 6 deletions result/overall.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"github.com/NETWAYS/go-check/perfdata"
)

// So, this is the idea:
// A check plugin has a single Overall (singleton)
// Each partial thing which is tested, gets its own subcheck
// Overall is a singleton for a monitoring pluging that has several partial results (or sub-results)
//
// Design decisions: A check plugin has a single Overall (singleton),
// each partial thing which is tested, gets its own subcheck.
//
// The results of these may be relevant to the overall status in the end
// or not, e.g. if a plugin tries two different methods for something and
// one suffices, but one fails, the whole check might be OK and only the subcheck
Expand All @@ -28,6 +30,7 @@ type Overall struct {
PartialResults []PartialResult
}

// PartialResult represents a sub-result for an Overall struct
type PartialResult struct {
state int // Result state, either set explicitely or derived from partialResults
Output string
Expand All @@ -38,11 +41,13 @@ type PartialResult struct {
PartialResults []PartialResult
}

// String returns the status and output of the PartialResult
func (s *PartialResult) String() string {
return fmt.Sprintf("[%s] %s", check.StatusText(s.GetStatus()), s.Output)
}

// Add State explicitely
// Add adds a return state explicitely
//
// Hint: This will set stateSetExplicitely to true
func (o *Overall) Add(state int, output string) {
switch state {
Expand All @@ -62,14 +67,17 @@ func (o *Overall) Add(state int, output string) {
o.Outputs = append(o.Outputs, fmt.Sprintf("[%s] %s", check.StatusText(state), output))
}

// AddSubcheck adds a PartialResult to the Overall
func (o *Overall) AddSubcheck(subcheck PartialResult) {
o.PartialResults = append(o.PartialResults, subcheck)
}

// AddSubcheck adds a PartialResult to the PartialResult
func (o *PartialResult) AddSubcheck(subcheck PartialResult) {
o.PartialResults = append(o.PartialResults, subcheck)
}

// GetStatus returns the current state (ok, warning, critical, unknown) of the Overall
func (o *Overall) GetStatus() int {
if o.stateSetExplicitely {
if o.criticals > 0 {
Expand Down Expand Up @@ -129,6 +137,7 @@ func (o *Overall) GetStatus() int {
}
}

// GetSummary returns a text representation of the current state of the Overall
// nolint: funlen
func (o *Overall) GetSummary() string {
if o.Summary != "" {
Expand Down Expand Up @@ -210,6 +219,7 @@ func (o *Overall) GetSummary() string {
return o.Summary
}

// GetOutput returns a text representation of the current outputs of the Overall
func (o *Overall) GetOutput() string {
var output strings.Builder

Expand Down Expand Up @@ -238,7 +248,7 @@ func (o *Overall) GetOutput() string {
return output.String()
}

// Returns all subsequent perfdata as a concatenated string
// getPerfdata returns all subsequent perfdata as a concatenated string
func (s *PartialResult) getPerfdata() string {
var output strings.Builder

Expand All @@ -255,7 +265,7 @@ func (s *PartialResult) getPerfdata() string {
return strings.TrimSpace(output.String())
}

// Generates indented output for all subsequent PartialResults
// getOutput generates indented output for all subsequent PartialResults
func (s *PartialResult) getOutput(indent_level int) string {
var output strings.Builder

Expand All @@ -271,6 +281,7 @@ func (s *PartialResult) getOutput(indent_level int) string {
return output.String()
}

// SetDefaultState sets a new default state for a PartialResult
func (s *PartialResult) SetDefaultState(state int) error {
if state < check.OK || state > check.Unknown {
return errors.New("Default State is not a valid result state. Got " + fmt.Sprint(state) + " which is not valid")
Expand All @@ -282,6 +293,7 @@ func (s *PartialResult) SetDefaultState(state int) error {
return nil
}

// SetState sets a state for a PartialResult
func (s *PartialResult) SetState(state int) error {
if state < check.OK || state > check.Unknown {
return errors.New("Default State is not a valid result state. Got " + fmt.Sprint(state) + " which is not valid")
Expand All @@ -293,6 +305,7 @@ func (s *PartialResult) SetState(state int) error {
return nil
}

// GetStatus returns the current state (ok, warning, critical, unknown) of the PartialResult
// nolint: unused
func (s *PartialResult) GetStatus() int {
if s.stateSetExplicitely {
Expand Down

0 comments on commit a49af59

Please sign in to comment.