|
| 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 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/olekukonko/tablewriter" |
| 25 | + "github.com/pkg/errors" |
| 26 | + "github.com/sirupsen/logrus" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + |
| 29 | + "k8s.io/release/pkg/log" |
| 30 | + "sigs.k8s.io/yaml" |
| 31 | +) |
| 32 | + |
| 33 | +// PatchSchedule main struct to hold the schedules |
| 34 | +type PatchSchedule struct { |
| 35 | + Schedules []Schedule `yaml:"schedules"` |
| 36 | +} |
| 37 | + |
| 38 | +// PreviousPatches struct to define the old pacth schedules |
| 39 | +type PreviousPatches struct { |
| 40 | + Release string `yaml:"release"` |
| 41 | + CherryPickDeadline string `yaml:"cherryPickDeadline"` |
| 42 | + TargetDate string `yaml:"targetDate"` |
| 43 | +} |
| 44 | + |
| 45 | +// Schedule struct to define the release schedule for a specific version |
| 46 | +type Schedule struct { |
| 47 | + Release string `yaml:"release"` |
| 48 | + Next string `yaml:"next"` |
| 49 | + CherryPickDeadline string `yaml:"cherryPickDeadline"` |
| 50 | + TargetDate string `yaml:"targetDate"` |
| 51 | + EndOfLifeDate string `yaml:"endOfLifeDate"` |
| 52 | + PreviousPatches []PreviousPatches `yaml:"previousPatches"` |
| 53 | +} |
| 54 | + |
| 55 | +// rootCmd represents the base command when called without any subcommands |
| 56 | +var rootCmd = &cobra.Command{ |
| 57 | + Use: "schedule-builder --config-path path/to/schedule.yaml [--output-file <filename.md>]", |
| 58 | + Short: "schedule-builder generate a humam readable format of the Kubernetes release schedule", |
| 59 | + Example: "schedule-builder --config-path /home/user/kubernetes/sig-release/releases/schedule.yaml", |
| 60 | + SilenceUsage: true, |
| 61 | + SilenceErrors: true, |
| 62 | + PersistentPreRunE: initLogging, |
| 63 | + RunE: func(*cobra.Command, []string) error { |
| 64 | + return run(opts) |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +type options struct { |
| 69 | + configPath string |
| 70 | + outputFile string |
| 71 | + logLevel string |
| 72 | +} |
| 73 | + |
| 74 | +var opts = &options{} |
| 75 | + |
| 76 | +const ( |
| 77 | + configPathFlag = "config-path" |
| 78 | + outputFileFlag = "output-file" |
| 79 | +) |
| 80 | + |
| 81 | +var requiredFlags = []string{ |
| 82 | + configPathFlag, |
| 83 | +} |
| 84 | + |
| 85 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 86 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 87 | +func Execute() { |
| 88 | + if err := rootCmd.Execute(); err != nil { |
| 89 | + logrus.Fatal(err) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func init() { |
| 94 | + rootCmd.PersistentFlags().StringVar( |
| 95 | + &opts.configPath, |
| 96 | + configPathFlag, |
| 97 | + "", |
| 98 | + "path where can find the schedule.yaml file", |
| 99 | + ) |
| 100 | + |
| 101 | + rootCmd.PersistentFlags().StringVar( |
| 102 | + &opts.outputFile, |
| 103 | + outputFileFlag, |
| 104 | + "", |
| 105 | + "name of the file that save the schedule to. If not set it will just output to the stdout.", |
| 106 | + ) |
| 107 | + |
| 108 | + rootCmd.PersistentFlags().StringVar( |
| 109 | + &opts.logLevel, |
| 110 | + "log-level", |
| 111 | + "info", |
| 112 | + "the logging verbosity, either 'panic', 'fatal', 'error', 'warn', 'warning', 'info', 'debug' or 'trace'", |
| 113 | + ) |
| 114 | + |
| 115 | + for _, flag := range requiredFlags { |
| 116 | + if err := rootCmd.MarkPersistentFlagRequired(flag); err != nil { |
| 117 | + logrus.Fatal(err) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func initLogging(*cobra.Command, []string) error { |
| 123 | + return log.SetupGlobalLogger(opts.logLevel) |
| 124 | +} |
| 125 | + |
| 126 | +func run(opts *options) error { |
| 127 | + if err := opts.SetAndValidate(); err != nil { |
| 128 | + return errors.Wrap(err, "validating schedule-path options") |
| 129 | + } |
| 130 | + |
| 131 | + logrus.Infof("Reading the schedule file %s...", opts.configPath) |
| 132 | + data, err := ioutil.ReadFile(opts.configPath) |
| 133 | + if err != nil { |
| 134 | + return errors.Wrap(err, "failed to read the file") |
| 135 | + } |
| 136 | + |
| 137 | + var patchSchedule PatchSchedule |
| 138 | + |
| 139 | + logrus.Info("Parsing the schedule...") |
| 140 | + err = yaml.UnmarshalStrict(data, &patchSchedule) |
| 141 | + if err != nil { |
| 142 | + return errors.Wrap(err, "failed to decode the file") |
| 143 | + } |
| 144 | + |
| 145 | + logrus.Info("Generating the markdown output...") |
| 146 | + |
| 147 | + output := []string{} |
| 148 | + output = append(output, "### Timeline\n") |
| 149 | + for _, releaseSchedule := range patchSchedule.Schedules { |
| 150 | + output = append(output, fmt.Sprintf("### %s\n", releaseSchedule.Release), |
| 151 | + fmt.Sprintf("Next patch release is **%s**\n", releaseSchedule.Next), |
| 152 | + fmt.Sprintf("End of Life for **%s** is **%s**\n", releaseSchedule.Release, releaseSchedule.EndOfLifeDate)) |
| 153 | + |
| 154 | + tableString := &strings.Builder{} |
| 155 | + table := tablewriter.NewWriter(tableString) |
| 156 | + table.SetAutoWrapText(false) |
| 157 | + table.SetHeader([]string{"Patch Release", "Cherry Pick Deadline", "Target Date"}) |
| 158 | + table.Append([]string{strings.TrimSpace(releaseSchedule.Next), strings.TrimSpace(releaseSchedule.CherryPickDeadline), strings.TrimSpace(releaseSchedule.TargetDate)}) |
| 159 | + |
| 160 | + for _, previous := range releaseSchedule.PreviousPatches { |
| 161 | + table.Append([]string{strings.TrimSpace(previous.Release), strings.TrimSpace(previous.CherryPickDeadline), strings.TrimSpace(previous.TargetDate)}) |
| 162 | + } |
| 163 | + table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) |
| 164 | + table.SetCenterSeparator("|") |
| 165 | + table.Render() |
| 166 | + |
| 167 | + output = append(output, tableString.String()) |
| 168 | + } |
| 169 | + |
| 170 | + scheduleOut := strings.Join(output, "\n") |
| 171 | + |
| 172 | + logrus.Info("Schedule parsed") |
| 173 | + println(scheduleOut) |
| 174 | + |
| 175 | + if opts.outputFile != "" { |
| 176 | + logrus.Infof("Saving schedule to a file %s.", opts.outputFile) |
| 177 | + err := ioutil.WriteFile(opts.outputFile, []byte(scheduleOut), 0644) |
| 178 | + if err != nil { |
| 179 | + return errors.Wrap(err, "failed to save schedule to the file") |
| 180 | + } |
| 181 | + logrus.Info("File saved") |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +// SetAndValidate sets some default options and verifies if options are valid |
| 188 | +func (o *options) SetAndValidate() error { |
| 189 | + logrus.Info("Validating schedule-path options...") |
| 190 | + |
| 191 | + if o.configPath == "" { |
| 192 | + return errors.Errorf("need to set the config-path") |
| 193 | + } |
| 194 | + |
| 195 | + return nil |
| 196 | +} |
0 commit comments