这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: switch to cobra #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
77ef773
feat: switch to cobra
samchouse Feb 20, 2022
c456c2e
feat: basic logger
samchouse Feb 22, 2022
921b52a
feat: refactor some
samchouse Feb 23, 2022
4de5326
more work with cobra
samchouse Feb 27, 2022
0ac8dcb
fix options and other stuff
samchouse Feb 28, 2022
6491a4f
go 17
samchouse Mar 2, 2022
3333e9b
formatting and skeleton commands for run & prune
samchouse Mar 2, 2022
cd616b9
update login command
samchouse Mar 2, 2022
69bf13b
Merge branch 'main' into feat/cobra
samchouse Mar 2, 2022
2a04ca5
Merge branch 'main' into feat/cobra
samchouse Mar 3, 2022
14b434a
start working on run
samchouse Mar 3, 2022
8d248f6
mostly implement run
samchouse Mar 3, 2022
de64b83
Merge branch 'main' into feat/cobra
samchouse Mar 3, 2022
e9b3317
signals
samchouse Mar 3, 2022
99ff12d
Merge branch 'main' of https://github.com/vercel/turborepo into feat/…
samchouse Mar 3, 2022
576867a
prune command
samchouse Mar 3, 2022
d07ad12
Merge branch 'main' into feat/cobra
samchouse Mar 3, 2022
4cbda3f
Merge branch 'main' into feat/cobra
samchouse Mar 4, 2022
c0f2e52
Merge branch 'main' into feat/cobra
samchouse Mar 4, 2022
a586322
Merge
samchouse Mar 8, 2022
d7bbc4f
refactoring
samchouse Mar 8, 2022
6146f2d
Merge branch 'main' of https://github.com/vercel/turborepo into feat/…
samchouse Mar 11, 2022
5b8389d
Merge branch 'main' of https://github.com/vercel/turborepo into feat/…
samchouse Mar 13, 2022
f83461b
add prefixed logger
samchouse Mar 13, 2022
cbaf1cb
Merge branch 'main' of https://github.com/vercel/turborepo into feat/…
samchouse Mar 13, 2022
11140b7
Merge branch 'main' of https://github.com/vercel/turborepo into feat/…
samchouse Mar 19, 2022
a50c38a
add utils to cmdutil
samchouse Mar 19, 2022
966b89a
testing
samchouse Mar 19, 2022
effd68a
refactor
samchouse Mar 19, 2022
e2ee807
Merge branch 'main' of https://github.com/vercel/turborepo into feat/…
samchouse Mar 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 8 additions & 156 deletions cli/cmd/turbo/main.go
Original file line number Diff line number Diff line change
@@ -1,179 +1,31 @@
package main

import (
"fmt"
"os"
"runtime/debug"
"strings"
"time"

"github.com/vercel/turborepo/cli/internal/config"
"github.com/vercel/turborepo/cli/internal/info"
"github.com/vercel/turborepo/cli/internal/login"
"github.com/hashicorp/go-hclog"
"github.com/vercel/turborepo/cli/internal/cmd"
"github.com/vercel/turborepo/cli/internal/process"
prune "github.com/vercel/turborepo/cli/internal/prune"
"github.com/vercel/turborepo/cli/internal/run"
"github.com/vercel/turborepo/cli/internal/ui"
uiPkg "github.com/vercel/turborepo/cli/internal/ui"
"github.com/vercel/turborepo/cli/internal/util"

"github.com/fatih/color"
hclog "github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
)

