diff --git a/cmd/skywire/commands/root.go b/cmd/skywire/commands/root.go new file mode 100644 index 000000000..18b5bd591 --- /dev/null +++ b/cmd/skywire/commands/root.go @@ -0,0 +1,356 @@ +// Package commands cmd/skywire/commands/root.go +package commands + +import ( + "fmt" + "log" + "os" + "path/filepath" + "strings" + + "github.com/bitfield/script" + "github.com/pterm/pterm" + "github.com/pterm/pterm/putils" + dmsgdisc "github.com/skycoin/dmsg/cmd/dmsg-discovery/commands" + dmsgserver "github.com/skycoin/dmsg/cmd/dmsg-server/commands" + dmsgsocks "github.com/skycoin/dmsg/cmd/dmsg-socks5/commands" + dmsgcurl "github.com/skycoin/dmsg/cmd/dmsgcurl/commands" + dmsghttp "github.com/skycoin/dmsg/cmd/dmsghttp/commands" + dmsgptycli "github.com/skycoin/dmsg/cmd/dmsgpty-cli/commands" + dmsgptyhost "github.com/skycoin/dmsg/cmd/dmsgpty-host/commands" + dmsgptyui "github.com/skycoin/dmsg/cmd/dmsgpty-ui/commands" + dmsgweb "github.com/skycoin/dmsg/cmd/dmsgweb/commands" + sd "github.com/skycoin/skycoin-service-discovery/cmd/service-discovery/commands" + "github.com/spf13/cobra" + + ar "github.com/skycoin/skywire-services/cmd/address-resolver/commands" + confbs "github.com/skycoin/skywire-services/cmd/config-bootstrapper/commands" + kg "github.com/skycoin/skywire-services/cmd/keys-gen/commands" + nv "github.com/skycoin/skywire-services/cmd/node-visualizer/commands" + rf "github.com/skycoin/skywire-services/cmd/route-finder/commands" + se "github.com/skycoin/skywire-services/cmd/sw-env/commands" + tpd "github.com/skycoin/skywire-services/cmd/transport-discovery/commands" + tps "github.com/skycoin/skywire-services/cmd/transport-setup/commands" + "github.com/skycoin/skywire-utilities/pkg/buildinfo" + sc "github.com/skycoin/skywire/cmd/apps/skychat/commands" + ssc "github.com/skycoin/skywire/cmd/apps/skysocks-client/commands" + ss "github.com/skycoin/skywire/cmd/apps/skysocks/commands" + vpnc "github.com/skycoin/skywire/cmd/apps/vpn-client/commands" + vpns "github.com/skycoin/skywire/cmd/apps/vpn-server/commands" + sn "github.com/skycoin/skywire/cmd/setup-node/commands" + scli "github.com/skycoin/skywire/cmd/skywire-cli/commands" + "github.com/skycoin/skywire/pkg/visor" +) + +func init() { + dmsgptyCmd.AddCommand( + dmsgptycli.RootCmd, + dmsgptyhost.RootCmd, + dmsgptyui.RootCmd, + ) + dmsgCmd.AddCommand( + dmsgptyCmd, + dmsgdisc.RootCmd, + dmsgserver.RootCmd, + dmsghttp.RootCmd, + dmsgcurl.RootCmd, + dmsgweb.RootCmd, + dmsgsocks.RootCmd, + ) + svcCmd.AddCommand( + sn.RootCmd, + tpd.RootCmd, + tps.RootCmd, + ar.RootCmd, + rf.RootCmd, + confbs.RootCmd, + kg.RootCmd, + nv.RootCmd, + se.RootCmd, + sd.RootCmd, + ) + appsCmd.AddCommand( + vpns.RootCmd, + vpnc.RootCmd, + ssc.RootCmd, + ss.RootCmd, + sc.RootCmd, + ) + RootCmd.AddCommand( + visor.RootCmd, + scli.RootCmd, + svcCmd, + dmsgCmd, + appsCmd, + treeCmd, + docCmd, + ) + visor.RootCmd.Long = ` + ┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┬ ┬┬┌─┐┌─┐┬─┐ + └─┐├┴┐└┬┘││││├┬┘├┤───└┐┌┘│└─┐│ │├┬┘ + └─┘┴ ┴ ┴ └┴┘┴┴└─└─┘ └┘ ┴└─┘└─┘┴└─` + dmsgcurl.RootCmd.Use = "curl" + dmsgweb.RootCmd.Use = "web" + dmsgptycli.RootCmd.Use = "cli" + dmsgptyhost.RootCmd.Use = "host" + dmsgptyui.RootCmd.Use = "ui" + dmsgdisc.RootCmd.Use = "disc" + dmsgserver.RootCmd.Use = "server" + dmsghttp.RootCmd.Use = "http" + dmsgcurl.RootCmd.Use = "curl" + dmsgweb.RootCmd.Use = "web" + dmsgsocks.RootCmd.Use = "socks" + tpd.RootCmd.Use = "tpd" + tps.RootCmd.Use = "tps" + ar.RootCmd.Use = "ar" + rf.RootCmd.Use = "rf" + confbs.RootCmd.Use = "cb" + kg.RootCmd.Use = "kg" + nv.RootCmd.Use = "nv" + se.RootCmd.Use = "se" + sd.RootCmd.Use = "sd" + sn.RootCmd.Use = "sn" + scli.RootCmd.Use = "cli" + visor.RootCmd.Use = "visor" + vpns.RootCmd.Use = "vpn-server" + vpnc.RootCmd.Use = "vpn-client" + ssc.RootCmd.Use = "skysocks-client" + ss.RootCmd.Use = "skysocks" + sc.RootCmd.Use = "skychat" +} + +// RootCmd contains literally every 'command' from four repos here +var RootCmd = &cobra.Command{ + Use: func() string { + return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0] + }(), + Long: ` + ┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ + └─┐├┴┐└┬┘││││├┬┘├┤ + └─┘┴ ┴ ┴ └┴┘┴┴└─└─┘`, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, + Version: buildinfo.Version(), +} + +// RootCmd contains all subcommands +var svcCmd = &cobra.Command{ + Use: "svc", + Short: "Skywire services", + Long: ` + ┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┌─┐┌─┐┬─┐┬ ┬┬┌─┐┌─┐┌─┐ + └─┐├┴┐└┬┘││││├┬┘├┤───└─┐├┤ ├┬┘└┐┌┘││ ├┤ └─┐ + └─┘┴ ┴ ┴ └┴┘┴┴└─└─┘ └─┘└─┘┴└─ └┘ ┴└─┘└─┘└─┘`, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, + Version: buildinfo.Version(), +} + +// RootCmd contains all binaries which may be separately compiled as subcommands +var dmsgCmd = &cobra.Command{ + Use: "dmsg", + Short: "Dmsg services & utilities", + Long: ` + ┌┬┐┌┬┐┌─┐┌─┐ + │││││└─┐│ ┬ + ─┴┘┴ ┴└─┘└─┘ `, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, +} + +var dmsgptyCmd = &cobra.Command{ + Use: "pty", + Short: "Dmsg pseudoterminal (pty)", + Long: ` + ┌─┐┌┬┐┬ ┬ + ├─┘ │ └┬┘ + ┴ ┴ ┴ `, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, +} +var appsCmd = &cobra.Command{ + Use: "app", + Short: "skywire native applications", + Long: ` + ┌─┐┌─┐┌─┐┌─┐ + ├─┤├─┘├─┘└─┐ + ┴ ┴┴ ┴ └─┘`, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, +} + +var treeCmd = &cobra.Command{ + Use: "tree", + Short: "subcommand tree", + Long: `subcommand tree`, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, + Run: func(_ *cobra.Command, _ []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() { + use := strings.Split(j.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 1, Text: use[0]}) + for _, k := range j.Commands() { + use := strings.Split(k.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 2, Text: use[0]}) + for _, l := range k.Commands() { + use := strings.Split(l.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 3, Text: use[0]}) + for _, m := range l.Commands() { + use := strings.Split(m.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 4, Text: use[0]}) + for _, n := range m.Commands() { + use := strings.Split(n.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 5, Text: use[0]}) + for _, o := range n.Commands() { + use := strings.Split(o.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 6, Text: use[0]}) + for _, p := range o.Commands() { + use := strings.Split(p.Use, " ") + leveledList = append(leveledList, pterm.LeveledListItem{Level: 7, Text: use[0]}) + } + } + } + } + } + } + } + // Generate tree from LeveledList. + r := putils.TreeFromLeveledList(leveledList) + + // Render TreePrinter + err := pterm.DefaultTree.WithRoot(r).Render() + if err != nil { + log.Fatal("render subcommand tree: ", err) + } + }, +} + +// for toc generation use: https://github.com/ekalinin/github-markdown-toc.go +var docCmd = &cobra.Command{ + Use: "doc", + Short: "generate markdown docs", + Long: `generate markdown docs + + UNHIDEFLAGS=1 go run cmd/skywire/skywire.go doc + + UNHIDEFLAGS=1 go run cmd/skywire/skywire.go doc > cmd/skywire/README1.md + + generate toc: + + cat cmd/skywire/README1.md | gh-md-toc`, + SilenceErrors: true, + SilenceUsage: true, + DisableSuggestions: true, + DisableFlagsInUseLine: true, + Run: func(_ *cobra.Command, _ []string) { + fmt.Printf("\n# %s\n", "skywire documentation") + fmt.Printf("\n## %s\n", "subcommand tree") + fmt.Printf("\n%s\n", "A tree representation of the skywire subcommands") + fmt.Printf("\n```\n") + _, err := script.Exec(os.Args[0] + " tree").Stdout() //nolint + if err != nil { + fmt.Println(err.Error()) + } + fmt.Printf("\n```\n") + + var use string + for _, j := range RootCmd.Commands() { + use = strings.Split(j.Use, " ")[0] + fmt.Printf("\n### %s\n", use) + fmt.Printf("\n```\n") + j.Help() //nolint + fmt.Printf("\n```\n") + if j.Name() == "cli" { + fmt.Printf("\n%s\n", "skywire command line interface") + fmt.Printf("\n## %s\n", RootCmd.Use) + fmt.Printf("\n```\n") + 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.") + + fmt.Printf("\n%s\n", "It is not recommended to expose the rpc server on the local network. Exposing the rpc allows unsecured access to the machine over the local network") + fmt.Printf("\n```\n") + fmt.Printf("\n%s\n", "Global Flags:") + fmt.Printf("\n%s\n", " --rpc string RPC server address (default \"localhost:3435\")") + fmt.Printf("\n%s\n", " --json bool print output as json") + fmt.Printf("\n```\n") + } + for _, k := range j.Commands() { + use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + fmt.Printf("\n#### %s\n", use) + fmt.Printf("\n```\n") + k.Help() //nolint + fmt.Printf("\n```\n") + if k.Name() == "survey" { + fmt.Printf("\n```\n") + _, err = script.Exec("sudo " + os.Args[0] + ` survey`).Stdout() //nolint + if err != nil { + fmt.Println(err.Error()) + } + fmt.Printf("\n```\n") + } + for _, l := range k.Commands() { + use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + fmt.Printf("\n##### %s\n", use) + fmt.Printf("\n```\n") + l.Help() //nolint + fmt.Printf("\n```\n") + if l.Name() == "gen" { + fmt.Printf("\n##### Example for package / msi\n") + fmt.Printf("\n```\n") + fmt.Printf("$ skywire cli config gen -bpirxn\n") + _, err = script.Exec(os.Args[0] + ` cli config gen -bpirxn`).Stdout() //nolint + if err != nil { + fmt.Println(err.Error()) + } + fmt.Printf("\n```\n") + } + for _, m := range l.Commands() { + use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + " " + strings.Split(m.Use, " ")[0] + fmt.Printf("\n###### %s\n", use) + fmt.Printf("\n```\n") + m.Help() //nolint + fmt.Printf("\n```\n") + for _, n := range m.Commands() { + use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + " " + strings.Split(m.Use, " ")[0] + " " + strings.Split(n.Use, " ")[0] + fmt.Printf("\n###### %s\n", use) + fmt.Printf("\n```\n") + m.Help() //nolint + fmt.Printf("\n```\n") + for _, o := range n.Commands() { + use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + " " + strings.Split(m.Use, " ")[0] + " " + strings.Split(n.Use, " ")[0] + " " + strings.Split(o.Use, " ")[0] + fmt.Printf("\n###### %s\n", use) + fmt.Printf("\n```\n") + m.Help() //nolint + fmt.Printf("\n```\n") + } + } + } + } + } + } + }, +} + +// Execute executes root CLI command. +func Execute() { + if err := RootCmd.Execute(); err != nil { + log.Fatal("Failed to execute command: ", err) + } +} diff --git a/cmd/skywire/skywire.go b/cmd/skywire/skywire.go index 719067e9a..66e854a02 100644 --- a/cmd/skywire/skywire.go +++ b/cmd/skywire/skywire.go @@ -5,365 +5,23 @@ skywire package main import ( - "fmt" - "log" - "os" - "path/filepath" - "strings" - - "github.com/bitfield/script" cc "github.com/ivanpirog/coloredcobra" - "github.com/pterm/pterm" - "github.com/pterm/pterm/putils" - dmsgdisc "github.com/skycoin/dmsg/cmd/dmsg-discovery/commands" - dmsgserver "github.com/skycoin/dmsg/cmd/dmsg-server/commands" - dmsgsocks "github.com/skycoin/dmsg/cmd/dmsg-socks5/commands" - dmsgcurl "github.com/skycoin/dmsg/cmd/dmsgcurl/commands" - dmsghttp "github.com/skycoin/dmsg/cmd/dmsghttp/commands" - dmsgptycli "github.com/skycoin/dmsg/cmd/dmsgpty-cli/commands" - dmsgptyhost "github.com/skycoin/dmsg/cmd/dmsgpty-host/commands" - dmsgptyui "github.com/skycoin/dmsg/cmd/dmsgpty-ui/commands" - dmsgweb "github.com/skycoin/dmsg/cmd/dmsgweb/commands" - sd "github.com/skycoin/skycoin-service-discovery/cmd/service-discovery/commands" "github.com/spf13/cobra" - ar "github.com/skycoin/skywire-services/cmd/address-resolver/commands" - confbs "github.com/skycoin/skywire-services/cmd/config-bootstrapper/commands" - kg "github.com/skycoin/skywire-services/cmd/keys-gen/commands" - nv "github.com/skycoin/skywire-services/cmd/node-visualizer/commands" - rf "github.com/skycoin/skywire-services/cmd/route-finder/commands" - se "github.com/skycoin/skywire-services/cmd/sw-env/commands" - tpd "github.com/skycoin/skywire-services/cmd/transport-discovery/commands" - tps "github.com/skycoin/skywire-services/cmd/transport-setup/commands" - "github.com/skycoin/skywire-utilities/pkg/buildinfo" - sc "github.com/skycoin/skywire/cmd/apps/skychat/commands" - ssc "github.com/skycoin/skywire/cmd/apps/skysocks-client/commands" - ss "github.com/skycoin/skywire/cmd/apps/skysocks/commands" - vpnc "github.com/skycoin/skywire/cmd/apps/vpn-client/commands" - vpns "github.com/skycoin/skywire/cmd/apps/vpn-server/commands" - sn "github.com/skycoin/skywire/cmd/setup-node/commands" - scli "github.com/skycoin/skywire/cmd/skywire-cli/commands" - "github.com/skycoin/skywire/pkg/visor" + "github.com/skycoin/skywire/cmd/skywire/commands" ) func init() { - dmsgptyCmd.AddCommand( - dmsgptycli.RootCmd, - dmsgptyhost.RootCmd, - dmsgptyui.RootCmd, - ) - dmsgCmd.AddCommand( - dmsgptyCmd, - dmsgdisc.RootCmd, - dmsgserver.RootCmd, - dmsghttp.RootCmd, - dmsgcurl.RootCmd, - dmsgweb.RootCmd, - dmsgsocks.RootCmd, - ) - svcCmd.AddCommand( - sn.RootCmd, - tpd.RootCmd, - tps.RootCmd, - ar.RootCmd, - rf.RootCmd, - confbs.RootCmd, - kg.RootCmd, - nv.RootCmd, - se.RootCmd, - sd.RootCmd, - ) - appsCmd.AddCommand( - vpns.RootCmd, - vpnc.RootCmd, - ssc.RootCmd, - ss.RootCmd, - sc.RootCmd, - ) - RootCmd.AddCommand( - visor.RootCmd, - scli.RootCmd, - svcCmd, - dmsgCmd, - appsCmd, - treeCmd, - docCmd, - ) - visor.RootCmd.Long = ` - ┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┬ ┬┬┌─┐┌─┐┬─┐ - └─┐├┴┐└┬┘││││├┬┘├┤───└┐┌┘│└─┐│ │├┬┘ - └─┘┴ ┴ ┴ └┴┘┴┴└─└─┘ └┘ ┴└─┘└─┘┴└─` - dmsgcurl.RootCmd.Use = "curl" - dmsgweb.RootCmd.Use = "web" - dmsgptycli.RootCmd.Use = "cli" - dmsgptyhost.RootCmd.Use = "host" - dmsgptyui.RootCmd.Use = "ui" - dmsgdisc.RootCmd.Use = "disc" - dmsgserver.RootCmd.Use = "server" - dmsghttp.RootCmd.Use = "http" - dmsgcurl.RootCmd.Use = "curl" - dmsgweb.RootCmd.Use = "web" - dmsgsocks.RootCmd.Use = "socks" - tpd.RootCmd.Use = "tpd" - tps.RootCmd.Use = "tps" - ar.RootCmd.Use = "ar" - rf.RootCmd.Use = "rf" - confbs.RootCmd.Use = "cb" - kg.RootCmd.Use = "kg" - nv.RootCmd.Use = "nv" - se.RootCmd.Use = "se" - sd.RootCmd.Use = "sd" - sn.RootCmd.Use = "sn" - scli.RootCmd.Use = "cli" - visor.RootCmd.Use = "visor" - vpns.RootCmd.Use = "vpn-server" - vpnc.RootCmd.Use = "vpn-client" - ssc.RootCmd.Use = "skysocks-client" - ss.RootCmd.Use = "skysocks" - sc.RootCmd.Use = "skychat" - 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 - RootCmd.SetUsageTemplate(help) - -} - -// RootCmd contains literally every 'command' from four repos here -var RootCmd = &cobra.Command{ - Use: func() string { - return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0] - }(), - Long: ` - ┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ - └─┐├┴┐└┬┘││││├┬┘├┤ - └─┘┴ ┴ ┴ └┴┘┴┴└─└─┘`, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, - Version: buildinfo.Version(), -} - -// RootCmd contains all subcommands -var svcCmd = &cobra.Command{ - Use: "svc", - Short: "Skywire services", - Long: ` - ┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┌─┐┌─┐┬─┐┬ ┬┬┌─┐┌─┐┌─┐ - └─┐├┴┐└┬┘││││├┬┘├┤───└─┐├┤ ├┬┘└┐┌┘││ ├┤ └─┐ - └─┘┴ ┴ ┴ └┴┘┴┴└─└─┘ └─┘└─┘┴└─ └┘ ┴└─┘└─┘└─┘`, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, - Version: buildinfo.Version(), -} - -// RootCmd contains all binaries which may be separately compiled as subcommands -var dmsgCmd = &cobra.Command{ - Use: "dmsg", - Short: "Dmsg services & utilities", - Long: ` - ┌┬┐┌┬┐┌─┐┌─┐ - │││││└─┐│ ┬ - ─┴┘┴ ┴└─┘└─┘ `, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, -} - -var dmsgptyCmd = &cobra.Command{ - Use: "pty", - Short: "Dmsg pseudoterminal (pty)", - Long: ` - ┌─┐┌┬┐┬ ┬ - ├─┘ │ └┬┘ - ┴ ┴ ┴ `, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, -} -var appsCmd = &cobra.Command{ - Use: "app", - Short: "skywire native applications", - Long: ` - ┌─┐┌─┐┌─┐┌─┐ - ├─┤├─┘├─┘└─┐ - ┴ ┴┴ ┴ └─┘`, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, -} - -var treeCmd = &cobra.Command{ - Use: "tree", - Short: "subcommand tree", - Long: `subcommand tree`, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, - Run: func(_ *cobra.Command, _ []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() { - use := strings.Split(j.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 1, Text: use[0]}) - for _, k := range j.Commands() { - use := strings.Split(k.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 2, Text: use[0]}) - for _, l := range k.Commands() { - use := strings.Split(l.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 3, Text: use[0]}) - for _, m := range l.Commands() { - use := strings.Split(m.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 4, Text: use[0]}) - for _, n := range m.Commands() { - use := strings.Split(n.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 5, Text: use[0]}) - for _, o := range n.Commands() { - use := strings.Split(o.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 6, Text: use[0]}) - for _, p := range o.Commands() { - use := strings.Split(p.Use, " ") - leveledList = append(leveledList, pterm.LeveledListItem{Level: 7, Text: use[0]}) - } - } - } - } - } - } - } - // Generate tree from LeveledList. - r := putils.TreeFromLeveledList(leveledList) - - // Render TreePrinter - err := pterm.DefaultTree.WithRoot(r).Render() - if err != nil { - log.Fatal("render subcommand tree: ", err) - } - }, -} - -// for toc generation use: https://github.com/ekalinin/github-markdown-toc.go -var docCmd = &cobra.Command{ - Use: "doc", - Short: "generate markdown docs", - Long: `generate markdown docs - - UNHIDEFLAGS=1 go run cmd/skywire/skywire.go doc - - UNHIDEFLAGS=1 go run cmd/skywire/skywire.go doc > cmd/skywire/README1.md - - generate toc: - - cat cmd/skywire/README1.md | gh-md-toc`, - SilenceErrors: true, - SilenceUsage: true, - DisableSuggestions: true, - DisableFlagsInUseLine: true, - Run: func(_ *cobra.Command, _ []string) { - fmt.Printf("\n# %s\n", "skywire documentation") - fmt.Printf("\n## %s\n", "subcommand tree") - fmt.Printf("\n%s\n", "A tree representation of the skywire subcommands") - fmt.Printf("\n```\n") - _, err := script.Exec(os.Args[0] + " tree").Stdout() //nolint - if err != nil { - fmt.Println(err.Error()) - } - fmt.Printf("\n```\n") - - var use string - for _, j := range RootCmd.Commands() { - use = strings.Split(j.Use, " ")[0] - fmt.Printf("\n### %s\n", use) - fmt.Printf("\n```\n") - j.Help() //nolint - fmt.Printf("\n```\n") - if j.Name() == "cli" { - fmt.Printf("\n%s\n", "skywire command line interface") - fmt.Printf("\n## %s\n", RootCmd.Use) - fmt.Printf("\n```\n") - 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.") - - fmt.Printf("\n%s\n", "It is not recommended to expose the rpc server on the local network. Exposing the rpc allows unsecured access to the machine over the local network") - fmt.Printf("\n```\n") - fmt.Printf("\n%s\n", "Global Flags:") - fmt.Printf("\n%s\n", " --rpc string RPC server address (default \"localhost:3435\")") - fmt.Printf("\n%s\n", " --json bool print output as json") - fmt.Printf("\n```\n") - } - for _, k := range j.Commands() { - use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] - fmt.Printf("\n#### %s\n", use) - fmt.Printf("\n```\n") - k.Help() //nolint - fmt.Printf("\n```\n") - if k.Name() == "survey" { - fmt.Printf("\n```\n") - _, err = script.Exec("sudo " + os.Args[0] + ` survey`).Stdout() //nolint - if err != nil { - fmt.Println(err.Error()) - } - fmt.Printf("\n```\n") - } - for _, l := range k.Commands() { - use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] - fmt.Printf("\n##### %s\n", use) - fmt.Printf("\n```\n") - l.Help() //nolint - fmt.Printf("\n```\n") - if l.Name() == "gen" { - fmt.Printf("\n##### Example for package / msi\n") - fmt.Printf("\n```\n") - fmt.Printf("$ skywire cli config gen -bpirxn\n") - _, err = script.Exec(os.Args[0] + ` cli config gen -bpirxn`).Stdout() //nolint - if err != nil { - fmt.Println(err.Error()) - } - fmt.Printf("\n```\n") - } - for _, m := range l.Commands() { - use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + " " + strings.Split(m.Use, " ")[0] - fmt.Printf("\n###### %s\n", use) - fmt.Printf("\n```\n") - m.Help() //nolint - fmt.Printf("\n```\n") - for _, n := range m.Commands() { - use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + " " + strings.Split(m.Use, " ")[0] + " " + strings.Split(n.Use, " ")[0] - fmt.Printf("\n###### %s\n", use) - fmt.Printf("\n```\n") - m.Help() //nolint - fmt.Printf("\n```\n") - for _, o := range n.Commands() { - use = strings.Split(j.Use, " ")[0] + " " + strings.Split(k.Use, " ")[0] + " " + strings.Split(l.Use, " ")[0] + " " + strings.Split(m.Use, " ")[0] + " " + strings.Split(n.Use, " ")[0] + " " + strings.Split(o.Use, " ")[0] - fmt.Printf("\n###### %s\n", use) - fmt.Printf("\n```\n") - m.Help() //nolint - fmt.Printf("\n```\n") - } - } - } - } - } - } - }, + commands.RootCmd.SetUsageTemplate(help) + commands.RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help menu") + commands.RootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) + commands.RootCmd.PersistentFlags().MarkHidden("help") //nolint } func main() { cc.Init(&cc.Config{ - RootCmd: RootCmd, + RootCmd: commands.RootCmd, Headings: cc.HiBlue + cc.Bold, Commands: cc.HiBlue + cc.Bold, CmdShortDescr: cc.HiBlue, @@ -374,14 +32,12 @@ func main() { NoExtraNewlines: true, NoBottomNewline: true, }) - if err := RootCmd.Execute(); err != nil { - fmt.Println(err) - } + commands.Execute() } -const help = "{{if gt (len .Aliases) 0}}" + +const help = "{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" + "{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}" + - "Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}\r\n " + + "Available Commands:{{range .Commands}} {{if and (ne .Name \"completion\") .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" + diff --git a/go.mod b/go.mod index d256ec5d2..66d925fe8 100644 --- a/go.mod +++ b/go.mod @@ -30,10 +30,10 @@ require ( github.com/pterm/pterm v0.12.66 github.com/robert-nix/ansihtml v1.0.1 github.com/sirupsen/logrus v1.9.3 - github.com/skycoin/dmsg v1.3.26-0.20240922174815-ced25b343ec5 + github.com/skycoin/dmsg v1.3.26-0.20240924024628-ed5195787d40 github.com/skycoin/skycoin v0.27.1 github.com/skycoin/skycoin-service-discovery v1.3.26-0.20240923193126-2175869a0af5 - github.com/skycoin/skywire-services v1.3.26-0.20240923193909-50d313921778 + github.com/skycoin/skywire-services v1.3.26-0.20240924025250-cb6c2e8fe08f github.com/skycoin/skywire-utilities v1.3.25 github.com/skycoin/systray v1.10.0 github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 @@ -179,5 +179,7 @@ replace github.com/xxxserxxx/gotop/v4 => github.com/ersonp/gotop/v4 v4.2.1 // then, re-comment the line before saving. // replace github.com/skycoin/dmsg => github.com/skycoin/dmsg // replace github.com/skycoin/skywire-services => github.com/skycoin/skywire-services +//replace github.com/skycoin/skywire-services => github.com/skycoin/skywire-services + // replace github.com/skycoin/skycoin-service-discovery => github.com/skycoin/skycoin-service-discovery // replace github.com/skycoin/skywire-utilities => github.com/skycoin/skywire-utilities diff --git a/go.sum b/go.sum index 42151b19d..306bd0fe0 100644 --- a/go.sum +++ b/go.sum @@ -682,16 +682,16 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/skycoin/dmsg v1.3.26-0.20240922174815-ced25b343ec5 h1:992WLADQdKjZbpJYdw0EYuOaI7mKHQQ57jDFebvUYPs= -github.com/skycoin/dmsg v1.3.26-0.20240922174815-ced25b343ec5/go.mod h1:qfF8cAig3JHLAojmfYEWn8ttV4aHsiitkZeD1TH8Wrg= +github.com/skycoin/dmsg v1.3.26-0.20240924024628-ed5195787d40 h1:GKUDgXHWahO45dDuivC36h6rRVfg6R02d8oDhQp2t74= +github.com/skycoin/dmsg v1.3.26-0.20240924024628-ed5195787d40/go.mod h1:qfF8cAig3JHLAojmfYEWn8ttV4aHsiitkZeD1TH8Wrg= github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0duwyG+7WliWz5u9kgk1h5MnLuA= github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:UXghlricA7J3aRD/k7p/zBObQfmBawwCxIVPVjz2Q3o= github.com/skycoin/skycoin v0.27.1 h1:HatxsRwVSPaV4qxH6290xPBmkH/HgiuAoY2qC+e8C9I= github.com/skycoin/skycoin v0.27.1/go.mod h1:78nHjQzd8KG0jJJVL/j0xMmrihXi70ti63fh8vXScJw= github.com/skycoin/skycoin-service-discovery v1.3.26-0.20240923193126-2175869a0af5 h1:YuUEIh0ZxpprE6cHgTks3nsA9W0h6MAeTiTYzvRvlyY= github.com/skycoin/skycoin-service-discovery v1.3.26-0.20240923193126-2175869a0af5/go.mod h1:vfFU+qDJkxdf2AgPPem/WbKRxm2dNsWy1d+HJ3mluSM= -github.com/skycoin/skywire-services v1.3.26-0.20240923193909-50d313921778 h1:WP9BZ//o7a4XnIF7x+IIbqxlbq1HN/TGUHipQk2vCM0= -github.com/skycoin/skywire-services v1.3.26-0.20240923193909-50d313921778/go.mod h1:oQnvmSxQsunEwogMKnwXsUN852XBctixoUsih0XfjO8= +github.com/skycoin/skywire-services v1.3.26-0.20240924025250-cb6c2e8fe08f h1:Jyd+js9PUSGb8RcKgOhZ/wqpvkpLIV4dU6fPcQNBDvw= +github.com/skycoin/skywire-services v1.3.26-0.20240924025250-cb6c2e8fe08f/go.mod h1:4SS2gwkGUZU9vjYUVpS74Roo+tWhug+uTRVu+fjbgqs= github.com/skycoin/skywire-utilities v1.3.25 h1:mk8dUonFdhVopFF3d9wbOyXXoiuAO+mN1y+ve6SzgX4= github.com/skycoin/skywire-utilities v1.3.25/go.mod h1:yFKWpL1bDRPKU3uK+cTF4PnYUMe+eyIj5N2bk4sF5Cw= github.com/skycoin/systray v1.10.0 h1:fQZJHMylpVvfmOOTLvUssfyHVDoC8Idx6Ba2BlLEuGg= diff --git a/vendor/modules.txt b/vendor/modules.txt index f5ba888d4..39c2a9fbd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -477,7 +477,7 @@ github.com/rs/cors ## explicit; go 1.13 github.com/sirupsen/logrus github.com/sirupsen/logrus/hooks/syslog -# github.com/skycoin/dmsg v1.3.26-0.20240922174815-ced25b343ec5 +# github.com/skycoin/dmsg v1.3.26-0.20240924024628-ed5195787d40 ## explicit; go 1.21 github.com/skycoin/dmsg/cmd/dmsg-discovery/commands github.com/skycoin/dmsg/cmd/dmsg-server/commands @@ -525,7 +525,7 @@ github.com/skycoin/skycoin-service-discovery/internal/pg github.com/skycoin/skycoin-service-discovery/internal/sdmetrics github.com/skycoin/skycoin-service-discovery/pkg/service-discovery/api github.com/skycoin/skycoin-service-discovery/pkg/service-discovery/store -# github.com/skycoin/skywire-services v1.3.26-0.20240923193909-50d313921778 +# github.com/skycoin/skywire-services v1.3.26-0.20240924025250-cb6c2e8fe08f ## explicit; go 1.21 github.com/skycoin/skywire-services/cmd/address-resolver/commands github.com/skycoin/skywire-services/cmd/config-bootstrapper/commands