github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/command/serve.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 9 "github.com/GeniusesGroup/libgo/protocol" 10 ) 11 12 func ServeCLA(c protocol.Command, arguments []string) (err protocol.Error) { 13 var serviceName string 14 if len(arguments) > 0 { 15 serviceName = arguments[0] 16 } else { 17 serviceName = "help" 18 } 19 20 // Also check for finding help command to check any custom help command 21 var command protocol.Command = c.SubCommand(serviceName) 22 if command == nil { 23 // We don't find any related command even custom help, so print auto generated help. 24 if serviceName == "help" || serviceName == "-h" || serviceName == "--help" { 25 // helpMessage() 26 // Accept 'go mod help' and 'go mod help foo' for 'go help mod' and 'go help mod foo'. 27 // help.Help(os.Stdout, append(strings.Split(cfg.CmdName, " "), args[1:]...)) 28 } else { 29 fmt.Fprintf(os.Stderr, "unknown command\nRun '%s help' for usage.\n", CommandPath(c)) 30 err = &ErrServiceNotFound 31 } 32 return 33 } else if command.Name() != serviceName { 34 fmt.Fprintf(os.Stderr, "Do you mean '%s %s'?\n", CommandPath(c), command.Name()) 35 // TODO::: 36 return 37 } 38 39 err = command.ServeCLA(arguments[1:]) 40 return 41 } 42 43 // Root finds root command. or return nil if it is the root 44 func Root(c protocol.Command) (root protocol.Command) { 45 for { 46 if root.Parent() != nil { 47 root = root.Parent() 48 } else { 49 break 50 } 51 } 52 return 53 } 54 55 // CommandPath returns the full path to this command exclude itself. 56 func CommandPath(command protocol.Command) (fullName string) { 57 for { 58 fullName = command.Name() + " " + fullName 59 command = command.Parent() 60 if command == nil { 61 break 62 } 63 } 64 // remove trailing space 65 fullName = fullName[:len(fullName)-1] 66 return 67 }