From 55f4bee70d7fc0f44b447669717ce62e45f42c80 Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 17:24:56 +0530 Subject: [PATCH 1/7] Fix pkCmd --- cmd/skywire-cli/commands/visor/info.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/skywire-cli/commands/visor/info.go b/cmd/skywire-cli/commands/visor/info.go index 6ac3473226..c284a883b6 100644 --- a/cmd/skywire-cli/commands/visor/info.go +++ b/cmd/skywire-cli/commands/visor/info.go @@ -3,6 +3,7 @@ package clivisor import ( "fmt" "net/http" + "strings" "github.com/spf13/cobra" @@ -60,8 +61,7 @@ var pkCmd = &cobra.Command{ } outputPK = overview.PubKey.Hex() + "\n" } - - internal.PrintOutput(cmd.Flags(), outputPK, outputPK) + internal.PrintOutput(cmd.Flags(), strings.TrimSuffix(outputPK, "\n"), outputPK) }, } From 85b87ad361f5f2e05aa8617dbbbccadee27dcc13 Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 17:27:47 +0530 Subject: [PATCH 2/7] Fix hvpkCmd --- cmd/skywire-cli/commands/visor/info.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/skywire-cli/commands/visor/info.go b/cmd/skywire-cli/commands/visor/info.go index c284a883b6..90eca4c0bd 100644 --- a/cmd/skywire-cli/commands/visor/info.go +++ b/cmd/skywire-cli/commands/visor/info.go @@ -7,6 +7,7 @@ import ( "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" @@ -69,7 +70,7 @@ 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 @@ -80,16 +81,16 @@ var hvpkCmd = &cobra.Command{ if err != nil { internal.PrintError(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)) } - 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)) }, } From b7e99ba80eca3c84aa67081c33202279c152b804 Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 17:29:55 +0530 Subject: [PATCH 3/7] Fix rmTpCmd --- cmd/skywire-cli/commands/visor/transports.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/skywire-cli/commands/visor/transports.go b/cmd/skywire-cli/commands/visor/transports.go index aa3968d558..05357bbd2c 100644 --- a/cmd/skywire-cli/commands/visor/transports.go +++ b/cmd/skywire-cli/commands/visor/transports.go @@ -155,7 +155,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") }, } From d627565704394b7f7ca20a6f23d69dc3ea8d8c59 Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 19:14:13 +0530 Subject: [PATCH 4/7] Fix addTpCmd --- cmd/skywire-cli/commands/visor/transports.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/skywire-cli/commands/visor/transports.go b/cmd/skywire-cli/commands/visor/transports.go index 05357bbd2c..9362685344 100644 --- a/cmd/skywire-cli/commands/visor/transports.go +++ b/cmd/skywire-cli/commands/visor/transports.go @@ -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 @@ -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.PrintError(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) From bfdb9d083edf6838a2e138544673a0c0ff59fd81 Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 20:22:18 +0530 Subject: [PATCH 5/7] Fix vpnStartCmd --- cmd/skywire-cli/commands/vpn/vvpn.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/skywire-cli/commands/vpn/vvpn.go b/cmd/skywire-cli/commands/vpn/vvpn.go index aa6af63581..354dbc0eda 100644 --- a/cmd/skywire-cli/commands/vpn/vvpn.go +++ b/cmd/skywire-cli/commands/vpn/vvpn.go @@ -135,11 +135,10 @@ var vpnListCmd = &cobra.Command{ } var vpnStartCmd = &cobra.Command{ - Use: "start", + Use: "start ", Short: "start the vpn for ", 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")) }, From a043e4d7996d2eb0b1fa66b35393e9c5ecc844f0 Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 20:24:41 +0530 Subject: [PATCH 6/7] Update comments of PrintOutput and CLIOutput --- cmd/skywire-cli/internal/internal.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go index e5cdddd66b..82b853c020 100644 --- a/cmd/skywire-cli/internal/internal.go +++ b/cmd/skywire-cli/internal/internal.go @@ -61,13 +61,13 @@ func ParseUUID(cmdFlags *pflag.FlagSet, name, v string) uuid.UUID { 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 { From cdb7327202ce879ce842cfd19a909f000df08dbf Mon Sep 17 00:00:00 2001 From: ersonp Date: Fri, 9 Sep 2022 20:26:55 +0530 Subject: [PATCH 7/7] Rename PrintError to PrintFatalError --- cmd/skywire-cli/commands/dmsgpty/root.go | 2 +- cmd/skywire-cli/commands/visor/info.go | 14 +++++++------- cmd/skywire-cli/commands/visor/route.go | 2 +- cmd/skywire-cli/commands/visor/start.go | 2 +- cmd/skywire-cli/commands/visor/transports.go | 2 +- cmd/skywire-cli/commands/vpn/vvpn.go | 16 ++++++++-------- cmd/skywire-cli/internal/internal.go | 10 +++++----- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cmd/skywire-cli/commands/dmsgpty/root.go b/cmd/skywire-cli/commands/dmsgpty/root.go index 6b59503a20..ce698fe7fe 100644 --- a/cmd/skywire-cli/commands/dmsgpty/root.go +++ b/cmd/skywire-cli/commands/dmsgpty/root.go @@ -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 diff --git a/cmd/skywire-cli/commands/visor/info.go b/cmd/skywire-cli/commands/visor/info.go index 90eca4c0bd..26a6b9ea05 100644 --- a/cmd/skywire-cli/commands/visor/info.go +++ b/cmd/skywire-cli/commands/visor/info.go @@ -45,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 { @@ -79,14 +79,14 @@ 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 = 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 = overview.Hypervisors } @@ -101,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)) }, @@ -113,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, @@ -153,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) diff --git a/cmd/skywire-cli/commands/visor/route.go b/cmd/skywire-cli/commands/visor/route.go index 21f2b423bc..fff05d3df4 100644 --- a/cmd/skywire-cli/commands/visor/route.go +++ b/cmd/skywire-cli/commands/visor/route.go @@ -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 } diff --git a/cmd/skywire-cli/commands/visor/start.go b/cmd/skywire-cli/commands/visor/start.go index 8ae3afd898..d9badd3843 100644 --- a/cmd/skywire-cli/commands/visor/start.go +++ b/cmd/skywire-cli/commands/visor/start.go @@ -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)) }, diff --git a/cmd/skywire-cli/commands/visor/transports.go b/cmd/skywire-cli/commands/visor/transports.go index 9362685344..959b04d2a6 100644 --- a/cmd/skywire-cli/commands/visor/transports.go +++ b/cmd/skywire-cli/commands/visor/transports.go @@ -126,7 +126,7 @@ var addTpCmd = &cobra.Command{ if transportType != "" { tp, err = clirpc.Client().AddTransport(pk, transportType, timeout) if err != nil { - internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to establish %v transport: %v", transportType, err)) + 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) diff --git a/cmd/skywire-cli/commands/vpn/vvpn.go b/cmd/skywire-cli/commands/vpn/vvpn.go index 354dbc0eda..11ce104041 100644 --- a/cmd/skywire-cli/commands/vpn/vvpn.go +++ b/cmd/skywire-cli/commands/vpn/vvpn.go @@ -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)) } }, } @@ -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()) } @@ -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 diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go index 82b853c020..21eb3c1b0e 100644 --- a/cmd/skywire-cli/internal/internal.go +++ b/cmd/skywire-cli/internal/internal.go @@ -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{ @@ -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 } @@ -56,7 +56,7 @@ 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 }