Skip to content

Commit

Permalink
feat: add more logs
Browse files Browse the repository at this point in the history
Signed-off-by: Kasper J. Hermansen <[email protected]>
  • Loading branch information
kjuulh committed Dec 5, 2023
1 parent c662fce commit 123bbd4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
16 changes: 11 additions & 5 deletions pkg/executors/golang/codegen/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ import (
"os"
"path"
"strings"

"github.com/lunarway/shuttle/pkg/ui"
)

type writeFileFunc = func(name string, contents []byte, permissions fs.FileMode) error

func PatchGoMod(rootDir string, shuttleLocalDir string) error {
func PatchGoMod(rootDir string, shuttleLocalDir string, ui *ui.UI) error {
return patchGoMod(
rootDir,
shuttleLocalDir,
ui,
os.WriteFile,
)
}

func patchGoMod(rootDir, shuttleLocalDir string, writeFileFunc writeFileFunc) error {
func patchGoMod(rootDir, shuttleLocalDir string, ui *ui.UI, writeFileFunc writeFileFunc) error {
packages := make(map[string]string, 0)

if rootWorkspaceExists(rootDir) {
ui.Verboseln("patching go action using workspace file")
modules, err := GetWorkspaceModules(rootDir)
if err != nil {
return fmt.Errorf("failed to parse go.mod in root of project: %w", err)
Expand All @@ -38,6 +42,7 @@ func patchGoMod(rootDir, shuttleLocalDir string, writeFileFunc writeFileFunc) er
}

} else if rootModExists(rootDir) {
ui.Verboseln("patching go action using mod file")
moduleName, modulePath, err := GetRootModule(rootDir)
if err != nil {
return fmt.Errorf("failed to parse go.mod in root of project: %w", err)
Expand All @@ -46,19 +51,19 @@ func patchGoMod(rootDir, shuttleLocalDir string, writeFileFunc writeFileFunc) er
packages[moduleName] = modulePath
}

if err := patchPackagesUsed(rootDir, shuttleLocalDir, packages, writeFileFunc); err != nil {
if err := patchPackagesUsed(rootDir, shuttleLocalDir, packages, ui, writeFileFunc); err != nil {
return err
}

return nil

}

func patchPackagesUsed(rootDir string, shuttleLocalDir string, packages map[string]string, writeFileFunc writeFileFunc) error {
func patchPackagesUsed(rootDir string, shuttleLocalDir string, packages map[string]string, ui *ui.UI, writeFileFunc writeFileFunc) error {
actionsModFilePath := path.Join(shuttleLocalDir, "tmp/go.mod")
relativeActionsModFilePath := strings.TrimPrefix(path.Join(strings.TrimPrefix(shuttleLocalDir, rootDir), "tmp/go.mod"), "/")

segmentsToRoot := strings.Count(relativeActionsModFilePath, "/")

actionsModFileContents, err := os.ReadFile(actionsModFilePath)
if err != nil {
return err
Expand All @@ -79,6 +84,7 @@ func patchPackagesUsed(rootDir string, shuttleLocalDir string, packages map[stri
if !actionsModFileContainsModule(moduleName) {
continue
}
ui.Verboseln("golang binary patch adding: moduleName: %s and modulePath: %s", moduleName, modulePath)

relativeToActionsModulePath := path.Join(strings.Repeat("../", segmentsToRoot), modulePath)

Expand Down
6 changes: 5 additions & 1 deletion pkg/executors/golang/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func Compile(ctx context.Context, ui *ui.UI, discovered *discover.Discovered) (*
binaries := &Binaries{}
if discovered.Local != nil {
egrp.Go(func() error {
ui.Verboseln("compiling golang actions binary for: %s", discovered.Local.DirPath)

path, err := compile(ctx, ui, discovered.Local)
if err != nil {
return err
Expand All @@ -59,6 +61,8 @@ func Compile(ctx context.Context, ui *ui.UI, discovered *discover.Discovered) (*
}
if discovered.Plan != nil {
egrp.Go(func() error {
ui.Verboseln("compiling golang actions binary for: %s", discovered.Plan.DirPath)

path, err := compile(ctx, ui, discovered.Plan)
if err != nil {
return err
Expand Down Expand Up @@ -113,7 +117,7 @@ func compile(ctx context.Context, ui *ui.UI, actions *discover.ActionsDiscovered

var binarypath string

if err := codegen.PatchGoMod(actions.ParentDir, shuttlelocaldir); err != nil {
if err := codegen.PatchGoMod(actions.ParentDir, shuttlelocaldir, ui); err != nil {
return "", fmt.Errorf("failed to patch generated go.mod: %w", err)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/executors/golang/compile/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/lunarway/shuttle/pkg/executors/golang/discover"
"github.com/lunarway/shuttle/pkg/ui"
"golang.org/x/exp/slices"
"golang.org/x/mod/sumdb/dirhash"
)

Expand Down Expand Up @@ -66,6 +67,7 @@ func GetHash(ctx context.Context, actions *discover.ActionsDiscovered) (string,
return io.NopCloser(bytes.NewReader(b)), nil
}

slices.Sort(entries)
hash, err := dirhash.Hash1(entries, open)
if err != nil {
return "", err
Expand Down
6 changes: 6 additions & 0 deletions pkg/executors/golang/executer/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"time"

"github.com/lunarway/shuttle/pkg/config"
"github.com/lunarway/shuttle/pkg/executors/golang/compile"
Expand All @@ -17,6 +18,8 @@ func prepare(
path string,
c *config.ShuttleProjectContext,
) (*compile.Binaries, error) {
ui.Verboseln("preparing shuttle golang actions")
start := time.Now()
log.SetFlags(log.LstdFlags | log.Lshortfile)

disc, err := discover.Discover(ctx, path, c)
Expand All @@ -29,5 +32,8 @@ func prepare(
return nil, fmt.Errorf("failed to compile binaries: %v", err)
}

elapsed := time.Since(start)
ui.Verboseln("preparing shuttle golang actions took: %d ms", elapsed.Milliseconds())

return binaries, nil
}
1 change: 1 addition & 0 deletions pkg/executors/golang/executer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Run(
return err
}

ui.Verboseln("executing shuttle golang actions")
if err := executeAction(ctx, binaries, args...); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/executors/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func executeTask(ctx context.Context, ui *ui.UI, context ActionExecutionContext)
args = append(args, value)
}

err := executer.Run(ctx, ui, &context.ScriptContext.Project, "shuttle.yaml", args...)
err := executer.Run(ctx, ui, &context.ScriptContext.Project, fmt.Sprintf("%s/shuttle.yaml", context.ScriptContext.Project.ProjectPath), args...)
if err != nil {
return err
}
Expand Down

0 comments on commit 123bbd4

Please sign in to comment.