Skip to content

Commit

Permalink
Automatically determine commit info with buildinfo (#243)
Browse files Browse the repository at this point in the history
* Add example for how to build from source w/ version info

* Automatically determine commit info with buildinfo

Gracefully falls back to ldflags provided value then "none" if -buildvcs=false

* enable colored syntach for the readme section

* fix the banner  when using the build from source

---------

Co-authored-by: simulot <[email protected]>
  • Loading branch information
mjsir911 and simulot authored May 20, 2024
1 parent f79261d commit e953522
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"os"
"os/signal"
"runtime/debug"

"github.com/simulot/immich-go/cmd"
"github.com/simulot/immich-go/cmd/duplicate"
Expand All @@ -26,9 +27,34 @@ var (
date = "unknown"
)

func getCommitInfo() string {
dirty := false
buildvcs := false

buildinfo, _ := debug.ReadBuildInfo()
for _, s := range buildinfo.Settings {
switch s.Key {
case "vcs.revision":
buildvcs = true
commit = s.Value
case "vcs.modified":
if s.Value == "true" {
dirty = true
}
case "vcs.time":
date = s.Value
}
}
if buildvcs && dirty {
commit += "-dirty"
}
return commit
}

func main() {
var err error
fmt.Printf("immich-go %s, commit %s, built at %s\n", version, commit, date)

fmt.Printf("immich-go %s, commit %s, built at %s\n", version, getCommitInfo(), date)

// Create a context with cancel function to gracefully handle Ctrl+C events
ctx, cancel := context.WithCancel(context.Background())
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ Open a command windows, go to the directory where immich-go resides, and type th
For a source-based installation, ensure you have the necessary Go language development tools (https://go.dev/doc/install) in place.
Download the source files or clone the repository.

```bash
go build -ldflags "-X 'main.version=$(git describe --tag)' -X 'main.date=$(date)'"
```

## Installation with Nix

`immich-go` is packaged with [nix](https://nixos.org/) and distributed via [nixpkgs](https://search.nixos.org/packages?channel=unstable&type=packages&query=immich-go).
Expand Down
12 changes: 8 additions & 4 deletions ui/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type Banner struct {
func NewBanner(version, commit, date string) Banner {
return Banner{
b: []string{
". _ _ _ _ . _|_ __ _ _ ",
"|| | || | ||(_| | (_|(_)",
". _ _ _ _ . _|_ _ _ ",
"|| | || | ||(_| | ── (_|(_)",
" _) ",
},
version: version,
Expand All @@ -29,14 +29,18 @@ func NewBanner(version, commit, date string) Banner {

// String generate a string with new lines and place the given text on the latest line
func (b Banner) String() string {
const lenVersion = 20
var text string
if b.version != "" {
text = fmt.Sprintf("version %s", b.version)
text = fmt.Sprintf("v %s", b.version)
}
sb := strings.Builder{}
for i := range b.b {
if i == len(b.b)-1 && text != "" {
sb.WriteString(b.b[i][:17-len(text)] + text + b.b[i][17:])
if len(text) >= lenVersion {
text = text[:lenVersion]
}
sb.WriteString(b.b[i][:lenVersion-len(text)] + text + b.b[i][lenVersion:])
} else {
sb.WriteString(b.b[i])
}
Expand Down

0 comments on commit e953522

Please sign in to comment.