Skip to content

Commit

Permalink
Fixes for Windows + release script (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
oderwat committed Oct 28, 2024
1 parent 8b909f3 commit b93f598
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 53 deletions.
100 changes: 100 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Release

on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version tag (e.g., v1.0.0)'
required: true
type: string

permissions:
contents: write

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- os: linux
arch: amd64
binary: docker-inspector
- os: darwin
arch: amd64
binary: docker-inspector
- os: darwin
arch: arm64
binary: docker-inspector
- os: windows
arch: amd64
binary: docker-inspector.exe

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'

- name: Build internal inspector
env:
GOOS: linux
GOARCH: amd64
CGO_ENABLED: 0
run: |
go build -o cmd/docker-inspector/internal-inspector ./cmd/internal-inspector
- name: Build platform binary
env:
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}
CGO_ENABLED: 0
run: |
go build -o ${{ matrix.binary }} ./cmd/docker-inspector
- name: Create release archive
run: |
zip docker-inspector-${{ matrix.os }}-${{ matrix.arch }}.zip ${{ matrix.binary }}
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: binary-${{ matrix.os }}-${{ matrix.arch }}
path: docker-inspector-${{ matrix.os }}-${{ matrix.arch }}.zip
retention-days: 1

release:
needs: build
runs-on: ubuntu-latest
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
pattern: binary-*
merge-multiple: true

- name: Get version
id: get_version
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
else
echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
fi
- name: Create Release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.get_version.outputs.version }}
name: Release ${{ steps.get_version.outputs.version }}
files: docker-inspector-*.zip
generate_release_notes: true
draft: false
prerelease: false
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
docker-inspector-*
/docker-inspector-darwin
/docker-inspector-linux
/docker-inspector.exe
cmd/docker-inspector/internal-inspector
51 changes: 0 additions & 51 deletions cmd/docker-inspector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"text/tabwriter"
)

Expand Down Expand Up @@ -442,53 +441,3 @@ func extractID(s string) (int, error) {
}
return id, nil
}

func isOwnershipSupported(dir string) bool {
// Convert to absolute path
absPath, err := filepath.Abs(dir)
if err != nil {
return false
}

created := false
stat, err := os.Stat(absPath)
if err != nil {
// Create the output directory if it doesn't exist
if err := os.Mkdir(absPath, 0755); err != nil {
return false
}
created = true
}
defer func() {
if created {
if err := os.Remove(absPath); err != nil {
fmt.Fprintf(os.Stderr, "failed to remove %q: %v", absPath, err)
}
}
}()

testFile, err := os.CreateTemp(dir, ".ownership-test-*")
if err != nil {
return false
}
testPath := testFile.Name()
testFile.Close()
defer os.Remove(testPath)

fmt.Fprintf(os.Stderr, "Checking filesystem of %q for ownership support (requires sudo)...\n", dir)
// Try to change ownership to root:root
if err := exec.Command("sudo", "chown", "999:999", testPath).Run(); err != nil {
return false
}

// Read back the ownership
stat, err = os.Stat(testPath)
if err != nil {
return false
}

if sys, ok := stat.Sys().(*syscall.Stat_t); ok {
return sys.Uid == 999 && sys.Gid == 999
}
return false
}
61 changes: 61 additions & 0 deletions cmd/docker-inspector/ownership_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//go:build darwin || linux

package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
)

func isOwnershipSupported(dir string) bool {
// Convert to absolute path
absPath, err := filepath.Abs(dir)
if err != nil {
return false
}

created := false
stat, err := os.Stat(absPath)
if err != nil {
// Create the output directory if it doesn't exist
if err := os.Mkdir(absPath, 0755); err != nil {
return false
}
created = true
}
defer func() {
if created {
if err := os.Remove(absPath); err != nil {
fmt.Fprintf(os.Stderr, "failed to remove %q: %v", absPath, err)
}
}
}()

testFile, err := os.CreateTemp(dir, ".ownership-test-*")
if err != nil {
return false
}
testPath := testFile.Name()
testFile.Close()
defer os.Remove(testPath)

fmt.Fprintf(os.Stderr, "Checking filesystem of %q for ownership support (requires sudo)...\n", dir)
// Try to change ownership to root:root
if err := exec.Command("sudo", "chown", "999:999", testPath).Run(); err != nil {
return false
}

// Read back the ownership
stat, err = os.Stat(testPath)
if err != nil {
return false
}

if sys, ok := stat.Sys().(*syscall.Stat_t); ok {
return sys.Uid == 999 && sys.Gid == 999
}
return false
}
5 changes: 5 additions & 0 deletions cmd/docker-inspector/ownership_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func isOwnershipSupported(dir string) bool {
return false
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/oderwat/docker-inspector

go 1.23.2
go 1.21

require (
github.com/alexflint/go-arg v1.5.1
Expand Down

0 comments on commit b93f598

Please sign in to comment.