Skip to content

Commit

Permalink
enhance help message
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 23, 2024
1 parent 48d5fef commit ced2865
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 58 deletions.
10 changes: 8 additions & 2 deletions cmd/xgo/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ The commands are:
version print xgo version
revision print xgo revision
upgrade upgrade to latest version of xgo
tool invoke xgo tools
tool invoke xgo tools
Examples:
xgo build -o main ./ build current module
xgo build -o main -gcflags="all=-N -l" ./ build current module with debug flags
xgo run ./ run current module
xgo test ./... test all test cases of current module
xgo exec go version print instrumented go version
xgo tool help print help for xgo tools
Examples of Trace:
xgo test -run TestSomething --strace ./ test and collect stack trace
xgo tool trace TestSomething.json view collected stack trace
xgo exec go version print instrumented go version
Example of Test Explorer:
xgo tool test-explorer open test explorer
See https://github.com/xhd2015/xgo for documentation.
Expand Down
8 changes: 2 additions & 6 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
fmt.Fprintf(os.Stderr, "xgo: requires command\nRun 'xgo help' for usage.\n")
os.Exit(1)
}
if cmd == "help" {
if cmd == "help" || cmd == "-h" || cmd == "--help" {
fmt.Print(strings.TrimPrefix(help, "\n"))
return
}
Expand All @@ -69,11 +69,7 @@ func main() {
return
}
if cmd == "tool" {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "xgo tool: requires tool to run\n")
os.Exit(1)
}
err := handleTool(args[0], args[1:])
err := handleTool(args)
if err != nil {
if err, ok := err.(*exec.ExitError); ok {
os.Exit(err.ExitCode())
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.36"
const REVISION = "1e1b9419279db2f75b8619256fced2e8a84e9ee7+1"
const NUMBER = 229
const REVISION = "48d5fefe9c2c051c940e088429f9253b80a65305+1"
const NUMBER = 230

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
48 changes: 47 additions & 1 deletion cmd/xgo/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,53 @@ import (
"github.com/xhd2015/xgo/support/cmd"
)

func handleTool(tool string, args []string) error {
const toolHelp = `
Xgo ships with a toolset that improves testing experience.
Usage:
xgo tool <command> [arguments]
The commands are:
trace stack trace visualization
test-explorer test explorer
coverage incremental coverage tool
list list all tools
help show help
Examples:
xgo tool trace TestSomething.json visualize a generated trace
xgo tool test-explorer open test explorer UI
xgo tool coverage serve cover.out visualize incrementa coverage of cover.out
See https://github.com/xhd2015/xgo for documentation.
`

const toolList = `
Available tools:
trace visualize a generated trace
test-explorer open test explorer UI
coverage visualize incrementa coverage
`

func handleTool(args []string) error {
// tool string,

// if len(args) == 0 {
// fmt.Fprintf(os.Stderr, "xgo tool: requires tool to run\n")
// os.Exit(1)
// }

if len(args) == 0 || args[0] == "help" || args[0] == "-h" || args[0] == "--help" {
fmt.Print(strings.TrimPrefix(toolHelp, "\n"))
return nil
}
tool := args[0]
args = args[1:]
if tool == "list" {
fmt.Print(strings.TrimPrefix(toolList, "\n"))
return nil
}
if tool == "trace" {
trace.Main(args)
return nil
Expand Down
31 changes: 27 additions & 4 deletions cmd/xgo/trace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,43 @@ import (
"github.com/xhd2015/xgo/support/netutil"
)

const help = `
Xgo tool trace visualize a generated trace file.
Usage:
xgo tool trace <file>
Examples:
xgo test -run TestSomething --strace ./ generate trace file
xgo tool trace TestSomething.json visualize a generated trace
See https://github.com/xhd2015/xgo for documentation.
`

func Main(args []string) {
var files []string
var port string
n := len(args)

var showHelp bool
for i := 0; i < n; i++ {
arg := args[i]
if arg == "--" {
files = append(files, args[i+1:]...)
break
}
if arg == "-h" || arg == "--help" {
showHelp = true
break
}
if arg == "--port" {
if i+1 >= n {
fmt.Fprintf(os.Stderr, "--port requires arg\n")
os.Exit(1)
}
port = arg
port = args[i+1]
i++
continue
} else if strings.HasPrefix(arg, "--port=") {
port = strings.TrimPrefix(arg, "--port=")
Expand All @@ -47,6 +68,10 @@ func Main(args []string) {
fmt.Fprintf(os.Stderr, "unrecognized flag: %s\n", arg)
os.Exit(1)
}
if showHelp {
fmt.Print(strings.TrimPrefix(help, "\n"))
return
}
if len(files) == 0 {
fmt.Fprintf(os.Stderr, "requires file\n")
os.Exit(1)
Expand Down Expand Up @@ -136,13 +161,11 @@ func serveFile(portStr string, file string) error {
}
port = int(parsePort)
}
err = netutil.ServePort(port, autoIncrPort, 500*time.Millisecond, func(port int) {
err = netutil.ServePortHTTP(server, port, autoIncrPort, 500*time.Millisecond, func(port int) {
url := fmt.Sprintf("http://localhost:%d", port)
fmt.Printf("Server listen at %s\n", url)

openURL(url)
}, func(port int) error {
return http.ListenAndServe(fmt.Sprintf(":%d", port), server)
})
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.36"
const REVISION = "1e1b9419279db2f75b8619256fced2e8a84e9ee7+1"
const NUMBER = 229
const REVISION = "48d5fefe9c2c051c940e088429f9253b80a65305+1"
const NUMBER = 230

func getRevision() string {
revSuffix := ""
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.36"
const REVISION = "1e1b9419279db2f75b8619256fced2e8a84e9ee7+1"
const NUMBER = 229
const REVISION = "48d5fefe9c2c051c940e088429f9253b80a65305+1"
const NUMBER = 230

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
77 changes: 39 additions & 38 deletions support/filecopy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,6 @@ import (
"testing"
)

func testCopyReplace(prepare func(rootDir string, srcDir string, dstDir string) error, check func(rootDir string, srcDir string, dstDir string) error) error {
tmpDir, err := os.MkdirTemp("", "copy-with-link")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

srcDir := filepath.Join(tmpDir, "src")
err = os.MkdirAll(srcDir, 0755)
if err != nil {
return err
}
dstDir := filepath.Join(tmpDir, "dst")

err = prepare(tmpDir, srcDir, dstDir)
if err != nil {
return err
}
err = CopyReplaceDir(srcDir, dstDir, false)
if err != nil {
return err
}
if check == nil {
return nil
}
return check(tmpDir, srcDir, dstDir)
}

func TestCopyWithSymLinkFiles(t *testing.T) {
// doc.txt
// src/
Expand Down Expand Up @@ -73,16 +45,6 @@ func TestCopyWithSymLinkFiles(t *testing.T) {
}
}

func checkIsSymLink(file string) (bool, error) {
finfo, err := os.Lstat(file)
if err != nil {
return false, err
}
if finfo.Mode()&fs.ModeSymlink != 0 {
return true, nil
}
return false, nil
}
func TestCopyWithSymLinkDirs(t *testing.T) {
// doc.txt
// src/
Expand Down Expand Up @@ -117,3 +79,42 @@ func TestCopyWithSymLinkDirs(t *testing.T) {
t.Fatal(err)
}
}

func checkIsSymLink(file string) (bool, error) {
finfo, err := os.Lstat(file)
if err != nil {
return false, err
}
if finfo.Mode()&fs.ModeSymlink != 0 {
return true, nil
}
return false, nil
}

func testCopyReplace(prepare func(rootDir string, srcDir string, dstDir string) error, check func(rootDir string, srcDir string, dstDir string) error) error {
tmpDir, err := os.MkdirTemp("", "copy-with-link")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

srcDir := filepath.Join(tmpDir, "src")
err = os.MkdirAll(srcDir, 0755)
if err != nil {
return err
}
dstDir := filepath.Join(tmpDir, "dst")

err = prepare(tmpDir, srcDir, dstDir)
if err != nil {
return err
}
err = CopyReplaceDir(srcDir, dstDir, false)
if err != nil {
return err
}
if check == nil {
return nil
}
return check(tmpDir, srcDir, dstDir)
}
7 changes: 6 additions & 1 deletion support/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ func ServePortHTTP(server *http.ServeMux, port int, autoIncrPort bool, watchTime
// suggested watch timeout: 500ms
func ServePort(port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int), doWithPort func(port int) error) error {
for {
serving, err := IsTCPAddrServing(net.JoinHostPort("localhost", strconv.Itoa(port)), 20*time.Millisecond)
addr := net.JoinHostPort("localhost", strconv.Itoa(port))
serving, err := IsTCPAddrServing(addr, 20*time.Millisecond)
if err != nil {
return err
}
if serving {
if !autoIncrPort {
return fmt.Errorf("bind %s failed: address in use", addr)
}
port++
continue
}

Expand Down

0 comments on commit ced2865

Please sign in to comment.