Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/cli json #1354

Merged
merged 7 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/skywire-cli/commands/dmsgpty/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var visorsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, _ []string) {
remoteVisors, err := rpcClient().RemoteVisors()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("RPC connection failed; is skywire running?: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("RPC connection failed; is skywire running?: %v", err))
}

var msg string
Expand Down
27 changes: 14 additions & 13 deletions cmd/skywire-cli/commands/visor/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package clivisor
import (
"fmt"
"net/http"
"strings"

"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/cipher"
clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
Expand Down Expand Up @@ -43,14 +45,14 @@ var pkCmd = &cobra.Command{
if path != "" {
conf, err := visorconfig.ReadFile(path)
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to read config: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to read config: %v", err))
}
outputPK = conf.PK.Hex()
} else {
client := clirpc.Client()
overview, err := client.Overview()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
pk = overview.PubKey.String() + "\n"
if web {
Expand All @@ -60,16 +62,15 @@ var pkCmd = &cobra.Command{
}
outputPK = overview.PubKey.Hex() + "\n"
}

internal.PrintOutput(cmd.Flags(), outputPK, outputPK)
internal.PrintOutput(cmd.Flags(), strings.TrimSuffix(outputPK, "\n"), outputPK)
},
}

var hvpkCmd = &cobra.Command{
Use: "hvpk",
Short: "Public key of remote hypervisor",
Run: func(cmd *cobra.Command, _ []string) {
var hypervisors string
var hypervisors []cipher.PubKey

if pkg {
path = visorconfig.Pkgpath
Expand All @@ -78,18 +79,18 @@ var hvpkCmd = &cobra.Command{
if path != "" {
conf, err := visorconfig.ReadFile(path)
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to read config: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to read config: %v", err))
}
hypervisors = fmt.Sprintf("%v\n", conf.Hypervisors)
hypervisors = conf.Hypervisors
} else {
client := clirpc.Client()
overview, err := client.Overview()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
hypervisors = fmt.Sprintf("%v\n", overview.Hypervisors)
hypervisors = overview.Hypervisors
}
internal.PrintOutput(cmd.Flags(), hypervisors, hypervisors)
internal.PrintOutput(cmd.Flags(), hypervisors, fmt.Sprintf("%v\n", hypervisors))
},
}

Expand All @@ -100,7 +101,7 @@ var chvpkCmd = &cobra.Command{
client := clirpc.Client()
overview, err := client.Overview()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
internal.PrintOutput(cmd.Flags(), overview.ConnectedHypervisor, fmt.Sprintf("%v\n", overview.ConnectedHypervisor))
},
Expand All @@ -112,7 +113,7 @@ var summaryCmd = &cobra.Command{
Run: func(cmd *cobra.Command, _ []string) {
summary, err := clirpc.Client().Summary()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
msg := fmt.Sprintf(".:: Visor Summary ::.\nPublic key: %q\nSymmetric NAT: %t\nIP: %s\nDMSG Server: %q\nPing: %q\nVisor Version: %s\nSkybian Version: %s\nUptime Tracker: %s\nTime Online: %f seconds\nBuild Tag: %s\n",
summary.Overview.PubKey, summary.Overview.IsSymmetricNAT, summary.Overview.LocalIP, summary.DmsgStats.ServerPK, summary.DmsgStats.RoundTrip, summary.Overview.BuildInfo.Version, summary.SkybianBuildVersion,
Expand Down Expand Up @@ -152,7 +153,7 @@ var buildInfoCmd = &cobra.Command{
client := clirpc.Client()
overview, err := client.Overview()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
buildInfo := overview.BuildInfo
msg := fmt.Sprintf("Version %q built on %q against commit %q\n", buildInfo.Version, buildInfo.Date, buildInfo.Commit)
Expand Down
2 changes: 1 addition & 1 deletion cmd/skywire-cli/commands/visor/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func printRoutingRules(cmdFlags *pflag.FlagSet, rules ...routing.Rule) {
func parseUint(cmdFlags *pflag.FlagSet, name, v string, bitSize int) uint64 {
i, err := strconv.ParseUint(v, 10, bitSize)
if err != nil {
internal.PrintError(cmdFlags, fmt.Errorf("failed to parse <%s>: %v", name, err))
internal.PrintFatalError(cmdFlags, fmt.Errorf("failed to parse <%s>: %v", name, err))
}
return i
}
2 changes: 1 addition & 1 deletion cmd/skywire-cli/commands/visor/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var startCmd = &cobra.Command{
output, err = script.Exec(`bash -c 'go run cmd/skywire-visor/skywire-visor.go'`).String()
}
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to start visor: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to start visor: %v", err))
}
internal.PrintOutput(cmd.Flags(), output, fmt.Sprintln(output))
},
Expand Down
21 changes: 14 additions & 7 deletions cmd/skywire-cli/commands/visor/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ var addTpCmd = &cobra.Command{
Short: "Add a transport",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
isJSON, _ := cmd.Flags().GetBool(internal.JSONString) //nolint:errcheck

pk := internal.ParsePK(cmd.Flags(), "remote-public-key", args[0])

var tp *visor.TransportSummary
Expand All @@ -124,24 +126,29 @@ var addTpCmd = &cobra.Command{
if transportType != "" {
tp, err = clirpc.Client().AddTransport(pk, transportType, timeout)
if err != nil {
logger.WithError(err).Fatalf("Failed to establish %v transport", transportType)
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to establish %v transport: %v", transportType, err))
}
if !isJSON {
logger.Infof("Established %v transport to %v", transportType, pk)
}

logger.Infof("Established %v transport to %v", transportType, pk)
} else {
transportTypes := []network.Type{
network.STCP,
network.STCPR,
network.SUDPH,
network.DMSG,
network.STCP,
}
for _, transportType := range transportTypes {
tp, err = clirpc.Client().AddTransport(pk, string(transportType), timeout)
if err == nil {
logger.Infof("Established %v transport to %v", transportType, pk)
if !isJSON {
logger.Infof("Established %v transport to %v", transportType, pk)
}
break
}
logger.WithError(err).Warnf("Failed to establish %v transport", transportType)
if !isJSON {
logger.WithError(err).Warnf("Failed to establish %v transport", transportType)
}
}
}
PrintTransports(cmd.Flags(), tp)
Expand All @@ -155,7 +162,7 @@ var rmTpCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
tID := internal.ParseUUID(cmd.Flags(), "transport-id", args[0])
internal.Catch(cmd.Flags(), clirpc.Client().RemoveTransport(tID))
fmt.Println("OK")
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}

Expand Down
19 changes: 9 additions & 10 deletions cmd/skywire-cli/commands/vpn/vvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ var vpnUICmd = &cobra.Command{
if path != "" {
conf, err := visorconfig.ReadFile(path)
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to read in config: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to read in config: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", conf.PK.Hex())
} else {
client := clirpc.Client()
overview, err := client.Overview()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", overview.PubKey.Hex())
}
if err := webbrowser.Open(url); err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to open VPN UI in browser:: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to open VPN UI in browser:: %v", err))
}
},
}
Expand All @@ -78,14 +78,14 @@ var vpnURLCmd = &cobra.Command{
if path != "" {
conf, err := visorconfig.ReadFile(path)
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to read in config: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to read in config: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", conf.PK.Hex())
} else {
client := clirpc.Client()
overview, err := client.Overview()
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", overview.PubKey.Hex())
}
Expand All @@ -111,13 +111,13 @@ var vpnListCmd = &cobra.Command{
}
servers, err := client.VPNServers(ver, country)
if err != nil {
internal.PrintError(cmd.Flags(), err)
internal.PrintFatalError(cmd.Flags(), err)
}
if len(servers) == 0 {
internal.PrintError(cmd.Flags(), fmt.Errorf("No VPN Servers found"))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("No VPN Servers found"))
}
if isStats {
internal.PrintError(cmd.Flags(), fmt.Errorf("%d VPN Servers", len(servers)))
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("%d VPN Servers", len(servers)))
}

var msg string
Expand All @@ -135,11 +135,10 @@ var vpnListCmd = &cobra.Command{
}

var vpnStartCmd = &cobra.Command{
Use: "start",
Use: "start <public-key>",
Short: "start the vpn for <public-key>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(args[0])
internal.Catch(cmd.Flags(), clirpc.Client().StartVPNClient(args[0]))
internal.PrintOutput(cmd.Flags(), "OK", fmt.Sprintln("OK"))
},
Expand Down
14 changes: 7 additions & 7 deletions cmd/skywire-cli/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var JSONString = "json"
// Catch handles errors for skywire-cli commands packages
func Catch(cmdFlags *pflag.FlagSet, err error) {
if err != nil {
PrintError(cmdFlags, err)
PrintFatalError(cmdFlags, err)
}
}

// PrintError prints errors for skywire-cli commands packages
func PrintError(cmdFlags *pflag.FlagSet, err error) {
// PrintFatalError prints errors for skywire-cli commands packages
func PrintFatalError(cmdFlags *pflag.FlagSet, err error) {
isJSON, _ := cmdFlags.GetBool(JSONString) //nolint:errcheck
if isJSON {
errJSON := CLIOutput{
Expand All @@ -47,7 +47,7 @@ func ParsePK(cmdFlags *pflag.FlagSet, name, v string) cipher.PubKey {
var pk cipher.PubKey
err := pk.Set(v)
if err != nil {
PrintError(cmdFlags, fmt.Errorf("failed to parse <%s>: %v", name, err))
PrintFatalError(cmdFlags, fmt.Errorf("failed to parse <%s>: %v", name, err))
}
return pk
}
Expand All @@ -56,18 +56,18 @@ func ParsePK(cmdFlags *pflag.FlagSet, name, v string) cipher.PubKey {
func ParseUUID(cmdFlags *pflag.FlagSet, name, v string) uuid.UUID {
id, err := uuid.Parse(v)
if err != nil {
PrintError(cmdFlags, fmt.Errorf("failed to parse <%s>: %v", name, err))
PrintFatalError(cmdFlags, fmt.Errorf("failed to parse <%s>: %v", name, err))
}
return id
}

// CLIOutput ss
// CLIOutput is used to print the cli output in json
type CLIOutput struct {
Output interface{} `json:"output,omitempty"`
Err string `json:"error,omitempty"`
}

// PrintOutput ss
// PrintOutput prints either the normal output or the json output as per the global `--json` flag
func PrintOutput(cmdFlags *pflag.FlagSet, outputJSON, output interface{}) {
isJSON, _ := cmdFlags.GetBool(JSONString) //nolint:errcheck
if isJSON {
Expand Down