[minor] add debug/verbose output to client

This commit is contained in:
Julien Vehent 2015-07-10 11:58:18 -04:00
Родитель 50da7d34a7
Коммит 9af4353161
2 изменённых файлов: 35 добавлений и 0 удалений

Просмотреть файл

@ -38,6 +38,7 @@ type Client struct {
Token string
Conf Configuration
Version string
debug bool
}
// Configuration stores the live configuration and global parameters of a client
@ -241,6 +242,10 @@ func (cli Client) Do(r *http.Request) (resp *http.Response, err error) {
}
}
r.Header.Set("X-PGPAUTHORIZATION", cli.Token)
if cli.debug {
fmt.Printf("debug: %s %s %s\ndebug: User-Agent: %s\ndebug: X-PGPAUTHORIZATION: %s\n",
r.Method, r.URL.String(), r.Proto, r.UserAgent(), r.Header.Get("X-PGPAUTHORIZATION"))
}
// execute the request
resp, err = cli.API.Do(r)
if err != nil {
@ -256,6 +261,10 @@ func (cli Client) Do(r *http.Request) (resp *http.Response, err error) {
panic(err)
}
r.Header.Set("X-PGPAUTHORIZATION", cli.Token)
if cli.debug {
fmt.Printf("debug: %s %s %s\ndebug: User-Agent: %s\ndebug: X-PGPAUTHORIZATION: %s\n",
r.Method, r.URL.String(), r.Proto, r.UserAgent(), r.Header.Get("X-PGPAUTHORIZATION"))
}
// execute the request
resp, err = cli.API.Do(r)
if err != nil {
@ -290,10 +299,17 @@ func (cli Client) GetAPIResource(target string) (resource *cljs.Resource, err er
panic(err)
}
if len(body) > 1 {
if cli.debug {
fmt.Printf("debug: RESPONSE BODY:\ndebug: %s\n", body)
}
err = json.Unmarshal(body, &resource)
if err != nil {
panic(err)
}
} else {
if cli.debug {
fmt.Printf("debug: RESPONSE BODY: EMPTY\n")
}
}
}
if resp.StatusCode != 200 {
@ -893,3 +909,15 @@ func PrintCommandResults(cmd mig.Command, onlyFound, showAgent bool) (err error)
}
return
}
// EnableDebug() prints debug messages to stdout
func (cli *Client) EnableDebug() {
cli.debug = true
return
}
// DisableDebug() disables the printing of debug messages to stdout
func (cli *Client) DisableDebug() {
cli.debug = false
return
}

Просмотреть файл

@ -43,6 +43,7 @@ usage: %s <module> <global options> <module parameters>
* agents named *mysql*: -t "name like '%%mysql%%'"
* proxied linux agents: -t "queueloc LIKE 'linux.%%' AND environment->>'isproxied' = 'true'"
* agents operated by IT: -t "tags#>>'{operator}'='IT'"
-v verbose output, includes debug information and raw queries
Progress information is sent to stderr, silence it with "2>/dev/null".
Results are sent to stdout, redirect them with "1>/path/to/file".
@ -70,6 +71,7 @@ func main() {
op mig.Operation
a mig.Action
migrc, show, render, target, expiration, afile string
verbose bool
modargs []string
run interface{}
)
@ -87,6 +89,7 @@ func main() {
fs.StringVar(&target, "t", fmt.Sprintf("status='%s' AND mode='daemon'", mig.AgtStatusOnline), "action target")
fs.StringVar(&expiration, "e", "300s", "expiration")
fs.StringVar(&afile, "i", "/path/to/file", "Load action from file")
fs.BoolVar(&verbose, "v", false, "Enable verbose output")
// if first argument is missing, or is help, print help
// otherwise, pass the remainder of the arguments to the module for parsing
@ -182,6 +185,10 @@ readytolaunch:
panic(err)
}
if verbose {
cli.EnableDebug()
}
// set the validity 60 second in the past to deal with clock skew
a.ValidFrom = time.Now().Add(-60 * time.Second).UTC()
period, err := time.ParseDuration(expiration)