Skip to content

Commit

Permalink
feat: Add github actions workflow and version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
HikariKnight committed Mar 30, 2024
1 parent be8b46b commit c4ea43b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
26 changes: 13 additions & 13 deletions .github/workflow/release.yml → .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
# run only against tags
tags:
- '*'
- "*"

permissions:
contents: write
Expand All @@ -15,25 +15,25 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v3
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '>=1.20.1'
cache: true
# More assembly might be required: Docker logins, GPG, etc. It all depends
# on your needs.
- uses: goreleaser/goreleaser-action@v4
go-version: stable
# More assembly might be required: Docker logins, GPG, etc.
# It all depends on your needs.
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
# either 'goreleaser' (default) or 'goreleaser-pro':
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
# 'latest', 'nightly', or a semver
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
# distribution:
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}

48 changes: 48 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 1

before:
hooks:
- go mod tidy

builds:
-
id: "ls-iommu"
main: ./cmd/main.go
binary: "ls-iommu"
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm64
mod_timestamp: "{{ .CommitTimestamp }}"
ldflags:
- -s -w -X github.com/HikariKnight/ls-iommu/internal/version.Version={{.Version}}
archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"

upx:
-
enabled: true
compress: best
lzma: true
brute: true
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ It's purpose was to just list every device and their associated IOMMU group.
This project does the same thing, but extends the functionality by implementing arguments to list the output in different ways or provide just the details needed without having to hop through grep, sed, awk or perl pipe hoops.

Currently the program supports the same behavior as Wendell's ls-iommu script but can also be told to display only devices in selected IOMMU groups, only GPUs or only USB controllers and also locate related devices.<br>
More extended functionality is planned.
More functionality can be added if it is deemed useful, just open an issue with the request.

Note: arm builds are generated but not tested as I lack the relevant hardware.

![screenshot](https://user-images.githubusercontent.com/2557889/223729837-66461127-997c-4ce4-9183-9d2b85219a07.png)

Expand All @@ -29,10 +31,11 @@ Prerequisites:
* Go 1.20+
* git

This will build the latest `ls-iommu` and set the version to the latest commit hash.
```bash
git clone https://github.com/HikariKnight/ls-iommu.git
cd ls-iommu
CGO_ENABLED=0 go build
CGO_ENABLED=0 go build -ldflags="-X github.com/HikariKnight/ls-iommu/internal/version.Version=$(git rev-parse --short HEAD)" -o ls-iommu cmd/main.go
```

The binary `ls-iommu` will now be located the root of the project directory.
8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"os"

"github.com/HikariKnight/ls-iommu/internal/version"
iommu "github.com/HikariKnight/ls-iommu/pkg/iommu"
params "github.com/HikariKnight/ls-iommu/pkg/params"
)
Expand All @@ -11,6 +13,12 @@ func main() {
// Get all our arguments in 1 neat struct
pArg := params.NewParams()

// Display version and exit if the version flag is present
if pArg.Flag["version"] {
fmt.Printf("ls-iommu version %s built in Go\n", version.Version)
os.Exit(0)
}

// Work with the output depending on arguments given
if pArg.Flag["gpu"] {
// Get all GPUs (3d controllers are ignored)
Expand Down
4 changes: 4 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package version

// This is automatically set in CI using -ldflags="-X github.com/HikariKnight/ls-iommu/internal/version.Version=${TAG}" as a build argument
var Version string
9 changes: 9 additions & 0 deletions pkg/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func NewParams() *Params {
// Setup the parser for arguments
parser := argparse.NewParser("ls-iommu", "A Tool to print out all devices and their IOMMU groups")

// Add version flag
version := parser.Flag("v", "version", &argparse.Options{
Required: false,
Help: "Display the version of ls-iommu",
})

// Configure arguments
gpu := parser.Flag("g", "gpu", &argparse.Options{
Required: false,
Expand Down Expand Up @@ -144,6 +150,9 @@ func NewParams() *Params {
String: make(map[string]string),
}

// Add version flag
pArg.addFlag("version", *version)

// Add all parsed arguments to a struct for portability since we will use them all over the program
pArg.addFlag("gpu", *gpu)
pArg.addFlag("usb", *usb)
Expand Down

0 comments on commit c4ea43b

Please sign in to comment.