Skip to content

Commit f4ff6ca

Browse files
committed
migrate version to use urface cli
1 parent 09b487b commit f4ff6ca

1 file changed

Lines changed: 45 additions & 36 deletions

File tree

cmd/src/version.go

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,72 @@ package main
33
import (
44
"context"
55
"encoding/json"
6-
"flag"
76
"fmt"
87
"io"
98
"net/http"
9+
"os"
1010

1111
"github.com/sourcegraph/sourcegraph/lib/errors"
1212

1313
"github.com/sourcegraph/src-cli/internal/api"
14+
"github.com/sourcegraph/src-cli/internal/clicompat"
1415
"github.com/sourcegraph/src-cli/internal/version"
16+
17+
"github.com/urfave/cli/v3"
1518
)
1619

17-
func init() {
18-
usage := `
19-
Examples:
20+
const versionExamples = `Examples:
2021
2122
Get the src-cli version and the Sourcegraph instance's recommended version:
2223
2324
$ src version
2425
`
2526

26-
flagSet := flag.NewFlagSet("version", flag.ExitOnError)
27-
28-
var (
29-
clientOnly = flagSet.Bool("client-only", false, "If true, only the client version will be printed.")
30-
apiFlags = api.NewFlags(flagSet)
31-
)
32-
33-
handler := func(args []string) error {
34-
fmt.Printf("Current version: %s\n", version.BuildTag)
35-
if clientOnly != nil && *clientOnly {
36-
return nil
27+
var versionCommandv2 = clicompat.WithLegacyCommandHelp(&cli.Command{
28+
Name: "version",
29+
Usage: "display and compare the src-cli version against the recommended version for your instance",
30+
UsageText: "src version [options]",
31+
Description: `
32+
` + versionExamples,
33+
Flags: clicompat.WithAPIFlags(
34+
&cli.BoolFlag{
35+
Name: "client-only",
36+
Usage: "If true, only the client version will be printed.",
37+
},
38+
),
39+
HideVersion: true,
40+
Action: func(ctx context.Context, c *cli.Command) error {
41+
args := VersionArgs{
42+
Client: cfg.apiClient(clicompat.APIFlagsFromContext(c), os.Stdout),
43+
ClientOnly: c.Bool("client-only"),
3744
}
45+
return versionHandler(args)
46+
},
47+
})
48+
49+
type VersionArgs struct {
50+
ClientOnly bool
51+
Client api.Client
52+
Output io.Writer
53+
}
3854

39-
client := cfg.apiClient(apiFlags, flagSet.Output())
40-
recommendedVersion, err := getRecommendedVersion(context.Background(), client)
41-
if err != nil {
42-
return errors.Wrap(err, "failed to get recommended version for Sourcegraph deployment")
43-
}
44-
if recommendedVersion == "" {
45-
fmt.Println("Recommended version: <unknown>")
46-
fmt.Println("This Sourcegraph instance does not support this feature.")
47-
return nil
48-
}
49-
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
55+
func versionHandler(args VersionArgs) error {
56+
fmt.Printf("Current version: %s\n", version.BuildTag)
57+
if args.ClientOnly {
5058
return nil
5159
}
5260

53-
// Register the command.
54-
commands = append(commands, &command{
55-
flagSet: flagSet,
56-
handler: handler,
57-
usageFunc: func() {
58-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src %s':\n", flagSet.Name())
59-
flagSet.PrintDefaults()
60-
fmt.Println(usage)
61-
},
62-
})
61+
recommendedVersion, err := getRecommendedVersion(context.Background(), args.Client)
62+
if err != nil {
63+
return errors.Wrap(err, "failed to get recommended version for Sourcegraph deployment")
64+
}
65+
if recommendedVersion == "" {
66+
fmt.Println("Recommended version: <unknown>")
67+
fmt.Println("This Sourcegraph instance does not support this feature.")
68+
return nil
69+
}
70+
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
71+
return nil
6372
}
6473

6574
func getRecommendedVersion(ctx context.Context, client api.Client) (string, error) {

0 commit comments

Comments
 (0)