sourcegraph/dev/sg/sg_version.go
Bolaji Olajide 4d57eb1188
fix(sg): make sg gen output more readable (#64406)
Closes DINF-78

The output of `sg gen` is a bit hard to read when there's an error, this
is because the new line character `\n` isn't rendered as a new line. It
turns out the `%q` formatting directive used to quote a string doesn't
render the `\n` character as a new line.

| Before |
|---|
| ![CleanShot 2024-08-12 at 11 17
57@2x](https://github.com/user-attachments/assets/e03ec503-e437-4b68-80b3-fe34ac8848fb)
|

| After  |
|---|
| ![CleanShot 2024-08-12 at 10 53
35@2x](https://github.com/user-attachments/assets/5b7aac63-27b6-4de0-9c56-3b739f0ee0f9)
|

I also added a func to extract error messages from a bazel command to
avoid long output message when a bazel command fails and give the user
relevant messages related to the error.

| Before  |
|---|


https://github.com/user-attachments/assets/2d029ec1-5804-41bf-a675-8642e169ea80


| After  |
|---|
| ![CleanShot 2024-08-12 at 14 45
59@2x](https://github.com/user-attachments/assets/7d567fd6-de37-48aa-b2b5-03dc591fc77a)
|

## Test plan

<!-- REQUIRED; info at
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->

* Manual testing

## Changelog

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
2024-08-13 06:22:21 -05:00

113 lines
3.1 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/urfave/cli/v2"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/run"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
)
var (
versionChangelogNext bool
versionChangelogEntries int
versionCommand = &cli.Command{
Name: "version",
Usage: "View details for this installation of sg",
Action: versionExec,
Category: category.Util,
Subcommands: []*cli.Command{
{
Name: "changelog",
Aliases: []string{"c"},
Usage: "See what's changed in or since this version of sg",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "next",
Usage: "Show changelog for changes you would get if you upgrade.",
Destination: &versionChangelogNext,
},
&cli.IntFlag{
Name: "limit",
Usage: "Number of changelog entries to show.",
Value: 5,
Destination: &versionChangelogEntries,
},
},
Action: changelogExec,
},
},
}
)
func versionExec(c *cli.Context) error {
// Write on stderr to ensure we can use the output in scripts without having to trim
// the output from the contextual infos.
outErr := std.NewOutput(os.Stderr, verbose)
outErr.WriteNoticef("Showing the current version of the sg CLI, if you're looking for deployed Sourcegraph instances version, please use `sg live` instead.")
if verbose {
std.Out.Writef("Version: %s\nBuild commit: %s",
c.App.Version, BuildCommit)
} else {
std.Out.Write(c.App.Version)
}
return nil
}
func changelogExec(ctx *cli.Context) error {
if _, err := run.GitCmd("fetch", "origin", "main"); err != nil {
return errors.Newf("failed to update main: %s", err)
}
logArgs := []string{
// Format nicely
"log", "--pretty=%C(reset)%s %C(dim)%h by %an, %ar",
"--color=always",
// Filter out stuff we don't want
"--no-merges",
// Limit entries
fmt.Sprintf("--max-count=%d", versionChangelogEntries),
}
var title string
if BuildCommit != "dev" {
current := strings.TrimPrefix(BuildCommit, "dev-")
if versionChangelogNext {
logArgs = append(logArgs, current+"..origin/main")
title = fmt.Sprintf("Changes since sg release %s", ReleaseName)
} else {
logArgs = append(logArgs, current)
title = fmt.Sprintf("Changes in sg release %s", ReleaseName)
}
} else {
std.Out.WriteWarningf("Dev version detected - just showing recent changes.")
title = "Recent sg changes"
}
gitLog := exec.Command("git", append(logArgs, "--", "./dev/sg")...)
gitLog.Env = os.Environ()
out, err := run.InRoot(gitLog, run.InRootArgs{})
if err != nil {
return err
}
block := std.Out.Block(output.Styled(output.StyleSearchQuery, title))
if len(out) == 0 {
block.Write("No changes found.")
} else {
block.Write(out + "...")
}
block.Close()
std.Out.WriteLine(output.Styledf(output.StyleSuggestion,
"Only showing %d entries - configure with 'sg version changelog -limit=50'", versionChangelogEntries))
return nil
}