Skip to content

Commit

Permalink
Merge pull request #200 from gustavosbarreto/agent-sdk-go
Browse files Browse the repository at this point in the history
updatehub-ctl: refactor using agent-sdk-go api
  • Loading branch information
otavio committed Apr 11, 2018
2 parents ebecaf9 + c9ef615 commit cc688f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
47 changes: 16 additions & 31 deletions cmd/updatehub-ctl/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"log"

"github.com/hokaccha/go-prettyjson"
"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
"github.com/updatehub/agent-sdk-go"
)

var rootCmd = &cobra.Command{
Expand All @@ -18,27 +17,29 @@ var rootCmd = &cobra.Command{
func main() {
var probeServerAddress string

agent := updatehub.NewClient()

probeCmd := &cobra.Command{
Use: "probe",
Short: "Probe the server for update",
RunE: func(cmd *cobra.Command, args []string) error {
return execProbeCmd(probeServerAddress)
return execProbeCmd(agent, probeServerAddress)
},
}

infoCmd := &cobra.Command{
Use: "info",
Short: "Print general information",
RunE: func(cmd *cobra.Command, args []string) error {
return execInfoCmd()
return execInfoCmd(agent)
},
}

logsCmd := &cobra.Command{
Use: "logs",
Short: "Print agent log entries",
RunE: func(cmd *cobra.Command, args []string) error {
return execLogsCmd()
return execLogsCmd(agent)
},
}

Expand All @@ -53,17 +54,10 @@ func main() {
}
}

func execProbeCmd(serverAddress string) error {
var probe ProbeResponse

var req struct {
ServerAddress string `json:"server-address"`
}
req.ServerAddress = serverAddress

_, _, errs := gorequest.New().Post(buildURL("/probe")).Send(req).EndStruct(&probe)
if len(errs) > 0 {
return errs[0]
func execProbeCmd(agent *updatehub.Client, serverAddress string) error {
probe, err := agent.ProbeCustomServer(serverAddress)
if err != nil {
return err
}

output, _ := prettyjson.Marshal(probe)
Expand All @@ -72,12 +66,10 @@ func execProbeCmd(serverAddress string) error {
return nil
}

func execInfoCmd() error {
var info AgentInfo

_, _, errs := gorequest.New().Get(buildURL("/info")).EndStruct(&info)
if len(errs) > 0 {
return errs[0]
func execInfoCmd(agent *updatehub.Client) error {
info, err := agent.GetInfo()
if err != nil {
return err
}

output, _ := prettyjson.Marshal(info)
Expand All @@ -86,15 +78,8 @@ func execInfoCmd() error {
return nil
}

func execLogsCmd() error {
_, body, errs := gorequest.New().Get(buildURL("/log")).End()
if len(errs) > 0 {
return errs[0]
}

var entries []LogEntry

err := json.Unmarshal([]byte(body), &entries)
func execLogsCmd(agent *updatehub.Client) error {
entries, err := agent.GetLogs()
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ import:
version: ^0.2.15
- package: github.com/anacrolix/missinggo
- package: github.com/koofr/go-httputils
- package: github.com/updatehub/agent-sdk-go
version: v1

0 comments on commit cc688f3

Please sign in to comment.