|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "encoding/json" |
| 25 | + "os" |
| 26 | + |
| 27 | + "github.com/pkg/errors" |
| 28 | + "github.com/rs/zerolog/log" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +const cmdVersionCheckInitContainersInvalidVersionExitCode = 11 |
| 33 | + |
| 34 | +type cmdVersionCheckInitContainersInputStruct struct { |
| 35 | + versionPath string |
| 36 | + |
| 37 | + major, minor int |
| 38 | +} |
| 39 | + |
| 40 | +var ( |
| 41 | + cmdVersionCheckInitContainers = &cobra.Command{ |
| 42 | + Use: "version-check", |
| 43 | + RunE: cmdVersionCheckInitContainersInput.Run, |
| 44 | + } |
| 45 | + |
| 46 | + cmdVersionCheckInitContainersInput cmdVersionCheckInitContainersInputStruct |
| 47 | +) |
| 48 | + |
| 49 | +type cmdVersionCheckInitContainersData struct { |
| 50 | + Version int `json:"version,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +func init() { |
| 54 | + cmdInitContainers.AddCommand(cmdVersionCheckInitContainers) |
| 55 | + f := cmdVersionCheckInitContainers.Flags() |
| 56 | + f.StringVar(&cmdVersionCheckInitContainersInput.versionPath, "path", "", "Path to the VERSION file") |
| 57 | + f.IntVar(&cmdVersionCheckInitContainersInput.major, "major", 0, "Major version of the ArangoDB. 0 if check is disabled") |
| 58 | + f.IntVar(&cmdVersionCheckInitContainersInput.minor, "minor", 0, "Minor version of the ArangoDB. 0 if check is disabled") |
| 59 | +} |
| 60 | + |
| 61 | +func (c cmdVersionCheckInitContainersInputStruct) Run(cmd *cobra.Command, args []string) error { |
| 62 | + if c.versionPath == "" { |
| 63 | + return errors.Errorf("Path cannot be empty") |
| 64 | + } |
| 65 | + |
| 66 | + if data, err := os.ReadFile(c.versionPath); err != nil { |
| 67 | + log.Err(err).Msg("File is not readable, continue") |
| 68 | + return nil |
| 69 | + } else { |
| 70 | + major, minor, _, ok := extractVersionFromData(data) |
| 71 | + if !ok { |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + if c.major != 0 { |
| 76 | + if c.major != major { |
| 77 | + return Exit(cmdVersionCheckInitContainersInvalidVersionExitCode) |
| 78 | + } |
| 79 | + if c.minor != 0 { |
| 80 | + if c.minor != minor { |
| 81 | + return Exit(cmdVersionCheckInitContainersInvalidVersionExitCode) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func extractVersionFromData(data []byte) (int, int, int, bool) { |
| 91 | + var c cmdVersionCheckInitContainersData |
| 92 | + |
| 93 | + if err := json.Unmarshal(data, &c); err != nil { |
| 94 | + log.Err(err).Msg("Invalid json, continue") |
| 95 | + return 0, 0, 0, false |
| 96 | + } |
| 97 | + |
| 98 | + return c.Version / 10000, c.Version % 10000 / 100, c.Version % 100, true |
| 99 | +} |
0 commit comments