Skip to content

Commit

Permalink
adding version command and version marker
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw7 committed May 31, 2024
1 parent 5f52f79 commit c22796d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ WORKDIR /src
COPY go.mod go.sum ./
COPY ./cmd ./cmd
COPY ./pkg ./pkg
COPY .git .git

RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
--mount=type=cache,id=gobuild,target=/root/.cache/go-build \
Expand Down
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
## Bug Fixes

* [#405](https://github.com/suse-edge/edge-image-builder/issues/405) - OCI registries are assumed to include the chart name

* [#457](https://github.com/suse-edge/edge-image-builder/issues/457) - Added version command and version marker on CRB images
---

# v1.0.0
Expand Down
1 change: 1 addition & 0 deletions cmd/eib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
app.Commands = []*cli.Command{
cmd.NewBuildCommand(build.Run),
cmd.NewValidateCommand(build.Validate),
cmd.NewVersionCommand(build.Version),
}

if err := app.Run(os.Args); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package build

import (
"github.com/suse-edge/edge-image-builder/pkg/log"
"github.com/suse-edge/edge-image-builder/pkg/template"
"github.com/urfave/cli/v2"
)

func Version(_ *cli.Context) error {
log.AuditInfof("Edge Image Builder Version: %s", template.GetVersion())
return nil
}
16 changes: 16 additions & 0 deletions pkg/cli/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"fmt"

"github.com/urfave/cli/v2"
)

func NewVersionCommand(action func(*cli.Context) error) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "Inspect program version",
UsageText: fmt.Sprintf("%s version", appName),
Action: action,
}
}
19 changes: 16 additions & 3 deletions pkg/combustion/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

"github.com/suse-edge/edge-image-builder/pkg/template"

"github.com/suse-edge/edge-image-builder/pkg/fileio"
"github.com/suse-edge/edge-image-builder/pkg/image"
"github.com/suse-edge/edge-image-builder/pkg/log"
Expand All @@ -20,11 +22,22 @@ const (
var messageScript string

func configureMessage(ctx *image.Context) ([]string, error) {
filename := filepath.Join(ctx.CombustionDir, messageScriptName)
values := struct {
Version string
}{
Version: template.GetVersion(),
}

if err := os.WriteFile(filename, []byte(messageScript), fileio.ExecutablePerms); err != nil {
data, err := template.Parse(messageScriptName, messageScript, &values)
if err != nil {
return nil, fmt.Errorf("parsing message script template: %w", err)
}

filename := filepath.Join(ctx.CombustionDir, messageScriptName)
err = os.WriteFile(filename, []byte(data), fileio.ExecutablePerms)
if err != nil {
log.AuditComponentFailed(messageComponentName)
return nil, fmt.Errorf("copying script %s: %w", messageScriptName, err)
return nil, fmt.Errorf("writing message script: %w", err)
}

log.AuditComponentSuccessful(messageComponentName)
Expand Down
2 changes: 1 addition & 1 deletion pkg/combustion/templates/48-message.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
set -euo pipefail
echo "Configured with the Edge Image Builder" >> /etc/issue.d/eib
echo "Configured with the Edge Image Builder {{ .Version }}" >> /etc/issue.d/eib
20 changes: 20 additions & 0 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package template
import (
"bytes"
"fmt"
"runtime/debug"
"strings"
"text/template"
)

var version string

func Parse(name string, contents string, templateData any) (string, error) {
if templateData == nil {
return "", fmt.Errorf("template data not provided")
Expand All @@ -26,3 +29,20 @@ func Parse(name string, contents string, templateData any) (string, error) {

return buff.String(), nil
}

func GetVersion() string {
if version != "" {
return version
}

if info, ok := debug.ReadBuildInfo(); ok {
fmt.Println(info)
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return fmt.Sprintf("git-%s", setting.Value)
}
}
}

return ""
}

0 comments on commit c22796d

Please sign in to comment.