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

[sg] Upgrade urfave/cli from v2 to v3 #61239

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .bazel_fix_commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
{
"regex": "^Check that imports in Go sources match importpath attributes in deps.$",
"command": "sg",
"args": ["bazel", "configure"]
"args": ["--skip-auto-update", "bazel", "configure"]
},
{
"regex": "missing input file",
"command": "sg",
"args": ["bazel", "configure"]
"args": ["--skip-auto-update", "bazel", "configure"]
},
{
"regex": ": undefined:",
"command": "sg",
"args": ["bazel", "configure"]
"args": ["--skip-auto-update", "bazel", "configure"]
}
]
14 changes: 14 additions & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5587,13 +5587,27 @@ def go_dependencies():
sum = "h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8=",
version = "v1.22.12",
)
go_repository(
name = "com_github_urfave_cli_altsrc_v3",
build_file_proto_mode = "disable_global",
importpath = "github.com/urfave/cli-altsrc/v3",
sum = "h1:j4SaBpPB8++L0c0KuTnz/Yus3UQoWJ54hQjhIMW8rCM=",
version = "v3.0.0-alpha2",
)
go_repository(
name = "com_github_urfave_cli_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/urfave/cli/v2",
sum = "h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=",
version = "v2.25.7",
)
go_repository(
name = "com_github_urfave_cli_v3",
build_file_proto_mode = "disable_global",
importpath = "github.com/urfave/cli/v3",
sum = "h1:P0RMy5fQm1AslQS+XCmy9UknDXctOmG/q/FZkUFnJSo=",
version = "v3.0.0-alpha9",
)
go_repository(
name = "com_github_urfave_negroni",
build_file_proto_mode = "disable_global",
Expand Down
4 changes: 2 additions & 2 deletions dev/sg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ go_library(
"@com_github_sourcegraph_conc//pool",
"@com_github_sourcegraph_log//:log",
"@com_github_sourcegraph_run//:run",
"@com_github_urfave_cli_v2//:cli",
"@com_github_urfave_cli_v3//:cli",
"@in_gopkg_yaml_v3//:yaml_v3",
"@io_opentelemetry_go_otel//attribute",
"@io_opentelemetry_go_otel_trace//:trace",
Expand Down Expand Up @@ -160,6 +160,6 @@ go_test(
"//lib/output/outputtest",
"@com_github_google_go_cmp//cmp",
"@com_github_stretchr_testify//assert",
"@com_github_urfave_cli_v2//:cli",
"@com_github_urfave_cli_v3//:cli",
],
)
13 changes: 7 additions & 6 deletions dev/sg/analytics.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"context"
"fmt"
"runtime"
"strings"

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

Expand All @@ -21,8 +22,8 @@ import (
func addAnalyticsHooks(commandPath []string, commands []*cli.Command) {
for _, command := range commands {
fullCommandPath := append(commandPath, command.Name)
if len(command.Subcommands) > 0 {
addAnalyticsHooks(fullCommandPath, command.Subcommands)
if len(command.Commands) > 0 {
addAnalyticsHooks(fullCommandPath, command.Commands)
}

// No action to perform analytics on
Expand All @@ -35,9 +36,9 @@ func addAnalyticsHooks(commandPath []string, commands []*cli.Command) {

// Wrap action with analytics
wrappedAction := command.Action
command.Action = func(cmd *cli.Context) (actionErr error) {
command.Action = func(ctx context.Context, cmd *cli.Command) (actionErr error) {
var span *analytics.Span
cmd.Context, span = analytics.StartSpan(cmd.Context, fullCommand, "action",
ctx, span = analytics.StartSpan(ctx, fullCommand, "action",
trace.WithAttributes(
attribute.StringSlice("flags", cmd.FlagNames()),
attribute.Int("args", cmd.NArg()),
Expand All @@ -63,7 +64,7 @@ func addAnalyticsHooks(commandPath []string, commands []*cli.Command) {
})

// Call the underlying action
actionErr = wrappedAction(cmd)
actionErr = wrappedAction(ctx, cmd)

// Capture analytics post-run
if actionErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion dev/sg/ci/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ go_library(
"@com_github_google_uuid//:uuid",
"@com_github_grafana_regexp//:regexp",
"@com_github_sourcegraph_run//:run",
"@com_github_urfave_cli_v2//:cli",
"@com_github_urfave_cli_v3//:cli",
],
)
6 changes: 3 additions & 3 deletions dev/sg/ci/buildkite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/buildkite/go-buildkite/v3/buildkite"
"github.com/gen2brain/beeep"
sgrun "github.com/sourcegraph/run"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"

"github.com/sourcegraph/sourcegraph/dev/ci/runtype"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/bk"
Expand Down Expand Up @@ -50,7 +50,7 @@ type targetBuild struct {
// Buildkite build.
//
// Requires ciBranchFlag and ciBuildFlag to be registered on the command.
func getBuildTarget(cmd *cli.Context) (target targetBuild, err error) {
func getBuildTarget(ctx context.Context, cmd *cli.Command) (target targetBuild, err error) {
target.pipeline = ciPipelineFlag.Get(cmd)
if target.pipeline == "" {
target.pipeline = "sourcegraph"
Expand All @@ -77,7 +77,7 @@ func getBuildTarget(cmd *cli.Context) (target targetBuild, err error) {

case commit != "":
// get the full commit
target.target, err = root.Run(sgrun.Cmd(cmd.Context, "git rev-parse", commit)).String()
target.target, err = root.Run(sgrun.Cmd(ctx, "git rev-parse", commit)).String()
if err != nil {
return
}
Expand Down
7 changes: 4 additions & 3 deletions dev/sg/ci/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ci

import (
"github.com/urfave/cli/v3"

"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
"github.com/urfave/cli/v2"
)

var (
Expand All @@ -24,7 +25,7 @@ var (
ciPipelineFlag = cli.StringFlag{
Name: "pipeline",
Aliases: []string{"p"},
EnvVars: []string{"SG_CI_PIPELINE"},
Sources: cli.EnvVars("SG_CI_PIPELINE"),
Usage: "Select a custom Buildkite `pipeline` in the Sourcegraph org",
Value: "sourcegraph",
}
Expand Down Expand Up @@ -77,7 +78,7 @@ sg ci build --force --commit my-commit main-dry-run
sg ci build --help
`,
Category: category.Dev,
Subcommands: []*cli.Command{
Commands: []*cli.Command{
previewCommand,
bazelCommand,
statusCommand,
Expand Down
55 changes: 27 additions & 28 deletions dev/sg/ci/subcommands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ci

import (
"context"
"encoding/json"
"fmt"
"net/url"
Expand All @@ -14,7 +15,7 @@ import (
"github.com/google/uuid"
"github.com/grafana/regexp"
sgrun "github.com/sourcegraph/run"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"

"github.com/sourcegraph/sourcegraph/dev/ci/runtype"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/bk"
Expand Down Expand Up @@ -43,11 +44,11 @@ var previewCommand = &cli.Command{
Value: "markdown",
},
},
Action: func(cmd *cli.Context) error {
Action: func(ctx context.Context, cmd *cli.Command) error {
std.Out.WriteLine(output.Styled(output.StyleSuggestion,
"If the current branch were to be pushed, the following pipeline would be run:"))

target, err := getBuildTarget(cmd)
target, err := getBuildTarget(ctx, cmd)
if err != nil {
return err
}
Expand All @@ -68,23 +69,23 @@ var previewCommand = &cli.Command{
}
switch cmd.String("format") {
case "markdown":
previewCmd = usershell.Command(cmd.Context, "go run ./dev/ci/gen-pipeline.go -preview").
previewCmd = usershell.Command(ctx, "go run ./dev/ci/gen-pipeline.go -preview").
Env(env)
out, err := root.Run(previewCmd).String()
if err != nil {
return err
}
return std.Out.WriteMarkdown(out)
case "json":
previewCmd = usershell.Command(cmd.Context, "go run ./dev/ci/gen-pipeline.go").
previewCmd = usershell.Command(ctx, "go run ./dev/ci/gen-pipeline.go").
Env(env)
out, err := root.Run(previewCmd).String()
if err != nil {
return err
}
return std.Out.WriteCode("json", out)
case "yaml":
previewCmd = usershell.Command(cmd.Context, "go run ./dev/ci/gen-pipeline.go -yaml").
previewCmd = usershell.Command(ctx, "go run ./dev/ci/gen-pipeline.go -yaml").
Env(env)
out, err := root.Run(previewCmd).String()
if err != nil {
Expand Down Expand Up @@ -118,7 +119,7 @@ var bazelCommand = &cli.Command{
Value: false,
},
},
Action: func(cmd *cli.Context) (err error) {
Action: func(ctx context.Context, cmd *cli.Command) (err error) {
args := cmd.Args().Slice()

if !cmd.Bool("staged") {
Expand Down Expand Up @@ -170,11 +171,11 @@ var bazelCommand = &cli.Command{

// give buildkite some time to kick off the build so that we can find it later on
time.Sleep(10 * time.Second)
client, err := bk.NewClient(cmd.Context, std.Out)
client, err := bk.NewClient(ctx, std.Out)
if err != nil {
return err
}
build, err := client.TriggerBuild(cmd.Context, "sourcegraph", branch, commit, bk.WithEnvVar("DISABLE_ASPECT_WORKFLOWS", "true"))
build, err := client.TriggerBuild(ctx, "sourcegraph", branch, commit, bk.WithEnvVar("DISABLE_ASPECT_WORKFLOWS", "true"))
if err != nil {
return err
}
Expand All @@ -187,7 +188,7 @@ var bazelCommand = &cli.Command{

if cmd.Bool("wait") {
pending := std.Out.Pending(output.Styledf(output.StylePending, "Waiting for %d jobs...", len(build.Jobs)))
err = statusTicker(cmd.Context, fetchJobs(cmd.Context, client, &build, pending))
err = statusTicker(ctx, fetchJobs(ctx, client, &build, pending))
if err != nil {
return err
}
Expand All @@ -196,7 +197,7 @@ var bazelCommand = &cli.Command{
options := bk.ExportLogsOpts{
JobStepKey: "bazel-do",
}
logs, err := client.ExportLogs(cmd.Context, "sourcegraph", *build.Number, options)
logs, err := client.ExportLogs(ctx, "sourcegraph", *build.Number, options)
if err != nil {
return err
}
Expand Down Expand Up @@ -234,18 +235,18 @@ var statusCommand = &cli.Command{
Aliases: []string{"view", "w"},
Usage: "Open build page in web browser (--view is DEPRECATED and will be removed in the future)",
}),
Action: func(cmd *cli.Context) error {
client, err := bk.NewClient(cmd.Context, std.Out)
Action: func(ctx context.Context, cmd *cli.Command) error {
client, err := bk.NewClient(ctx, std.Out)
if err != nil {
return err
}
target, err := getBuildTarget(cmd)
target, err := getBuildTarget(ctx, cmd)
if err != nil {
return err
}

// Just support main pipeline for now
build, err := target.GetBuild(cmd.Context, client)
build, err := target.GetBuild(ctx, client)
if err != nil {
return err
}
Expand All @@ -265,7 +266,7 @@ var statusCommand = &cli.Command{
}

pending := std.Out.Pending(output.Styledf(output.StylePending, "Waiting for %d jobs...", len(build.Jobs)))
err := statusTicker(cmd.Context, fetchJobs(cmd.Context, client, &build, pending))
err := statusTicker(ctx, fetchJobs(ctx, client, &build, pending))
pending.Destroy()
if err != nil {
return err
Expand All @@ -274,7 +275,7 @@ var statusCommand = &cli.Command{

// lets get annotations (if any) for the build
var annotations bk.JobAnnotations
annotations, err = client.GetJobAnnotationsByBuildNumber(cmd.Context, "sourcegraph", strconv.Itoa(*build.Number))
annotations, err = client.GetJobAnnotationsByBuildNumber(ctx, "sourcegraph", strconv.Itoa(*build.Number))
if err != nil {
return errors.Newf("failed to get annotations for build %d: %w", *build.Number, err)
}
Expand Down Expand Up @@ -339,7 +340,7 @@ sg ci build docker-images-patch-notest prometheus
# Publish all images without testing
sg ci build docker-images-candidates-notest
`,
BashComplete: completions.CompleteArgs(getAllowedBuildTypeArgs),
ShellComplete: completions.CompleteArgs(getAllowedBuildTypeArgs),
Flags: []cli.Flag{
&ciPipelineFlag,
&cli.StringFlag{
Expand All @@ -348,8 +349,7 @@ sg ci build docker-images-candidates-notest
Usage: "`commit` from the current branch to build (defaults to current commit)",
},
},
Action: func(cmd *cli.Context) error {
ctx := cmd.Context
Action: func(ctx context.Context, cmd *cli.Command) error {
client, err := bk.NewClient(ctx, std.Out)
if err != nil {
return err
Expand Down Expand Up @@ -488,14 +488,13 @@ From there, you can start exploring logs with the Grafana explore panel.
Usage: "`state` to overwrite the job state metadata",
},
),
Action: func(cmd *cli.Context) error {
ctx := cmd.Context
Action: func(ctx context.Context, cmd *cli.Command) error {
client, err := bk.NewClient(ctx, std.Out)
if err != nil {
return err
}

target, err := getBuildTarget(cmd)
target, err := getBuildTarget(ctx, cmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -632,9 +631,9 @@ From there, you can start exploring logs with the Grafana explore panel.
var docsCommand = &cli.Command{
Name: "docs",
Usage: "Render reference documentation for build pipeline types",
Action: func(ctx *cli.Context) error {
cmd := exec.Command("go", "run", "./dev/ci/gen-pipeline.go", "-docs")
out, err := run.InRoot(cmd)
Action: func(ctx context.Context, cmd *cli.Command) error {
exe := exec.Command("go", "run", "./dev/ci/gen-pipeline.go", "-docs")
out, err := run.InRoot(exe)
if err != nil {
return err
}
Expand All @@ -646,9 +645,9 @@ var openCommand = &cli.Command{
Name: "open",
ArgsUsage: "[pipeline]",
Usage: "Open Sourcegraph's Buildkite page in browser",
Action: func(ctx *cli.Context) error {
Action: func(ctx context.Context, cmd *cli.Command) error {
buildkiteURL := fmt.Sprintf("https://buildkite.com/%s", bk.BuildkiteOrg)
args := ctx.Args().Slice()
args := cmd.Args().Slice()
if len(args) > 0 && args[0] != "" {
pipeline := args[0]
buildkiteURL += fmt.Sprintf("/%s", pipeline)
Expand Down
2 changes: 1 addition & 1 deletion dev/sg/internal/backport/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ go_library(
"//dev/sg/internal/std",
"//lib/errors",
"//lib/output",
"@com_github_urfave_cli_v2//:cli",
"@com_github_urfave_cli_v3//:cli",
],
)