Skip to content

Commit 515cf2b

Browse files
authored
Merge pull request #1040 from pivotal-k8s/announce-patch-go
Patch release announce in golang
2 parents d15d82e + 2b8cce8 commit 515cf2b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3783
-370
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ run:
44
deadline: 5m
55
issues:
66
exclude-rules:
7-
- path: fake_client\.go
7+
# counterfeiter fakes are usually named 'fake_<something>.go'
8+
# TODO: figure out why golangci-lint does not treat counterfeiter files as generated files
9+
- path: fake_.*\.go
810
linters:
911
- gocritic
1012
- dupl

BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ filegroup(
3030
"//cmd/blocking-testgrid-tests:all-srcs",
3131
"//cmd/krel:all-srcs",
3232
"//cmd/kubepkg:all-srcs",
33+
"//cmd/patch-announce:all-srcs",
3334
"//cmd/release-notes:all-srcs",
3435
"//lib:all-srcs",
3536
"//pkg/command:all-srcs",
3637
"//pkg/git:all-srcs",
3738
"//pkg/kubepkg:all-srcs",
3839
"//pkg/log:all-srcs",
3940
"//pkg/notes:all-srcs",
41+
"//pkg/patch:all-srcs",
4042
"//pkg/release:all-srcs",
4143
"//pkg/util:all-srcs",
4244
"//pkg/version:all-srcs",

announce-patch

Lines changed: 0 additions & 151 deletions
This file was deleted.

cmd/krel/cmd/BUILD.bazel

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = [
66
"changelog.go",
77
"ff.go",
8+
"patch-announce.go",
89
"push.go",
910
"root.go",
1011
"version.go",
@@ -16,6 +17,7 @@ go_library(
1617
"//pkg/log:go_default_library",
1718
"//pkg/notes:go_default_library",
1819
"//pkg/notes/options:go_default_library",
20+
"//pkg/patch:go_default_library",
1921
"//pkg/release:go_default_library",
2022
"//pkg/util:go_default_library",
2123
"//pkg/version:go_default_library",
@@ -28,6 +30,16 @@ go_library(
2830
],
2931
)
3032

33+
go_test(
34+
name = "go_default_test",
35+
srcs = [
36+
"changelog_test.go",
37+
"root_test.go",
38+
],
39+
embed = [":go_default_library"],
40+
deps = ["@com_github_stretchr_testify//require:go_default_library"],
41+
)
42+
3143
filegroup(
3244
name = "package-srcs",
3345
srcs = glob(["**"]),
@@ -41,13 +53,3 @@ filegroup(
4153
tags = ["automanaged"],
4254
visibility = ["//visibility:public"],
4355
)
44-
45-
go_test(
46-
name = "go_default_test",
47-
srcs = [
48-
"changelog_test.go",
49-
"root_test.go",
50-
],
51-
embed = [":go_default_library"],
52-
deps = ["@com_github_stretchr_testify//require:go_default_library"],
53-
)

cmd/krel/cmd/changelog.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ const (
9292
)
9393

9494
func init() {
95-
cobra.OnInitialize(initConfig)
96-
9795
const (
9896
tagFlag = "tag"
9997
tarsFlag = "tars"

cmd/krel/cmd/ff.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ var ffCmd = &cobra.Command{
5353
}
5454

5555
func init() {
56-
cobra.OnInitialize(initConfig)
57-
5856
ffCmd.PersistentFlags().StringVar(&ffOpts.branch, "branch", "", "branch")
5957
ffCmd.PersistentFlags().StringVar(&ffOpts.masterRef, "ref", kgit.DefaultMasterRef, "ref on master")
6058
ffCmd.PersistentFlags().StringVar(&ffOpts.org, "org", kgit.DefaultGithubOrg, "org to run tool against")

cmd/krel/cmd/patch-announce.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/sirupsen/logrus"
21+
"github.com/spf13/cobra"
22+
"k8s.io/release/pkg/log"
23+
"k8s.io/release/pkg/patch"
24+
"k8s.io/release/pkg/util"
25+
)
26+
27+
// slap the subcommand onto the parent/root
28+
func init() {
29+
cmd := patchAnnounceCommand()
30+
rootCmd.AddCommand(cmd)
31+
}
32+
33+
func patchAnnounceCommand() *cobra.Command {
34+
opts := patch.AnnounceOptions{}
35+
36+
cmd := &cobra.Command{
37+
Use: "patch-announce",
38+
Short: "Send out patch release announcement mails",
39+
SilenceUsage: true,
40+
SilenceErrors: true,
41+
Args: cobra.MaximumNArgs(0), // no additional/positional args allowed
42+
}
43+
44+
// setup local flags
45+
cmd.PersistentFlags().StringVarP(&opts.SenderName, "sender-name", "n", "", "email sender's name")
46+
cmd.PersistentFlags().StringVarP(&opts.SenderEmail, "sender-email", "e", "", "email sender's address")
47+
cmd.PersistentFlags().StringVarP(&opts.FreezeDate, "freeze-date", "f", "", "date when no CPs are allowed anymore")
48+
cmd.PersistentFlags().StringVarP(&opts.CutDate, "cut-date", "c", "", "date when the patch release is planned to be cut")
49+
cmd.PersistentFlags().StringVarP(&opts.ReleaseRepoPath, "release-repo", "r", "./release", "local path of the k/release checkout")
50+
51+
// TODO: figure out, how we can read env vars and also be able to set the flags to required in a cobra-native way
52+
cmd.PersistentFlags().StringVarP(&opts.SendgridAPIKey, "sendgrid-api-key", "s", util.EnvDefault("SENDGRID_API_KEY", ""), "API key for sendgrid")
53+
cmd.PersistentFlags().StringVarP(&opts.GithubToken, "github-token", "g", util.EnvDefault("GITHUB_TOKEN", ""), "a GitHub token, used r/o for generating the release notes")
54+
55+
cmd.PreRunE = func(cmd *cobra.Command, _ []string) error {
56+
// TODO: make github-token & sendgrid-api-key required too
57+
if err := setFlagsRequired(cmd, "sender-name", "sender-email", "freeze-date", "cut-date"); err != nil {
58+
return err
59+
}
60+
61+
var err error
62+
if opts.Nomock, err = cmd.Flags().GetBool("nomock"); err != nil {
63+
return err
64+
}
65+
if opts.K8sRepoPath, err = cmd.Flags().GetString("repo"); err != nil {
66+
return err
67+
}
68+
return nil
69+
}
70+
71+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
72+
// Get the global logger, add the command's name as an initial tracing
73+
// field and use that from here on
74+
localLogger := logrus.NewEntry(logrus.StandardLogger())
75+
logger := log.AddTracePath(localLogger, cmd.Name()).WithField("mock", !opts.Nomock)
76+
77+
announcer := &patch.Announcer{
78+
Opts: opts,
79+
}
80+
announcer.SetLogger(logger, "announcer")
81+
82+
logger.Debug("run announcer")
83+
return announcer.Run()
84+
}
85+
86+
return cmd
87+
}
88+
89+
func setFlagsRequired(cmd *cobra.Command, flags ...string) error {
90+
for _, f := range flags {
91+
if err := cmd.MarkPersistentFlagRequired(f); err != nil {
92+
return err
93+
}
94+
}
95+
return nil
96+
}

cmd/krel/cmd/root.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828

2929
// rootCmd represents the base command when called without any subcommands
3030
var rootCmd = &cobra.Command{
31-
Use: "krel",
32-
Short: "krel",
33-
PreRunE: initLogging,
31+
Use: "krel",
32+
Short: "krel",
33+
PersistentPreRunE: initLogging,
3434
}
3535

3636
type rootOptions struct {
@@ -51,26 +51,12 @@ func Execute() {
5151
}
5252

5353
func init() {
54-
cobra.OnInitialize(initConfig)
55-
5654
rootCmd.PersistentFlags().BoolVar(&rootOpts.nomock, "nomock", false, "nomock flag")
5755
rootCmd.PersistentFlags().BoolVar(&rootOpts.cleanup, "cleanup", false, "cleanup flag")
5856
rootCmd.PersistentFlags().StringVar(&rootOpts.repoPath, "repo", filepath.Join(os.TempDir(), "k8s"), "the local path to the repository to be used")
5957
rootCmd.PersistentFlags().StringVar(&rootOpts.logLevel, "log-level", "info", "the logging verbosity, either 'panic', 'fatal', 'error', 'warn', 'warning', 'info', 'debug' or 'trace'")
6058
}
6159

62-
// initConfig reads in config file and ENV variables if set.
63-
func initConfig() {
64-
}
65-
6660
func initLogging(*cobra.Command, []string) error {
67-
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
68-
lvl, err := logrus.ParseLevel(rootOpts.logLevel)
69-
if err != nil {
70-
return err
71-
}
72-
logrus.SetLevel(lvl)
73-
logrus.AddHook(log.NewFilenameHook())
74-
logrus.Debugf("Using log level %q", lvl)
75-
return nil
61+
return log.SetupGlobalLogger(rootOpts.logLevel)
7662
}

cmd/krel/cmd/version.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ var versionCmd = &cobra.Command{
4444
}
4545

4646
func init() {
47-
cobra.OnInitialize(initConfig)
4847
versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false,
4948
"print JSON instead of text")
5049
rootCmd.AddCommand(versionCmd)

0 commit comments

Comments
 (0)