Skip to content

Commit

Permalink
Merge pull request #8 from superseb/parse_response_statuscode
Browse files Browse the repository at this point in the history
Use response statuscode when setting loglevel
  • Loading branch information
superseb authored Mar 4, 2020
2 parents 6fd6a3c + 19f045a commit 6629ad0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand All @@ -30,7 +32,7 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "set",
Usage: "Set loglevel (info, debug, error)",
Usage: "Set loglevel",
},
cli.StringFlag{
Name: "socket-location",
Expand Down Expand Up @@ -79,15 +81,19 @@ func run(c *cli.Context) error {
}

func (client *Client) setLogLevel(level string) error {
if level != "info" && level != "debug" && level != "error" {
return fmt.Errorf("invalid log level specified (%s)", level)
}
response, err := client.httpc.Post("http://unix/v1/loglevel",
"application/x-www-form-urlencoded",
strings.NewReader(fmt.Sprintf("level=%v", level)))
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
return errors.New(strings.TrimRight(string(body), "\n"))
}
io.Copy(os.Stdout, response.Body)
return nil
}
Expand Down

0 comments on commit 6629ad0

Please sign in to comment.