func main() {
args := os.Args[1:]
heapFile := ""
traceFile := ""
cpuprofileFile := ""
argsEnd := 0
for _, arg := range args {
switch {
case strings.HasPrefix(arg, "--heap="):
heapFile = arg[len("--heap="):]
case strings.HasPrefix(arg, "--trace="):
traceFile = arg[len("--trace="):]
case strings.HasPrefix(arg, "--cpuprofile="):
cpuprofileFile = arg[len("--cpuprofile="):]
default:
// Strip any arguments that were handled above
args[argsEnd] = arg
argsEnd++
}
}
args = args[:argsEnd]

c := cli.NewCLI("turbo", turboVersion)

util.InitPrintf()
ui := ui.Default()

c.Args = args
c.HelpWriter = os.Stdout
c.ErrorWriter = os.Stderr
// Parse and validate cmd line flags and env vars
// Note that cf can be nil
cf, err := config.ParseAndValidate(c.Args, ui, turboVersion)
if err != nil {
ui.Error(fmt.Sprintf("%s %s", uiPkg.ERROR_PREFIX, color.RedString(err.Error())))
os.Exit(1)
}

var logger hclog.Logger
if cf != nil {
logger = cf.Logger
} else {
logger = hclog.Default()
}
processes := process.NewManager(logger.Named("processes"))
signalCh := watchSignals(func() { processes.Close() })
c.HiddenCommands = []string{"graph"}
c.Commands = map[string]cli.CommandFactory{
"run": func() (cli.Command, error) {
return &run.RunCommand{Config: cf, Ui: ui, Processes: processes},
nil
},
"prune": func() (cli.Command, error) {
return &prune.PruneCommand{Config: cf, Ui: ui}, nil
},
"link": func() (cli.Command, error) {
return &login.LinkCommand{Config: cf, Ui: ui}, nil
},
"unlink": func() (cli.Command, error) {
return &login.UnlinkCommand{Config: cf, Ui: ui}, nil
},
"login": func() (cli.Command, error) {
return &login.LoginCommand{Config: cf, UI: ui}, nil
},
"logout": func() (cli.Command, error) {
return &login.LogoutCommand{Config: cf, Ui: ui}, nil
},
"bin": func() (cli.Command, error) {
return &info.BinCommand{Config: cf, Ui: ui}, nil
},
}

// Capture the defer statements below so the "done" message comes last
exitCode := 1
doneCh := make(chan struct{})
processes := process.NewManager(hclog.Default().Named("processes"))
signalCh := watchSignals(func() { processes.Close() })

func() {
defer func() { close(doneCh) }()
// To view a CPU trace, use "go tool trace [file]". Note that the trace
// viewer doesn't work under Windows Subsystem for Linux for some reason.
if traceFile != "" {
if done := createTraceFile(args, traceFile); done == nil {
return
} else {
defer done()
}
}

// To view a heap trace, use "go tool pprof [file]" and type "top". You can
// also drop it into https://speedscope.app and use the "left heavy" or
// "sandwich" view modes.
if heapFile != "" {
if done := createHeapFile(args, heapFile); done == nil {
return
} else {
defer done()
}
}

// To view a CPU profile, drop the file into https://speedscope.app.
// Note: Running the CPU profiler doesn't work under Windows subsystem for
// Linux. The profiler has to be built for native Windows and run using the
// command prompt instead.
if cpuprofileFile != "" {
if done := createCpuprofileFile(args, cpuprofileFile); done == nil {
return
} else {
defer done()
}
}

if cpuprofileFile != "" {
// The CPU profiler in Go only runs at 100 Hz, which is far too slow to
// return useful information for esbuild, since it's so fast. Let's keep
// running for 30 seconds straight, which should give us 3,000 samples.
seconds := 30.0
start := time.Now()
for time.Since(start).Seconds() < seconds {
exitCode, err = c.Run()
if err != nil {
ui.Error(err.Error())
}
}
} else {
// Don't disable the GC if this is a long-running process
isServe := false
for _, arg := range args {
if arg == "--no-gc" {
isServe = true
break
}
}

// Disable the GC since we're just going to allocate a bunch of memory
// and then exit anyway. This speedup is not insignificant. Make sure to
// only do this here once we know that we're not going to be a long-lived
// process though.
if !isServe {
debug.SetGCPercent(-1)
}

exitCode, err = c.Run()
if err != nil {
ui.Error(err.Error())
}
}
exitCode = cmd.Execute(turboVersion, processes)
}()

// Wait for either our command to finish, in which case we need to clean up,
// or to receive a signal, in which case the signal handler above does the cleanup
select {
case <-doneCh:
processes.Close()
case <-signalCh:
}

os.Exit(exitCode)
}
39 changes: 24 additions & 15 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,40 +1,49 @@
module github.com/vercel/turborepo/cli

go 1.16
go 1.17

require (
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/AlecAivazis/survey/v2 v2.3.2
github.com/Masterminds/semver v1.5.0
github.com/adrg/xdg v0.3.3
github.com/adrg/xdg v0.4.0
github.com/bmatcuk/doublestar/v4 v4.0.2
github.com/briandowns/spinner v1.16.0
github.com/deckarep/golang-set v1.7.1
github.com/briandowns/spinner v1.18.1
github.com/deckarep/golang-set v1.8.0
github.com/fatih/color v1.13.0
github.com/gobwas/glob v0.2.3
github.com/google/chrometracing v0.0.0-20210413150014-55fded0163e7
github.com/google/chrometracing v0.0.0-20210820115312-9b2483a9dc7d
github.com/google/uuid v1.3.0
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-gatedio v0.5.0
github.com/hashicorp/go-hclog v1.1.0
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/karrick/godirwalk v1.16.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.14
github.com/mitchellh/cli v1.1.2
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.4.3
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1
github.com/pyr-sh/dag v1.0.0
github.com/sabhiram/go-gitignore v0.0.0-20201211210132-54b8a0bf510f
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/sourcegraph/go-diff v0.6.1
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.7.0
github.com/yosuke-furukawa/json5 v0.1.1
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
golang.org/x/text v0.3.7 // indirect
)
Loading