Skip to content

Commit 95e3ac4

Browse files
Copy over all the stuff from the other repo (#1)
1 parent e74c0f6 commit 95e3ac4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+9053
-0
lines changed

.github/renovate.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": [
4+
"config:recommended"
5+
]
6+
}

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
# For code coverage reports
6+
branches: [main]
7+
pull_request:
8+
branches: [main]
9+
10+
permissions:
11+
id-token: write
12+
contents: read
13+
14+
jobs:
15+
build:
16+
name: Build and Test
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- uses: actions/setup-go@v5
22+
with:
23+
go-version-file: go.mod
24+
cache-dependency-path: go.sum
25+
26+
- run: make build
27+
- run: make test
28+
29+
- name: Upload coverage to Codecov
30+
uses: codecov/codecov-action@v5
31+
with:
32+
use_oidc: true
33+
files: cover.profile

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bin/
2+
report.json
3+
cover.profile
4+
go.work*
5+
.vscode/settings.json
6+
.make/
7+
*.test
8+
.envrc

.versions/devctl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.2

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"golang.go"
4+
]
5+
}

Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
_ := $(shell mkdir -p .make bin)
2+
3+
WORKING_DIR := $(shell pwd)
4+
LOCALBIN := ${WORKING_DIR}/bin
5+
6+
export GOBIN := ${LOCALBIN}
7+
8+
DEVCTL := ${LOCALBIN}/devctl
9+
GINKGO := ${LOCALBIN}/ginkgo
10+
11+
ifeq ($(CI),)
12+
TEST_FLAGS := --label-filter !E2E
13+
else
14+
TEST_FLAGS := --github-output --race --trace --coverprofile=cover.profile
15+
endif
16+
17+
build: .make/build
18+
test: .make/test
19+
tidy: go.sum
20+
21+
test_all:
22+
$(GINKGO) run -r ./
23+
24+
go.sum: go.mod $(shell $(DEVCTL) list --go) | bin/devctl
25+
go mod tidy
26+
27+
%_suite_test.go: | bin/ginkgo
28+
cd $(dir $@) && $(GINKGO) bootstrap
29+
30+
%_test.go: | bin/ginkgo
31+
cd $(dir $@) && $(GINKGO) generate $(notdir $*)
32+
33+
bin/ginkgo: go.mod
34+
go install github.com/onsi/ginkgo/v2/ginkgo
35+
36+
bin/devctl: .versions/devctl
37+
go install github.com/unmango/devctl/cmd@v$(shell cat $<)
38+
mv ${LOCALBIN}/cmd $@
39+
40+
.envrc: hack/example.envrc
41+
cp $< $@
42+
43+
.make/build: $(shell $(DEVCTL) list --go --exclude-tests) | bin/devctl
44+
go build ./...
45+
@touch $@
46+
47+
.make/test: $(shell $(DEVCTL) list --go) | bin/ginkgo bin/devctl
48+
$(GINKGO) run ${TEST_FLAGS} $(sort $(dir $?))
49+
@touch $@

context.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package aferox
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/afero"
7+
"github.com/unmango/aferox/context"
8+
)
9+
10+
type contextKey struct{}
11+
12+
var defaultContextFs = afero.NewOsFs()
13+
14+
func SetContext(fs afero.Fs, ctx context.Context) error {
15+
if ctxfs, ok := fs.(context.Setter); !ok {
16+
return fmt.Errorf("context not supported: %s", fs.Name())
17+
} else {
18+
ctxfs.SetContext(ctx)
19+
}
20+
21+
return nil
22+
}
23+
24+
func FromContext(ctx context.Context) afero.Fs {
25+
if fs := ctx.Value(contextKey{}); fs != nil {
26+
return fs.(afero.Fs)
27+
} else {
28+
return defaultContextFs
29+
}
30+
}

context/accessor.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package context
2+
3+
import (
4+
"fmt"
5+
"io/fs"
6+
"time"
7+
8+
"github.com/spf13/afero"
9+
)
10+
11+
type AccessorFunc func() Context
12+
13+
func (fn AccessorFunc) Context() Context {
14+
return fn()
15+
}
16+
17+
func ToAccessor[T ~func() Context](fn T) Accessor {
18+
return AccessorFunc(fn)
19+
}
20+
21+
// AccessorFs adapts an [Fs] to an [afero.Fs] by using the given [ContextAccessor]
22+
// to source the [context.Context] for each operation
23+
type AccessorFs struct {
24+
Accessor
25+
Fs Fs
26+
}
27+
28+
// Chmod implements afero.Fs.
29+
func (a *AccessorFs) Chmod(name string, mode fs.FileMode) error {
30+
return a.Fs.Chmod(a.Context(), name, mode)
31+
}
32+
33+
// Chown implements afero.Fs.
34+
func (a *AccessorFs) Chown(name string, uid int, gid int) error {
35+
return a.Fs.Chown(a.Context(), name, uid, gid)
36+
}
37+
38+
// Chtimes implements afero.Fs.
39+
func (a *AccessorFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
40+
return a.Fs.Chtimes(a.Context(), name, atime, mtime)
41+
}
42+
43+
// Create implements afero.Fs.
44+
func (a *AccessorFs) Create(name string) (afero.File, error) {
45+
return a.Fs.Create(a.Context(), name)
46+
}
47+
48+
// Mkdir implements afero.Fs.
49+
func (a *AccessorFs) Mkdir(name string, perm fs.FileMode) error {
50+
return a.Fs.Mkdir(a.Context(), name, perm)
51+
}
52+
53+
// MkdirAll implements afero.Fs.
54+
func (a *AccessorFs) MkdirAll(path string, perm fs.FileMode) error {
55+
return a.Fs.MkdirAll(a.Context(), path, perm)
56+
}
57+
58+
// Name implements afero.Fs.
59+
func (a *AccessorFs) Name() string {
60+
return fmt.Sprintf("Context: %s", a.Fs.Name())
61+
}
62+
63+
// Open implements afero.Fs.
64+
func (a *AccessorFs) Open(name string) (afero.File, error) {
65+
return a.Fs.Open(a.Context(), name)
66+
}
67+
68+
// OpenFile implements afero.Fs.
69+
func (a *AccessorFs) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error) {
70+
return a.Fs.OpenFile(a.Context(), name, flag, perm)
71+
}
72+
73+
// Remove implements afero.Fs.
74+
func (a *AccessorFs) Remove(name string) error {
75+
return a.Fs.Remove(a.Context(), name)
76+
}
77+
78+
// RemoveAll implements afero.Fs.
79+
func (a *AccessorFs) RemoveAll(path string) error {
80+
return a.Fs.RemoveAll(a.Context(), path)
81+
}
82+
83+
// Rename implements afero.Fs.
84+
func (a *AccessorFs) Rename(oldname string, newname string) error {
85+
return a.Fs.Rename(a.Context(), oldname, newname)
86+
}
87+
88+
// Stat implements afero.Fs.
89+
func (a *AccessorFs) Stat(name string) (fs.FileInfo, error) {
90+
return a.Fs.Stat(a.Context(), name)
91+
}
92+
93+
var _ afero.Fs = (*AccessorFs)(nil)

0 commit comments

Comments
 (0)