Skip to content

Commit db4bd1e

Browse files
CLOUDP-114066: [MongoCLI] The json file of the download center doesn’t contain the release version (#1008)
1 parent dc613ff commit db4bd1e

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

build/ci/release.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ functions:
151151
export GOROOT="${go_root}"
152152
export PATH="./bin:${go_bin}:$PATH"
153153
154-
VERSION=$(git describe | cut -d "v" -f 2)
155-
156-
go run ../internal/release/main.go "${VERSION}" "${FEED_FILE_NAME}"
154+
go run ../internal/release/main.go --version "$(git describe | cut -d "v" -f 2)" --file "${FEED_FILE_NAME}"
157155
"upload dist":
158156
- command: s3.put
159157
params:

internal/flag/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,5 @@ const (
270270
WithoutDefaultAlertSettings = "withoutDefaultAlertSettings" // WithoutDefaultAlertSettings flag
271271
CurrentIP = "currentIp" // CurrentIP flag
272272
Gov = "gov" // Gov flag
273+
Version = "version" // Version flag
273274
)

internal/release/main.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ import (
2020
"os"
2121
"strings"
2222
"time"
23+
24+
"github.com/mongodb/mongocli/internal/flag"
25+
"github.com/spf13/cobra"
2326
)
2427

2528
const mongocli = "mongocli"
2629

30+
type Opts struct {
31+
fileName string
32+
version string
33+
}
34+
2735
type DownloadArchive struct {
2836
PreviousReleasesLink string `json:"previous_releases_link"`
2937
ReleaseDate time.Time `json:"release_date"`
@@ -51,21 +59,21 @@ type Link struct {
5159
Name string `json:"name"`
5260
}
5361

54-
func newPlatform(tool, version, arch, system, distro string, formats []string) *Platform {
62+
func newPlatform(packageName, version, arch, system, distro string, formats []string) *Platform {
5563
p := &Platform{}
5664
p.Arch = arch
5765
p.OS = distro
5866

5967
links := make([]Link, len(formats))
6068
for i, f := range formats {
6169
links[i] = Link{
62-
DownloadLink: fmt.Sprintf("https://fastdl.mongodb.org/mongocli/%s_%s_%s_%s.%s", tool, version, system, arch, f),
70+
DownloadLink: fmt.Sprintf("https://fastdl.mongodb.org/mongocli/%s_%s_%s_%s.%s", packageName, version, system, arch, f),
6371
Name: f,
6472
}
6573
}
6674

6775
title := "MongoDB Atlas CLI"
68-
if tool == mongocli {
76+
if packageName == mongocli {
6977
title = "MongoDB CLI"
7078
}
7179
p.Packages = Package{
@@ -76,17 +84,11 @@ func newPlatform(tool, version, arch, system, distro string, formats []string) *
7684
}
7785

7886
func main() {
79-
version := os.Args[1]
80-
feedFilename := os.Args[2]
81-
fmt.Printf("Generating JSON: %s\n", feedFilename)
82-
err := generateFile(feedFilename, version)
83-
84-
if err != nil {
87+
cmd := Builder()
88+
if err := cmd.Execute(); err != nil {
8589
fmt.Printf("error encoding file: %v\n", err)
86-
8790
os.Exit(1)
8891
}
89-
fmt.Printf("File %s has been generated\n", feedFilename)
9092
}
9193

9294
func generateFile(name, version string) error {
@@ -119,3 +121,28 @@ func generateFile(name, version string) error {
119121
jsonEncoder.SetIndent("", " ")
120122
return jsonEncoder.Encode(downloadArchive)
121123
}
124+
125+
func Builder() *cobra.Command {
126+
opts := Opts{}
127+
cmd := &cobra.Command{
128+
Use: "main",
129+
Short: "Generate the download center json file",
130+
Example: `
131+
Generate the download center json file for mongocli
132+
$ main --version 1.23.0 --file mongocli.json`,
133+
RunE: func(cmd *cobra.Command, args []string) error {
134+
fmt.Printf("Generating JSON: %s\n", opts.fileName)
135+
return generateFile(opts.fileName, opts.version)
136+
},
137+
PostRun: func(cmd *cobra.Command, args []string) {
138+
fmt.Printf("File %s has been generated\n", opts.fileName)
139+
},
140+
}
141+
142+
cmd.Flags().StringVar(&opts.version, flag.Version, "", "release version.")
143+
cmd.Flags().StringVar(&opts.fileName, flag.File, "mongocli.json", "file name of the download center json file.")
144+
145+
_ = cmd.MarkFlagFilename(flag.File)
146+
_ = cmd.MarkFlagRequired(flag.Version)
147+
return cmd
148+
}

internal/release/main_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build unit
16+
// +build unit
17+
18+
package main
19+
20+
import (
21+
"testing"
22+
23+
"github.com/mongodb/mongocli/internal/flag"
24+
"github.com/mongodb/mongocli/internal/test"
25+
)
26+
27+
func TestBuilder(t *testing.T) {
28+
test.CmdValidator(
29+
t,
30+
Builder(),
31+
0,
32+
[]string{flag.Version, flag.File},
33+
)
34+
}

0 commit comments

Comments
 (0)