Skip to content

Commit

Permalink
Version Handling (#458)
Browse files Browse the repository at this point in the history
* adding version command and version marker

* updates based on feedback
  • Loading branch information
dbw7 authored and atanasdinov committed Jun 3, 2024
1 parent a8ba33b commit d3ebd95
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 7 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: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## General

* Added version command and version marker on CRB images

## API

### Image Definition Changes
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/version"
"github.com/urfave/cli/v2"
)

func Version(_ *cli.Context) error {
log.Auditf("Edge Image Builder Version: %s", version.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,
}
}
21 changes: 17 additions & 4 deletions pkg/combustion/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,35 @@ import (
"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"
"github.com/suse-edge/edge-image-builder/pkg/template"
"github.com/suse-edge/edge-image-builder/pkg/version"
)

const (
messageScriptName = "48-message.sh"
messageComponentName = "identifier"
)

//go:embed templates/48-message.sh
//go:embed templates/48-message.sh.tpl
var messageScript string

func configureMessage(ctx *image.Context) ([]string, error) {
filename := filepath.Join(ctx.CombustionDir, messageScriptName)
values := struct {
Version string
}{
Version: version.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
3 changes: 0 additions & 3 deletions pkg/combustion/templates/48-message.sh

This file was deleted.

3 changes: 3 additions & 0 deletions pkg/combustion/templates/48-message.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -euo pipefail
echo "Configured with the Edge Image Builder {{ .Version }}" >> /etc/issue.d/eib
24 changes: 24 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package version

import (
"fmt"
"runtime/debug"
)

var version string

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

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

return "Unknown"
}

0 comments on commit d3ebd95

Please sign in to comment.