|
| 1 | +// Copyright Cozystack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package commands |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/Masterminds/semver/v3" |
| 22 | +) |
| 23 | + |
| 24 | +// devVersionStamp is the placeholder version written into a project's |
| 25 | +// Chart.yaml when it is generated by a non-release (dev) talm build. It |
| 26 | +// mirrors the sentinel used in main.go's initConfig. A project carrying |
| 27 | +// this value was never stamped by a real release, so there is nothing |
| 28 | +// meaningful to compare against. |
| 29 | +const devVersionStamp = "0.1.0" |
| 30 | + |
| 31 | +// CheckChartVersion compares the talm binary version against the version |
| 32 | +// stamped into the project's Chart.yaml at `talm init` / `talm init --update` |
| 33 | +// time. |
| 34 | +// |
| 35 | +// talm vendors its preset and library charts into the project directory at |
| 36 | +// init time; rendering (template/apply/upgrade) then reads that local copy, |
| 37 | +// never the binary's embedded charts. So upgrading the talm binary does not |
| 38 | +// touch the vendored charts/talm/ — they stay frozen at the version that last |
| 39 | +// ran init. This function surfaces that drift. |
| 40 | +// |
| 41 | +// binaryVersion is main.Version (e.g. "v0.30.0", or "dev" for source builds). |
| 42 | +// chartVersion is the project Chart.yaml `version:` field (e.g. "0.27.0"). |
| 43 | +// |
| 44 | +// It returns (true, message) only when both versions are real, parseable |
| 45 | +// releases that differ. It returns (false, "") — i.e. stays silent — when |
| 46 | +// either side is a dev build, the sentinel, empty, or unparseable, so it |
| 47 | +// never raises a false alarm against source builds or unstamped projects. |
| 48 | +func CheckChartVersion(binaryVersion, chartVersion string) (bool, string) { |
| 49 | + bin, err := semver.NewVersion(strings.TrimPrefix(binaryVersion, "v")) |
| 50 | + if err != nil { |
| 51 | + // dev / unparseable binary — nothing meaningful to compare. |
| 52 | + return false, "" |
| 53 | + } |
| 54 | + |
| 55 | + normalizedChart := strings.TrimPrefix(chartVersion, "v") |
| 56 | + if normalizedChart == "" || normalizedChart == devVersionStamp { |
| 57 | + // project was never stamped by a release build. |
| 58 | + return false, "" |
| 59 | + } |
| 60 | + |
| 61 | + chart, err := semver.NewVersion(normalizedChart) |
| 62 | + if err != nil { |
| 63 | + return false, "" |
| 64 | + } |
| 65 | + |
| 66 | + if bin.Equal(chart) { |
| 67 | + return false, "" |
| 68 | + } |
| 69 | + |
| 70 | + return true, fmt.Sprintf( |
| 71 | + "talm binary is %s but this project's charts were vendored by talm %s; "+ |
| 72 | + "run `talm init --update` to sync charts/talm/ (or ignore if this is intentional)", |
| 73 | + bin.String(), chart.String(), |
| 74 | + ) |
| 75 | +} |
0 commit comments