forked from vbatoufflet/go-livestatus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename Livestatus struct to Client * Use net.Dialer instead of function in NewClientWithDialer * Handle Query/Command separately to permit reuse over multiple clients * Rewrite Nagios commands generation script (code now respects "go fmt") * Fix some linting issues
- Loading branch information
1 parent
77cc4b6
commit e3de2df
Showing
18 changed files
with
3,450 additions
and
2,625 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package livestatus | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
const bufferSize = 1024 | ||
|
||
// Client represents a Livestatus client instance. | ||
type Client struct { | ||
network string | ||
address string | ||
dialer *net.Dialer | ||
conn net.Conn | ||
} | ||
|
||
// NewClient creates a new Livestatus client instance. | ||
func NewClient(network, address string) *Client { | ||
return NewClientWithDialer(network, address, new(net.Dialer)) | ||
} | ||
|
||
// NewClientWithDialer creates a new Livestatus client instance using a provided network dialer. | ||
func NewClientWithDialer(network, address string, dialer *net.Dialer) *Client { | ||
return &Client{ | ||
network: network, | ||
address: address, | ||
dialer: dialer, | ||
} | ||
} | ||
|
||
// Close closes any remaining connection. | ||
func (c *Client) Close() { | ||
if c.conn != nil { | ||
c.conn.Close() | ||
c.conn = nil | ||
} | ||
} | ||
|
||
// Exec executes a given Livestatus query. | ||
func (c *Client) Exec(r Request) (*Response, error) { | ||
var err error | ||
|
||
// Initialize connection if none available | ||
if c.conn == nil { | ||
c.conn, err = c.dialer.Dial(c.network, c.address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !r.keepAlive() { | ||
defer c.Close() | ||
} | ||
} | ||
|
||
return r.handle(c.conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package livestatus | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_Command(t *testing.T) { | ||
expected := fmt.Sprintf("COMMAND [%d] command1\n", time.Now().Unix()) | ||
|
||
c := NewCommand("command1") | ||
|
||
result := c.String() | ||
if result != expected { | ||
t.Logf("\nExpected %q\nbut got %q\n", expected, result) | ||
t.Fail() | ||
} | ||
} | ||
|
||
func Test_CommandWithInlineArgs(t *testing.T) { | ||
expected := fmt.Sprintf("COMMAND [%d] command1;arg1;arg2\n", time.Now().Unix()) | ||
|
||
c := NewCommand("command1", "arg1", "arg2") | ||
|
||
result := c.String() | ||
if result != expected { | ||
t.Logf("\nExpected %q\nbut got %q\n", expected, result) | ||
t.Fail() | ||
} | ||
} | ||
|
||
func Test_CommandWithArgs(t *testing.T) { | ||
expected := fmt.Sprintf("COMMAND [%d] command1;arg1;arg2\n", time.Now().Unix()) | ||
|
||
c := NewCommand("command1") | ||
c.Arg("arg1") | ||
c.Arg("arg2") | ||
|
||
result := c.String() | ||
if result != expected { | ||
t.Logf("\nExpected %q\nbut got %q\n", expected, result) | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.