Skip to content

Commit

Permalink
Combine skywire-cli skywire-visor setup-node binaries (#1565)
Browse files Browse the repository at this point in the history
* combined visor + cli binaries

* add setup-node as a subcommand of the skywire binary

* add comments for exported variables

* add comments for exported variables

* make format
  • Loading branch information
0pcom authored May 18, 2023
1 parent c534b7a commit 1b5e708
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 26 deletions.
15 changes: 8 additions & 7 deletions cmd/setup-node/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ var (
)

func init() {
rootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", "", "address to bind metrics API to")
rootCmd.Flags().StringVar(&syslogAddr, "syslog", "", "syslog server address. E.g. localhost:514")
rootCmd.Flags().StringVar(&tag, "tag", "setup_node", "logging tag")
rootCmd.Flags().BoolVarP(&cfgFromStdin, "stdin", "i", false, "read config from STDIN")
RootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", "", "address to bind metrics API to")
RootCmd.Flags().StringVar(&syslogAddr, "syslog", "", "syslog server address. E.g. localhost:514")
RootCmd.Flags().StringVar(&tag, "tag", "setup_node", "logging tag")
RootCmd.Flags().BoolVarP(&cfgFromStdin, "stdin", "i", false, "read config from STDIN")
}

var rootCmd = &cobra.Command{
// RootCmd is the root command for setup node
var RootCmd = &cobra.Command{
Use: "setup-node [config.json]",
Short: "Route Setup Node for skywire",
Long: `
Expand Down Expand Up @@ -124,7 +125,7 @@ func prepareMetrics(log logrus.FieldLogger) setupmetrics.Metrics {
// Execute executes root CLI command.
func Execute() {
cc.Init(&cc.Config{
RootCmd: rootCmd,
RootCmd: RootCmd,
Headings: cc.HiBlue + cc.Bold,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Expand All @@ -136,7 +137,7 @@ func Execute() {
NoExtraNewlines: true,
NoBottomNewline: true,
})
if err := rootCmd.Execute(); err != nil {
if err := RootCmd.Execute(); err != nil {
panic(err)
}
}
33 changes: 17 additions & 16 deletions cmd/skywire-cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
)

var rootCmd = &cobra.Command{
Use: "skywire-cli",
// RootCmd is the root command for skywire-cli
var RootCmd = &cobra.Command{
Use: "cli",
Short: "Command Line Interface for skywire",
Long: `
┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┌─┐┬ ┬
Expand All @@ -55,8 +56,8 @@ var treeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// You can use a LeveledList here, for easy generation.
leveledList := pterm.LeveledList{}
leveledList = append(leveledList, pterm.LeveledListItem{Level: 0, Text: rootCmd.Use})
for _, j := range rootCmd.Commands() {
leveledList = append(leveledList, pterm.LeveledListItem{Level: 0, Text: RootCmd.Use})
for _, j := range RootCmd.Commands() {
use := strings.Split(j.Use, " ")
leveledList = append(leveledList, pterm.LeveledListItem{Level: 1, Text: use[0]})
for _, k := range j.Commands() {
Expand Down Expand Up @@ -112,9 +113,9 @@ var docCmd = &cobra.Command{
fmt.Printf("\n# %s\n", "skywire-cli documentation")
fmt.Printf("\n%s\n", "skywire command line interface")

fmt.Printf("\n## %s\n", rootCmd.Use)
fmt.Printf("\n## %s\n", RootCmd.Use)
fmt.Printf("\n```\n")
rootCmd.Help() //nolint
RootCmd.Help() //nolint
fmt.Printf("\n```\n")
fmt.Printf("\n## %s\n", "global flags")
fmt.Printf("\n%s\n", "The skywire-cli interacts with the running visor via rpc calls. By default the rpc server is available on localhost:3435. The rpc address and port the visor is using may be changed in the config file, once generated.")
Expand All @@ -133,7 +134,7 @@ var docCmd = &cobra.Command{
fmt.Printf("\n```\n")

var use string
for _, j := range rootCmd.Commands() {
for _, j := range RootCmd.Commands() {
use = strings.Split(j.Use, " ")[0]
fmt.Printf("\n### %s\n", use)
fmt.Printf("\n```\n")
Expand Down Expand Up @@ -177,7 +178,7 @@ var docCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(
RootCmd.AddCommand(
cliconfig.RootCmd,
clidmsgpty.RootCmd,
clivisor.RootCmd,
Expand All @@ -196,19 +197,19 @@ func init() {
docCmd,
)
var jsonOutput bool
rootCmd.PersistentFlags().BoolVar(&jsonOutput, internal.JSONString, false, "print output in json")
rootCmd.PersistentFlags().MarkHidden(internal.JSONString) //nolint
RootCmd.PersistentFlags().BoolVar(&jsonOutput, internal.JSONString, false, "print output in json")
RootCmd.PersistentFlags().MarkHidden(internal.JSONString) //nolint
var helpflag bool
rootCmd.SetUsageTemplate(help)
rootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for "+rootCmd.Use)
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
rootCmd.PersistentFlags().MarkHidden("help") //nolint
RootCmd.SetUsageTemplate(help)
RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for "+RootCmd.Use)
RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
RootCmd.PersistentFlags().MarkHidden("help") //nolint
}

// Execute executes root CLI command.
func Execute() {
cc.Init(&cc.Config{
RootCmd: rootCmd,
RootCmd: RootCmd,
Headings: cc.HiBlue + cc.Bold, //+ cc.Underline,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Expand All @@ -220,7 +221,7 @@ func Execute() {
NoExtraNewlines: true,
NoBottomNewline: true,
})
if err := rootCmd.Execute(); err != nil {
if err := RootCmd.Execute(); err != nil {
log.Fatal("Failed to execute command: ", err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/skywire-visor/skywire-visor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// /* cmd/skywire-visor/skywire-visor.go
// /* cmd/skywire/skywire.go
/*
skywire visor
skywire
*/
package main

Expand Down
73 changes: 73 additions & 0 deletions cmd/skywire/skywire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// /* cmd/skywire-visor/skywire-visor.go
/*
skywire visor
*/
package main

import (
"fmt"

cc "github.com/ivanpirog/coloredcobra"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
setupnode "github.com/skycoin/skywire/cmd/setup-node/commands"
skywirecli "github.com/skycoin/skywire/cmd/skywire-cli/commands"
"github.com/skycoin/skywire/pkg/visor"
)

func init() {
rootCmd.AddCommand(
visor.RootCmd,
skywirecli.RootCmd,
setupnode.RootCmd,
)
var helpflag bool
rootCmd.SetUsageTemplate(help)
rootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for "+rootCmd.Use)
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
rootCmd.PersistentFlags().MarkHidden("help") //nolint
rootCmd.CompletionOptions.DisableDefaultCmd = true

}

var rootCmd = &cobra.Command{
Use: "skywire",
Long: `
┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐
└─┐├┴┐└┬┘││││├┬┘├┤
└─┘┴ ┴ ┴ └┴┘┴┴└─└─┘`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Version: buildinfo.Version(),
}

func main() {
cc.Init(&cc.Config{
RootCmd: rootCmd,
Headings: cc.HiBlue + cc.Bold,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Example: cc.HiBlue + cc.Italic,
ExecName: cc.HiBlue + cc.Bold,
Flags: cc.HiBlue + cc.Bold,
FlagsDescr: cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}

const help = "{{if gt (len .Aliases) 0}}" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}" +
"Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}\r\n " +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\r\n\r\n" +
"Flags:\r\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
"Global Flags:\r\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"
2 changes: 1 addition & 1 deletion pkg/visor/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func trimStringFromDot(s string) string {

// RootCmd contains the help command & invocation flags
var RootCmd = &cobra.Command{
Use: "skywire-visor",
Use: "visor",
Short: "Skywire Visor",
Long: `
┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐
Expand Down

0 comments on commit 1b5e708

Please sign in to comment.