diff --git a/cmd/root.go b/cmd/root.go index a99feff..b369a19 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,8 +1,10 @@ +//go:build !desktop // +build !desktop package cmd import ( + "context" "fmt" stdlog "log" "os" @@ -61,3 +63,16 @@ func Execute() { os.Exit(1) } } + +// GoExecute runs command parsing chain from go. +// Look example ../example/main.go. +func GoExecute(ctx context.Context, version, commit, mode string, args ...string) error { + config.Config.Version = version + config.Config.CommitHash = commit + config.Config.ClientMode = mode + + rootCmd.Version = fmt.Sprintf("%s (%s)", config.Config.Version, config.Config.CommitHash) + + rootCmd.SetArgs(args) + return rootCmd.ExecuteContext(ctx) +} diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..089fa95 --- /dev/null +++ b/example/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "context" + "fmt" + "time" + + "github.com/loophole/cli/cmd" +) + +func main() { + fmt.Println("Master func") + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + fmt.Println("Started loophole tunnel to 127.0.0.1:80") + go cmd.GoExecute(ctx, "development", "unknown", "cli", "--hostname", "slave", "http", "80") + + fmt.Println("Started local http server on 127.0.0.1:80") + time.Sleep(time.Second * 30) // Mock server works 30s + fmt.Println("Server stopped") +}