-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathversion.go
More file actions
108 lines (90 loc) · 2.48 KB
/
version.go
File metadata and controls
108 lines (90 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/sourcegraph/src-cli/internal/version"
"github.com/urfave/cli/v3"
)
const versionExamples = `Examples:
Get the src-cli version and the Sourcegraph instance's recommended version:
$ src version
`
var versionCommand = clicompat.Wrap(&cli.Command{
Name: "version",
Usage: "display and compare the src-cli version against the recommended version for your instance",
UsageText: "src version [options]",
OnUsageError: clicompat.OnUsageError,
Description: `
` + versionExamples,
Flags: clicompat.WithAPIFlags(
&cli.BoolFlag{
Name: "client-only",
Usage: "If true, only the client version will be printed.",
},
),
HideVersion: true,
Action: func(ctx context.Context, c *cli.Command) error {
args := VersionArgs{
Client: cfg.apiClient(clicompat.APIFlagsFromCmd(c), os.Stdout),
ClientOnly: c.Bool("client-only"),
}
return versionHandler(args)
},
})
type VersionArgs struct {
ClientOnly bool
Client api.Client
Output io.Writer
}
func versionHandler(args VersionArgs) error {
fmt.Printf("Current version: %s\n", version.BuildTag)
if args.ClientOnly {
return nil
}
recommendedVersion, err := getRecommendedVersion(context.Background(), args.Client)
if err != nil {
return errors.Wrap(err, "failed to get recommended version for Sourcegraph deployment")
}
if recommendedVersion == "" {
fmt.Println("Recommended version: <unknown>")
fmt.Println("This Sourcegraph instance does not support this feature.")
return nil
}
fmt.Printf("Recommended version: %s or later\n", recommendedVersion)
return nil
}
func getRecommendedVersion(ctx context.Context, client api.Client) (string, error) {
req, err := client.NewHTTPRequest(ctx, "GET", ".api/src-cli/version", nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return "", nil
}
return "", fmt.Errorf("error: %s\n\n%s", resp.Status, body)
}
payload := struct {
Version string `json:"version"`
}{}
if err := json.Unmarshal(body, &payload); err != nil {
return "", err
}
return payload.Version, nil
}