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

Feat/cli json output #1346

Merged
merged 33 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
94be372
Add global flag --json to cli
ersonp Aug 26, 2022
3aab3a1
Update pkCmd
ersonp Aug 26, 2022
88a0968
Add func PrintFatalError
ersonp Aug 26, 2022
6509b67
Minor update
ersonp Aug 26, 2022
e8ebecf
Update hvpkCmd
ersonp Aug 26, 2022
1930806
Update chvpkCmd
ersonp Aug 26, 2022
1893520
Update summaryCmd
ersonp Aug 26, 2022
fd04c2c
Update buildInfoCmd
ersonp Aug 26, 2022
763efae
Use Catch instead of PrintFatalError
ersonp Aug 26, 2022
da4f7da
Add func PrintError
ersonp Aug 26, 2022
3d67b08
Update command lsAppsCmd
ersonp Aug 29, 2022
066b905
Update start, stop, setAutoStart and logs command
ersonp Aug 29, 2022
8d1c6f9
Update visor execCmd
ersonp Aug 29, 2022
0405775
Update lsRulesCmd
ersonp Aug 29, 2022
90ea771
Update rmRuleCmd
ersonp Aug 29, 2022
8579a20
Fix and update addRuleCmd
ersonp Aug 29, 2022
e07bc6d
Update shutdownCmd
ersonp Aug 29, 2022
8f6b9d2
Update startCmd
ersonp Aug 29, 2022
32d9fc1
Update lsTypesCmd
ersonp Aug 29, 2022
223125f
Update PrintTransports
ersonp Aug 29, 2022
52d0316
Remove depreciated flag public from tp
ersonp Aug 29, 2022
040baf0
Update PrintTransportEntries
ersonp Aug 29, 2022
94eba37
Make format
ersonp Aug 29, 2022
2c3f476
Add changelog
ersonp Aug 29, 2022
8f9cabe
Update dmsgURLCmd
ersonp Aug 29, 2022
236664b
Update visorsCmd
ersonp Aug 29, 2022
f5c6b02
Update vpnUICmd
ersonp Aug 29, 2022
8b6e111
Update vpnURLCmd
ersonp Aug 29, 2022
f720cc4
Update vpnListCmd
ersonp Aug 29, 2022
43d3744
Update vpnStartCmd and vpnStopCmd
ersonp Aug 29, 2022
9241f30
Update vpnStatusCmd
ersonp Aug 29, 2022
de00068
Update rtfind
ersonp Aug 29, 2022
20b2d93
Update availableServersCmd
ersonp Aug 29, 2022
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.2.0
### Added
- `skywire-cli` global flag `--json` [#1346](https://github.com/skycoin/skywire/pull/1346)

### Changed
- `skywire-cli visor route add-rule` subcommands [#1346](https://github.com/skycoin/skywire/pull/1346)

### Removed
- `skywire-cli visor tp add` flag `--public` [#1346](https://github.com/skycoin/skywire/pull/1346)

## 1.1.0

### Added
Expand Down
16 changes: 12 additions & 4 deletions cmd/skywire-cli/commands/dmsgpty/hvdmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/toqueteos/webbrowser"

"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)

Expand Down Expand Up @@ -57,28 +58,35 @@ var dmsgUICmd = &cobra.Command{
var dmsgURLCmd = &cobra.Command{
Use: "url",
Short: "Show dmsgpty UI URL",
Run: func(_ *cobra.Command, _ []string) {
Run: func(cmd *cobra.Command, _ []string) {
if pk == "" {
if pkg {
path = visorconfig.Pkgpath
}
if path != "" {
conf, err := visorconfig.ReadFile(path)
if err != nil {
log.Fatal("Failed to read in config file:", err)
internal.Catch(cmd.Flags(), fmt.Errorf("Failed to read in config file: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/pty/%s", conf.PK.Hex())
} else {
client := rpcClient()
overview, err := client.Overview()
if err != nil {
logger.Fatal("Failed to connect; is skywire running?\n", err)
internal.Catch(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/pty/%s", overview.PubKey.Hex())
}
} else {
url = fmt.Sprintf("http://127.0.0.1:8000/pty/%s", pk)
}
fmt.Println(url)

output := struct {
URL string `json:"url"`
}{
URL: url,
}

internal.PrintOutput(cmd.Flags(), output, fmt.Sprintln(url))
},
}
14 changes: 5 additions & 9 deletions cmd/skywire-cli/commands/dmsgpty/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"os"
"strconv"
"time"

Expand All @@ -21,7 +20,6 @@ var (
ptyPort string
masterLogger = logging.NewMasterLogger()
packageLogger = masterLogger.PackageLogger("dmsgpty")
logger = logging.MustGetLogger("skywire-cli")
rpcAddr string
path string
pk string
Expand Down Expand Up @@ -51,29 +49,27 @@ func init() {
var visorsCmd = &cobra.Command{
Use: "list",
Short: "List connected visors",
Run: func(_ *cobra.Command, _ []string) {
Run: func(cmd *cobra.Command, _ []string) {
remoteVisors, err := rpcClient().RemoteVisors()
if err != nil {
packageLogger.Fatal("RPC connection failed; is skywire running?\n", err)
internal.PrintError(cmd.Flags(), fmt.Errorf("RPC connection failed; is skywire running?: %v", err))
}

var msg string
for idx, pk := range remoteVisors {
msg += fmt.Sprintf("%d. %s\n", idx+1, pk)
}
if _, err := os.Stdout.Write([]byte(msg)); err != nil {
packageLogger.Fatal("Failed to output build info:", err)
}
internal.PrintOutput(cmd.Flags(), remoteVisors, msg)
},
}

var shellCmd = &cobra.Command{
Use: "start <pk>",
Short: "Start dmsgpty session",
Args: cobra.MinimumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
cli := dmsgpty.DefaultCLI()
addr := internal.ParsePK("pk", args[0])
addr := internal.ParsePK(cmd.Flags(), "pk", args[0])
port, _ := strconv.ParseUint(ptyPort, 10, 16) //nolint
ctx, cancel := cmdutil.SignalContext(context.Background(), nil)
defer cancel()
Expand Down
56 changes: 43 additions & 13 deletions cmd/skywire-cli/commands/mdisc/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package climdisc

import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"text/tabwriter"
"time"

"github.com/sirupsen/logrus"
"github.com/skycoin/dmsg/pkg/disc"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/logging"
utilenv "github.com/skycoin/skywire-utilities/pkg/skyenv"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
Expand Down Expand Up @@ -44,36 +47,63 @@ var entryCmd = &cobra.Command{
Use: "entry <visor-public-key>",
Short: "Fetch an entry",
Args: cobra.MinimumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
pk := internal.ParsePK("visor-public-key", args[0])
pk := internal.ParsePK(cmd.Flags(), "visor-public-key", args[0])

masterLogger.SetLevel(logrus.InfoLevel)

entry, err := disc.NewHTTP(mdAddr, &http.Client{}, packageLogger).Entry(ctx, pk)
internal.Catch(err)
fmt.Println(entry)
internal.Catch(cmd.Flags(), err)
internal.PrintOutput(cmd.Flags(), entry, fmt.Sprintln(entry))
},
}

var availableServersCmd = &cobra.Command{
Use: "servers",
Short: "Fetch available servers",
Run: func(_ *cobra.Command, _ []string) {
Run: func(cmd *cobra.Command, _ []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

masterLogger.SetLevel(logrus.InfoLevel)

entries, err := disc.NewHTTP(mdAddr, &http.Client{}, packageLogger).AvailableServers(ctx)
internal.Catch(err)
printAvailableServers(entries)
internal.Catch(cmd.Flags(), err)
printAvailableServers(cmd.Flags(), entries)
},
}

func printAvailableServers(entries []*disc.Entry) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent)
func printAvailableServers(cmdFlags *pflag.FlagSet, entries []*disc.Entry) {
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 5, ' ', tabwriter.TabIndent)
_, err := fmt.Fprintln(w, "version\tregistered\tpublic-key\taddress\tavailable-sessions")
internal.Catch(err)
internal.Catch(cmdFlags, err)

type serverEntry struct {
Version string `json:"version"`
Registered int64 `json:"registered"`
PublicKey cipher.PubKey `json:"public_key"`
Address string `json:"address"`
AvailableSessions int `json:"available_sessions"`
}

var serverEntries []serverEntry

for _, entry := range entries {
_, err := fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%d\n",
entry.Version, entry.Timestamp, entry.Static, entry.Server.Address, entry.Server.AvailableSessions)
internal.Catch(err)
sEntry := serverEntry{
Version: entry.Version,
Registered: entry.Timestamp,
PublicKey: entry.Static,
Address: entry.Server.Address,
AvailableSessions: entry.Server.AvailableSessions,
}
serverEntries = append(serverEntries, sEntry)
internal.Catch(cmdFlags, err)
}
internal.Catch(w.Flush())
internal.Catch(cmdFlags, w.Flush())
internal.PrintOutput(cmdFlags, serverEntries, b.String())
}
7 changes: 6 additions & 1 deletion cmd/skywire-cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
clirtfind "github.com/skycoin/skywire/cmd/skywire-cli/commands/rtfind"
clivisor "github.com/skycoin/skywire/cmd/skywire-cli/commands/visor"
clivpn "github.com/skycoin/skywire/cmd/skywire-cli/commands/vpn"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
)

var rootCmd = &cobra.Command{
Expand All @@ -40,8 +41,12 @@ func init() {
climdisc.RootCmd,
clicompletion.RootCmd,
)

var helpflag bool
rootCmd.PersistentFlags().StringVarP(&clirpc.Addr, "rpc", "", "localhost:3435", "RPC server address")
var jsonOutput bool

rootCmd.PersistentFlags().StringVar(&clirpc.Addr, "rpc", "localhost:3435", "RPC server address")
rootCmd.PersistentFlags().BoolVar(&jsonOutput, internal.JSONString, false, "print output in json")
rootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for "+rootCmd.Use)
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
rootCmd.PersistentFlags().MarkHidden("help") //nolint
Expand Down
21 changes: 14 additions & 7 deletions cmd/skywire-cli/commands/rtfind/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,27 @@ var RootCmd = &cobra.Command{
Use: "rtfind <public-key-visor-1> <public-key-visor-2>",
Short: "Query the Route Finder",
Args: cobra.MinimumNArgs(2),
Run: func(_ *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, args []string) {
rfc := rfclient.NewHTTP(frAddr, timeout, &http.Client{}, nil)

var srcPK, dstPK cipher.PubKey
internal.Catch(srcPK.Set(args[0]))
internal.Catch(dstPK.Set(args[1]))
internal.Catch(cmd.Flags(), srcPK.Set(args[0]))
internal.Catch(cmd.Flags(), dstPK.Set(args[1]))
forward := [2]cipher.PubKey{srcPK, dstPK}
backward := [2]cipher.PubKey{dstPK, srcPK}
ctx := context.Background()
routes, err := rfc.FindRoutes(ctx, []routing.PathEdges{forward, backward},
&rfclient.RouteOptions{MinHops: frMinHops, MaxHops: frMaxHops})
internal.Catch(err)

fmt.Println("forward: ", routes[forward][0])
fmt.Println("reverse: ", routes[backward][0])
internal.Catch(cmd.Flags(), err)

output := fmt.Sprintf("forward: %v\n reverse: %v", routes[forward][0], routes[backward][0])
outputJSON := struct {
Forward []routing.Hop `json:"forward"`
Reverse []routing.Hop `json:"reverse"`
}{
Forward: routes[forward][0],
Reverse: routes[backward][0],
}
internal.PrintOutput(cmd.Flags(), outputJSON, output)
},
}
Loading