Skip to content
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

feat: Add todos-profile command #412

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Go pprof files
*.prof

# binary names
todos
todos-profile

# Dependency directories (remove the comment below to include it)
vendor/
node_modules/
48 changes: 48 additions & 0 deletions internal/cmd/todos-profile/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"
"runtime/pprof"

"github.com/urfave/cli/v2"

"github.com/ianlewis/todos/internal/cmd/todos/app"
"github.com/ianlewis/todos/internal/utils"
)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a todos-profile command comment.

func main() {
f, err := os.Create("todos.prof")
if err != nil {
_ = utils.Must(fmt.Fprintf(os.Stderr, "%v", err))
os.Exit(1)
}
if err := pprof.StartCPUProfile(f); err != nil {
_ = utils.Must(fmt.Fprintf(os.Stderr, "%v", err))
os.Exit(1)
}
defer pprof.StopCPUProfile()

exit := cli.OsExiter
cli.OsExiter = func(code int) {
// Make sure profile is stopped even if os.Exit is called.
pprof.StopCPUProfile()
exit(code)
}

app.Main()
}
17 changes: 14 additions & 3 deletions internal/cmd/todos/app.go → internal/cmd/todos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"encoding/json"
Expand Down Expand Up @@ -55,8 +55,8 @@ var (
ErrWalk = errors.New("walking")
)

// newTODOsApp returns a new `todos` application.
func newTODOsApp() *cli.App {
// NewTODOsApp returns a new `todos` application.
func NewTODOsApp() *cli.App {
return &cli.App{
Name: filepath.Base(os.Args[0]),
Usage: "Search for TODOS in code.",
Expand Down Expand Up @@ -288,3 +288,14 @@ func walkerOptionsFromContext(c *cli.Context) (*walker.Options, error) {

return &o, nil
}

// Main is the main function of the `todos` command.
func Main() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very simple Main test might be good.

// NOTE: Errors are generally handled in the app itself but Run could
// return errors if command line flags are incorrect etc. In this case neither
// Action nor ExitErrHandler are called.
todosApp := NewTODOsApp()
if err := todosApp.Run(os.Args); err != nil {
cli.OsExiter(ExitCodeUnknownError)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

import (
"bytes"
Expand Down Expand Up @@ -47,7 +47,7 @@ func newContext(app *cli.App, args []string) *cli.Context {
func Test_TODOsApp_version(t *testing.T) {
t.Parallel()

app := newTODOsApp()
app := NewTODOsApp()
var b strings.Builder
app.Writer = &b
c := newContext(app, []string{"--version"})
Expand Down Expand Up @@ -80,7 +80,7 @@ func Test_TODOsApp_Walk(t *testing.T) {
d := testutils.NewTempDir(files)
defer d.Cleanup()

app := newTODOsApp()
app := NewTODOsApp()
var b strings.Builder
app.Writer = &b
c := newContext(app, []string{d.Dir()})
Expand All @@ -106,7 +106,7 @@ func Test_TODOsApp_ExitErrHandler_ErrWalk(t *testing.T) {
cli.OsExiter = oldExiter
}()

app := newTODOsApp()
app := NewTODOsApp()
var b strings.Builder
app.ErrWriter = &b
c := newContext(app, nil)
Expand Down
12 changes: 2 additions & 10 deletions internal/cmd/todos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
package main

import (
"os"

"github.com/urfave/cli/v2"
"github.com/ianlewis/todos/internal/cmd/todos/app"
)

func main() {
// NOTE: Errors are generally handled in the app itself but Run could
// return errors if command line flags are incorrect etc. In this case neither
// Action nor ExitErrHandler are called.
app := newTODOsApp()
if err := app.Run(os.Args); err != nil {
cli.OsExiter(ExitCodeUnknownError)
}
app.Main()
}