Skip to content

Commit

Permalink
Restructure cmd/dmsg (#288)
Browse files Browse the repository at this point in the history
* update dependency graph

* restructure command
  • Loading branch information
0pcom authored Sep 24, 2024
1 parent 5629102 commit ed51957
Show file tree
Hide file tree
Showing 4 changed files with 657 additions and 632 deletions.
10 changes: 3 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ run:
# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
format: colored-line-number
formats: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true
Expand All @@ -57,7 +57,7 @@ output:
# all available settings of specific linters
linters-settings:
errcheck:
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false

Expand All @@ -66,8 +66,7 @@ linters-settings:
check-blank: true
govet:
# report about shadowed variables
check-shadowing: true

shadow: true
# Obtain type information from installed (to $GOPATH/pkg) package files:
# golangci-lint will execute `go install -i` and `go test -i` for analyzed packages
# before analyzing them.
Expand Down Expand Up @@ -157,15 +156,12 @@ linters:
- errcheck
- gosimple
- staticcheck
- unused
- ineffassign
- typecheck
- gosec
- megacheck
- misspell
- nakedret
enable-all: false
disable:
disable-all: true
presets:
fast: false
Expand Down
89 changes: 89 additions & 0 deletions cmd/dmsg/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Package commands cmd/dmsg/commands/root.go
package commands

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

"github.com/spf13/cobra"

dd "github.com/skycoin/dmsg/cmd/dmsg-discovery/commands"
ds "github.com/skycoin/dmsg/cmd/dmsg-server/commands"
ds5 "github.com/skycoin/dmsg/cmd/dmsg-socks5/commands"
dc "github.com/skycoin/dmsg/cmd/dmsgcurl/commands"
dh "github.com/skycoin/dmsg/cmd/dmsghttp/commands"
di "github.com/skycoin/dmsg/cmd/dmsgip/commands"
dpc "github.com/skycoin/dmsg/cmd/dmsgpty-cli/commands"
dph "github.com/skycoin/dmsg/cmd/dmsgpty-host/commands"
dpu "github.com/skycoin/dmsg/cmd/dmsgpty-ui/commands"
dw "github.com/skycoin/dmsg/cmd/dmsgweb/commands"
)

func init() {
dmsgptyCmd.AddCommand(
dpc.RootCmd,
dph.RootCmd,
dpu.RootCmd,
)
RootCmd.AddCommand(
dmsgptyCmd,
dd.RootCmd,
ds.RootCmd,
dh.RootCmd,
dc.RootCmd,
dw.RootCmd,
ds5.RootCmd,
di.RootCmd,
)
dd.RootCmd.Use = "disc"
ds.RootCmd.Use = "server"
dh.RootCmd.Use = "http"
dc.RootCmd.Use = "curl"
dw.RootCmd.Use = "web"
ds5.RootCmd.Use = "socks"
dpc.RootCmd.Use = "cli"
dph.RootCmd.Use = "host"
dpu.RootCmd.Use = "ui"
di.RootCmd.Use = "ip"
}

// RootCmd contains all binaries which may be separately compiled as subcommands
var RootCmd = &cobra.Command{
Use: func() string {
return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0]
}(),
Short: "DMSG services & utilities",
Long: `
┌┬┐┌┬┐┌─┐┌─┐
│││││└─┐│ ┬
─┴┘┴ ┴└─┘└─┘
DMSG services & utilities`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
}

var dmsgptyCmd = &cobra.Command{
Use: "pty",
Short: "DMSG pseudoterminal (pty)",
Long: `
┌─┐┌┬┐┬ ┬
├─┘ │ └┬┘
┴ ┴ ┴
DMSG pseudoterminal (pty)`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
}

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

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

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

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"
dmsgip "github.com/skycoin/dmsg/cmd/dmsgip/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"
"github.com/skycoin/dmsg/cmd/dmsg/commands"
)

func init() {
dmsgptyCmd.AddCommand(
dmsgptycli.RootCmd,
dmsgptyhost.RootCmd,
dmsgptyui.RootCmd,
)
RootCmd.AddCommand(
dmsgptyCmd,
dmsgdisc.RootCmd,
dmsgserver.RootCmd,
dmsghttp.RootCmd,
dmsgcurl.RootCmd,
dmsgweb.RootCmd,
dmsgsocks.RootCmd,
dmsgip.RootCmd,
)
dmsgdisc.RootCmd.Use = "disc"
dmsgserver.RootCmd.Use = "server"
dmsghttp.RootCmd.Use = "http"
dmsgcurl.RootCmd.Use = "curl"
dmsgweb.RootCmd.Use = "web"
dmsgsocks.RootCmd.Use = "socks"
dmsgptycli.RootCmd.Use = "cli"
dmsgptyhost.RootCmd.Use = "host"
dmsgptyui.RootCmd.Use = "ui"
dmsgip.RootCmd.Use = "ip"

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

}

// RootCmd contains all binaries which may be separately compiled as subcommands
var RootCmd = &cobra.Command{
Use: func() string {
return strings.Split(filepath.Base(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%v", os.Args), "[", ""), "]", "")), " ")[0]
}(),
Short: "DMSG services & utilities",
Long: `
┌┬┐┌┬┐┌─┐┌─┐
│││││└─┐│ ┬
─┴┘┴ ┴└─┘└─┘
DMSG services & utilities`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
}

var dmsgptyCmd = &cobra.Command{
Use: "pty",
Short: "DMSG pseudoterminal (pty)",
Long: `
┌─┐┌┬┐┬ ┬
├─┘ │ └┬┘
┴ ┴ ┴
DMSG pseudoterminal (pty)`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
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 @@ -102,15 +29,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" +
Expand Down
Loading

0 comments on commit ed51957

Please sign in to comment.