-
Notifications
You must be signed in to change notification settings - Fork 523
Patch release announce in golang #1040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
08e7e34
786b7a0
406b14a
0eef4d5
7db9810
7624e3b
7f7ec9d
1b393f6
d15f30b
2b8cce8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,8 +89,6 @@ const ( | |
) | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
const ( | ||
tagFlag = "tag" | ||
tarsFlag = "tars" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"k8s.io/release/pkg/log" | ||
"k8s.io/release/pkg/patch" | ||
"k8s.io/release/pkg/util" | ||
) | ||
|
||
// slap the subcommand onto the parent/root | ||
func init() { | ||
cmd := patchAnnounceCommand() | ||
rootCmd.AddCommand(cmd) | ||
} | ||
|
||
func patchAnnounceCommand() *cobra.Command { | ||
opts := patch.AnnounceOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "patch-announce", | ||
Short: "Send out patch release announcement mails", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
Args: cobra.MaximumNArgs(0), // no additional/positional args allowed | ||
} | ||
|
||
// setup local flags | ||
cmd.PersistentFlags().StringVarP(&opts.SenderName, "sender-name", "n", "", "email sender's name") | ||
cmd.PersistentFlags().StringVarP(&opts.SenderEmail, "sender-email", "e", "", "email sender's address") | ||
cmd.PersistentFlags().StringVarP(&opts.FreezeDate, "freeze-date", "f", "", "date when no CPs are allowed anymore") | ||
cmd.PersistentFlags().StringVarP(&opts.CutDate, "cut-date", "c", "", "date when the patch release is planned to be cut") | ||
cmd.PersistentFlags().StringVarP(&opts.ReleaseRepoPath, "release-repo", "r", "./release", "local path of the k/release checkout") | ||
|
||
// TODO: figure out, how we can read env vars and also be able to set the flags to required in a cobra-native way | ||
cmd.PersistentFlags().StringVarP(&opts.SendgridAPIKey, "sendgrid-api-key", "s", util.EnvDefault("SENDGRID_API_KEY", ""), "API key for sendgrid") | ||
cmd.PersistentFlags().StringVarP(&opts.GithubToken, "github-token", "g", util.EnvDefault("GITHUB_TOKEN", ""), "a GitHub token, used r/o for generating the release notes") | ||
|
||
cmd.PreRunE = func(cmd *cobra.Command, _ []string) error { | ||
// TODO: make github-token & sendgrid-api-key required too | ||
if err := setFlagsRequired(cmd, "sender-name", "sender-email", "freeze-date", "cut-date"); err != nil { | ||
return err | ||
} | ||
|
||
var err error | ||
if opts.Nomock, err = cmd.Flags().GetBool("nomock"); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return err | ||
} | ||
if opts.K8sRepoPath, err = cmd.Flags().GetString("repo"); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #1040 (comment) |
||
return err | ||
} | ||
return nil | ||
} | ||
|
||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
// Get the global logger, add the command's name as an initial tracing | ||
// field and use that from here on | ||
localLogger := logrus.NewEntry(logrus.StandardLogger()) | ||
logger := log.AddTracePath(localLogger, cmd.Name()).WithField("mock", !opts.Nomock) | ||
|
||
announcer := &patch.Announcer{ | ||
Opts: opts, | ||
} | ||
announcer.SetLogger(logger, "announcer") | ||
|
||
logger.Debug("run announcer") | ||
return announcer.Run() | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func setFlagsRequired(cmd *cobra.Command, flags ...string) error { | ||
for _, f := range flags { | ||
if err := cmd.MarkPersistentFlagRequired(f); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
init() should be avoided.
the responsibility of the addition should be on the side of the root command.
pseudo-summary of the pattern:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I also don't like the use of
init
, but I also tried to be consistent with what we have right now. If you check my implementation, I did take care of encapsulating my command, not relying on global state or such, so that it could be moved around and the initialisation/registration of the commands could change as needed.I could change to your proposed registration in this PR, but I lean towards be consistent with what we have right now, and change the registration for all commands in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's just a minor nit and for cobra related usage it should be fine.