Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy over all the stuff from the other repo #1

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
}
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI

on:
push:
# For code coverage reports
branches: [main]
pull_request:
branches: [main]

permissions:
id-token: write
contents: read

jobs:
build:
name: Build and Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache-dependency-path: go.sum

- run: make build
- run: make test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
use_oidc: true
files: cover.profile
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
bin/
report.json
cover.profile
go.work*
.vscode/settings.json
.make/
*.test
.envrc
1 change: 1 addition & 0 deletions .versions/devctl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.2
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"golang.go"
]
}
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
_ := $(shell mkdir -p .make bin)

WORKING_DIR := $(shell pwd)
LOCALBIN := ${WORKING_DIR}/bin

export GOBIN := ${LOCALBIN}

DEVCTL := ${LOCALBIN}/devctl
GINKGO := ${LOCALBIN}/ginkgo

ifeq ($(CI),)
TEST_FLAGS := --label-filter !E2E
else
TEST_FLAGS := --github-output --race --trace --coverprofile=cover.profile
endif

build: .make/build
test: .make/test
tidy: go.sum

test_all:
$(GINKGO) run -r ./

go.sum: go.mod $(shell $(DEVCTL) list --go) | bin/devctl
go mod tidy

%_suite_test.go: | bin/ginkgo
cd $(dir $@) && $(GINKGO) bootstrap

%_test.go: | bin/ginkgo
cd $(dir $@) && $(GINKGO) generate $(notdir $*)

bin/ginkgo: go.mod
go install github.com/onsi/ginkgo/v2/ginkgo

bin/devctl: .versions/devctl
go install github.com/unmango/devctl/cmd@v$(shell cat $<)
mv ${LOCALBIN}/cmd $@

.envrc: hack/example.envrc
cp $< $@

.make/build: $(shell $(DEVCTL) list --go --exclude-tests) | bin/devctl
go build ./...
@touch $@

.make/test: $(shell $(DEVCTL) list --go) | bin/ginkgo bin/devctl
$(GINKGO) run ${TEST_FLAGS} $(sort $(dir $?))
@touch $@
30 changes: 30 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package aferox

import (
"fmt"

"github.com/spf13/afero"
"github.com/unmango/aferox/context"
)

type contextKey struct{}

var defaultContextFs = afero.NewOsFs()

func SetContext(fs afero.Fs, ctx context.Context) error {
if ctxfs, ok := fs.(context.Setter); !ok {
return fmt.Errorf("context not supported: %s", fs.Name())
} else {
ctxfs.SetContext(ctx)
}

Check warning on line 19 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L14-L19

Added lines #L14 - L19 were not covered by tests

return nil

Check warning on line 21 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L21

Added line #L21 was not covered by tests
}

func FromContext(ctx context.Context) afero.Fs {
if fs := ctx.Value(contextKey{}); fs != nil {
return fs.(afero.Fs)
} else {
return defaultContextFs
}

Check warning on line 29 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L24-L29

Added lines #L24 - L29 were not covered by tests
}
93 changes: 93 additions & 0 deletions context/accessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package context

import (
"fmt"
"io/fs"
"time"

"github.com/spf13/afero"
)

type AccessorFunc func() Context

func (fn AccessorFunc) Context() Context {
return fn()

Check warning on line 14 in context/accessor.go

View check run for this annotation

Codecov / codecov/patch

context/accessor.go#L13-L14

Added lines #L13 - L14 were not covered by tests
}

func ToAccessor[T ~func() Context](fn T) Accessor {
return AccessorFunc(fn)

Check warning on line 18 in context/accessor.go

View check run for this annotation

Codecov / codecov/patch

context/accessor.go#L17-L18

Added lines #L17 - L18 were not covered by tests
}

// AccessorFs adapts an [Fs] to an [afero.Fs] by using the given [ContextAccessor]
// to source the [context.Context] for each operation
type AccessorFs struct {
Accessor
Fs Fs
}

// Chmod implements afero.Fs.
func (a *AccessorFs) Chmod(name string, mode fs.FileMode) error {
return a.Fs.Chmod(a.Context(), name, mode)
}

// Chown implements afero.Fs.
func (a *AccessorFs) Chown(name string, uid int, gid int) error {
return a.Fs.Chown(a.Context(), name, uid, gid)
}

// Chtimes implements afero.Fs.
func (a *AccessorFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
return a.Fs.Chtimes(a.Context(), name, atime, mtime)
}

// Create implements afero.Fs.
func (a *AccessorFs) Create(name string) (afero.File, error) {
return a.Fs.Create(a.Context(), name)
}

// Mkdir implements afero.Fs.
func (a *AccessorFs) Mkdir(name string, perm fs.FileMode) error {
return a.Fs.Mkdir(a.Context(), name, perm)
}

// MkdirAll implements afero.Fs.
func (a *AccessorFs) MkdirAll(path string, perm fs.FileMode) error {
return a.Fs.MkdirAll(a.Context(), path, perm)
}

// Name implements afero.Fs.
func (a *AccessorFs) Name() string {
return fmt.Sprintf("Context: %s", a.Fs.Name())

Check warning on line 60 in context/accessor.go

View check run for this annotation

Codecov / codecov/patch

context/accessor.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}

// Open implements afero.Fs.
func (a *AccessorFs) Open(name string) (afero.File, error) {
return a.Fs.Open(a.Context(), name)
}

// OpenFile implements afero.Fs.
func (a *AccessorFs) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error) {
return a.Fs.OpenFile(a.Context(), name, flag, perm)
}

// Remove implements afero.Fs.
func (a *AccessorFs) Remove(name string) error {
return a.Fs.Remove(a.Context(), name)
}

// RemoveAll implements afero.Fs.
func (a *AccessorFs) RemoveAll(path string) error {
return a.Fs.RemoveAll(a.Context(), path)
}

// Rename implements afero.Fs.
func (a *AccessorFs) Rename(oldname string, newname string) error {
return a.Fs.Rename(a.Context(), oldname, newname)
}

// Stat implements afero.Fs.
func (a *AccessorFs) Stat(name string) (fs.FileInfo, error) {
return a.Fs.Stat(a.Context(), name)
}

var _ afero.Fs = (*AccessorFs)(nil)
Loading
Loading