Skip to content

Commit

Permalink
feat: context propagation
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Semagin <[email protected]>
  • Loading branch information
pixel365 committed Feb 18, 2025
1 parent def9a0c commit 2c70096
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func build(cmd *cobra.Command, _ []string) error {
return err
}

module.Ctx = cmd.Context()

if err := module.Build(); err != nil {
return err
}
Expand Down
38 changes: 36 additions & 2 deletions internal/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -13,6 +14,7 @@ import (
)

func copyFromPath(
ctx context.Context,
wg *sync.WaitGroup,
errCh chan<- error,
ignore *[]string,
Expand All @@ -21,14 +23,20 @@ func copyFromPath(
) {
defer wg.Done()

if err := walk(wg, errCh, from, to, ignore, existsMode); err != nil {
if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

if err := walk(ctx, wg, errCh, from, to, ignore, existsMode); err != nil {
if !errors.Is(err, doublestar.SkipDir) {
errCh <- err
}
}
}

func walk(
ctx context.Context,
wg *sync.WaitGroup,
errCh chan<- error,
from, to string,
Expand All @@ -42,6 +50,11 @@ func walk(
jobs := make(chan struct{}, 10)

err := filepath.Walk(from, func(path string, info os.FileInfo, err error) error {
if ctxErr := CheckContextActivity(ctx); ctxErr != nil {
errCh <- ctxErr
return ctxErr
}

if err != nil {
return err
}
Expand Down Expand Up @@ -82,7 +95,7 @@ func walk(
}

wg2.Add(1)
go copyFile(&wg2, errCh, absFrom, absTo, jobs, existsMode)
go copyFile(ctx, &wg2, errCh, absFrom, absTo, jobs, existsMode)
}

return nil
Expand All @@ -94,6 +107,7 @@ func walk(
}

func copyFile(
ctx context.Context,
wg *sync.WaitGroup,
errCh chan<- error,
src, dst string,
Expand All @@ -102,6 +116,11 @@ func copyFile(
) {
defer wg.Done()

if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

fileName := strings.LastIndex(src, "/")
if !strings.HasSuffix(dst, src[fileName:]) {
dst = filepath.Join(dst, src[fileName:])
Expand Down Expand Up @@ -133,6 +152,11 @@ func copyFile(
}
}(in)

if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

info, err := in.Stat()
if err != nil {
errCh <- err
Expand Down Expand Up @@ -160,12 +184,22 @@ func copyFile(
}
}(out)

if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

_, err = io.Copy(out, in)
if err != nil {
errCh <- err
return
}

if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

err = os.Chtimes(dst, info.ModTime(), info.ModTime())
if err != nil {
errCh <- err
Expand Down
2 changes: 2 additions & 0 deletions internal/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"errors"
"fmt"
)
Expand All @@ -21,6 +22,7 @@ type Item struct {
}

type Module struct {
Ctx context.Context
Name string `yaml:"name"`
Version string `yaml:"version"`
Account string `yaml:"account"`
Expand Down
24 changes: 22 additions & 2 deletions internal/module_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -11,6 +12,10 @@ import (
)

func (m *Module) Build() error {
if err := CheckContextActivity(m.Ctx); err != nil {
return err
}

logFile, err := os.OpenFile(
fmt.Sprintf("./%s-%s.%s.log", m.Name, m.Version, time.Now().UTC().Format(time.RFC3339)),
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
Expand Down Expand Up @@ -131,8 +136,12 @@ func (m *Module) Collect(log *zerolog.Logger) error {
errCh := make(chan error, len(m.Mapping))

for _, item := range m.Mapping {
if err := CheckContextActivity(m.Ctx); err != nil {
return err
}

wg.Add(1)
go handleItem(&wg, errCh, &m.Ignore, item, buildDirectory)
go handleItem(m.Ctx, &wg, errCh, &m.Ignore, item, buildDirectory)
}

wg.Wait()
Expand All @@ -152,6 +161,7 @@ func (m *Module) Collect(log *zerolog.Logger) error {
}

func handleItem(
ctx context.Context,
wg *sync.WaitGroup,
errCh chan<- error,
ignore *[]string,
Expand All @@ -160,14 +170,24 @@ func handleItem(
) {
defer wg.Done()

if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

to, err := mkdir(fmt.Sprintf("%s/%s", buildDirectory, item.RelativePath))
if err != nil {
errCh <- err
return
}

for _, from := range item.Paths {
if err := CheckContextActivity(ctx); err != nil {
errCh <- err
return
}

wg.Add(1)
go copyFromPath(wg, errCh, ignore, from, to, item.IfFileExists)
go copyFromPath(ctx, wg, errCh, ignore, from, to, item.IfFileExists)
}
}
10 changes: 10 additions & 0 deletions internal/validators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -38,3 +39,12 @@ func ValidateVersion(version string) error {

return fmt.Errorf("invalid module version %s", version)
}

func CheckContextActivity(ctx context.Context) error {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled: %w", ctx.Err())
default:
return nil
}
}

0 comments on commit 2c70096

Please sign in to comment.