Skip to content

Commit

Permalink
Restructure cmd/skywire-services (#79)
Browse files Browse the repository at this point in the history
* restructure skywire-services command

* restructure skywire-services command

* update dependency graph

* make format
  • Loading branch information
0pcom authored Sep 24, 2024
1 parent 50d3139 commit cb6c2e8
Show file tree
Hide file tree
Showing 6 changed files with 586 additions and 552 deletions.
71 changes: 71 additions & 0 deletions cmd/skywire-services/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Package commands cmd/skywire-services/commands/services.go
package commands

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"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"
ut "github.com/skycoin/skywire-services/cmd/uptime-tracker/commands"
)

func init() {
RootCmd.AddCommand(
tpd.RootCmd,
tps.RootCmd,
ar.RootCmd,
rf.RootCmd,
confbs.RootCmd,
kg.RootCmd,
nv.RootCmd,
se.RootCmd,
ut.RootCmd,
)
tpd.RootCmd.Use = "tpd"
tps.RootCmd.Use = "tps"
ar.RootCmd.Use = "ar"
rf.RootCmd.Use = "rf"
confbs.RootCmd.Use = "confbs"
kg.RootCmd.Use = "kg"
nv.RootCmd.Use = "nv"
se.RootCmd.Use = "se"
ut.RootCmd.Use = "ut"
}

// RootCmd contains all subcommands
var RootCmd = &cobra.Command{
Use: func() string {
return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0]
}(),
Short: "Skywire services",
Long: `
┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┌─┐┌─┐┬─┐┬ ┬┬┌─┐┌─┐┌─┐
└─┐├┴┐└┬┘││││├┬┘├┤───└─┐├┤ ├┬┘└┐┌┘││ ├┤ └─┐
└─┘┴ ┴ ┴ └┴┘┴┴└─└─┘ └─┘└─┘┴└─ └┘ ┴└─┘└─┘└─┘
Skywire services`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Version: buildinfo.Version(),
}

// Execute executes root CLI command.
func Execute() {
if err := RootCmd.Execute(); err != nil {
log.Fatal("Failed to execute command: ", err)
}
}
79 changes: 10 additions & 69 deletions cmd/skywire-services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,23 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

cc "github.com/ivanpirog/coloredcobra"
"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"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"
ut "github.com/skycoin/skywire-services/cmd/uptime-tracker/commands"
"github.com/skycoin/skywire-services/cmd/skywire-services/commands"
)

func init() {
RootCmd.AddCommand(
tpd.RootCmd,
tps.RootCmd,
ar.RootCmd,
rf.RootCmd,
confbs.RootCmd,
kg.RootCmd,
nv.RootCmd,
se.RootCmd,
ut.RootCmd,
)
tpd.RootCmd.Use = "tpd"
tps.RootCmd.Use = "tps"
ar.RootCmd.Use = "ar"
rf.RootCmd.Use = "rf"
confbs.RootCmd.Use = "confbs"
kg.RootCmd.Use = "kg"
nv.RootCmd.Use = "nv"
se.RootCmd.Use = "se"
ut.RootCmd.Use = "ut"

var helpflag bool
RootCmd.SetUsageTemplate(help)
RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for skywire-services")
RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
RootCmd.PersistentFlags().MarkHidden("help") //nolint
RootCmd.CompletionOptions.DisableDefaultCmd = true

}

// RootCmd contains all subcommands
var RootCmd = &cobra.Command{
Use: func() string {
return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0]
}(),
Short: "Skywire services",
Long: `
┌─┐┬┌─┬ ┬┬ ┬┬┬─┐┌─┐ ┌─┐┌─┐┬─┐┬ ┬┬┌─┐┌─┐┌─┐
└─┐├┴┐└┬┘││││├┬┘├┤───└─┐├┤ ├┬┘└┐┌┘││ ├┤ └─┐
└─┘┴ ┴ ┴ └┴┘┴┴└─└─┘ └─┘└─┘┴└─ └┘ ┴└─┘└─┘└─┘
Skywire services`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Version: buildinfo.Version(),
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,
Expand All @@ -85,15 +29,12 @@ func main() {
NoExtraNewlines: true,
NoBottomNewline: true,
})
if err := RootCmd.Execute(); err != nil {
log.Fatal(err)
}
commands.Execute()
}

const help = "Usage:\r\n" +
" {{.UseLine}}{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}\r\n\r\n" +
"Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}\r\n " +
const help = "{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}" +
"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" +
Expand Down
Loading

0 comments on commit cb6c2e8

Please sign in to comment.