|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 | 8 | "strings"
|
9 |
| - "time" |
10 | 9 |
|
| 10 | + "github.com/Masterminds/semver" |
11 | 11 | "github.com/ory/viper"
|
12 | 12 | "github.com/spf13/cobra"
|
13 | 13 | "github.com/spf13/pflag"
|
@@ -329,61 +329,52 @@ func cwd() (cwd string) {
|
329 | 329 | return cwd
|
330 | 330 | }
|
331 | 331 |
|
| 332 | +// Version information populated on build. |
332 | 333 | type Version struct {
|
333 | 334 | // Date of compilation
|
334 | 335 | Date string
|
335 | 336 | // Version tag of the git commit, or 'tip' if no tag.
|
336 | 337 | Vers string
|
| 338 | + // Kver is the version of knative in which func was most recently |
| 339 | + // If the build is not tagged as being released with a specific Knative |
| 340 | + // build, this is the most recent version of knative along with a suffix |
| 341 | + // consisting of the number of commits which have been added since it was |
| 342 | + // included in Knative. |
| 343 | + Kver string |
337 | 344 | // Hash of the currently active git commit on build.
|
338 | 345 | Hash string
|
339 | 346 | // Verbose printing enabled for the string representation.
|
340 | 347 | Verbose bool
|
341 | 348 | }
|
342 | 349 |
|
343 |
| -// Return the stringification of the Version struct, which takes into account |
344 |
| -// the verbosity setting. |
| 350 | +// Return the stringification of the Version struct. |
345 | 351 | func (v Version) String() string {
|
346 | 352 | if v.Verbose {
|
347 | 353 | return v.StringVerbose()
|
348 | 354 | }
|
349 |
| - |
350 |
| - // Ensure that the value returned is parseable as a semver, with the special |
351 |
| - // value v0.0.0 as the default indicating there is no version information |
352 |
| - // available. |
353 |
| - if strings.HasPrefix(v.Vers, "v") { |
354 |
| - // TODO: this is the naive approach, perhaps consider actually parse it |
355 |
| - // using the semver lib |
356 |
| - return v.Vers |
357 |
| - } |
358 |
| - |
359 |
| - // Any non-semver value is invalid, and thus indistinguishable from a |
360 |
| - // nonexistent version value, so the default zero value of v0.0.0 is used. |
361 |
| - return "v0.0.0" |
| 355 | + _ = semver.MustParse(v.Vers) |
| 356 | + return v.Vers |
362 | 357 | }
|
363 | 358 |
|
364 |
| -// StringVerbose returns the verbose version of the version stringification. |
365 |
| -// The format returned is [semver]-[hash]-[date] where the special value |
366 |
| -// 'v0.0.0' and 'source' are used when version is not available and/or the |
367 |
| -// libray has been built from source, respectively. |
| 359 | +// StringVerbose returns the version along with extended version metadata. |
368 | 360 | func (v Version) StringVerbose() string {
|
369 | 361 | var (
|
370 | 362 | vers = v.Vers
|
| 363 | + kver = v.Kver |
371 | 364 | hash = v.Hash
|
372 |
| - date = v.Date |
373 | 365 | )
|
374 |
| - if vers == "" { |
375 |
| - vers = "v0.0.0" |
376 |
| - } |
377 |
| - if hash == "" { |
378 |
| - hash = "source" |
379 |
| - } |
380 |
| - if date == "" { |
381 |
| - date = time.Now().Format(time.RFC3339) |
| 366 | + if strings.HasPrefix(kver, "knative-") { |
| 367 | + kver = strings.Split(kver, "-")[1] |
382 | 368 | }
|
383 |
| - funcVersion := fmt.Sprintf("%s-%s-%s", vers, hash, date) |
384 |
| - return fmt.Sprintf("Version: %s\n"+ |
385 |
| - "SocatImage: %s\n"+ |
386 |
| - "TarImage: %s", funcVersion, |
| 369 | + return fmt.Sprintf( |
| 370 | + "Version: %s\n"+ |
| 371 | + "Knative: %s\n"+ |
| 372 | + "Commit: %s\n"+ |
| 373 | + "SocatImage: %s\n"+ |
| 374 | + "TarImage: %s\n", |
| 375 | + vers, |
| 376 | + kver, |
| 377 | + hash, |
387 | 378 | k8s.SocatImage,
|
388 | 379 | k8s.TarImage)
|
389 | 380 | }
|
|
0 commit comments