|
| 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 | +} |
0 commit comments