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

Extracting filesystem accesses; 'workspace inspect' command #13

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions cmd/warpforge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func makeApp(stdin io.Reader, stdout, stderr io.Writer) *cli.App {
&statusCmdDef,
&quickstartCmdDef,
&ferkCmdDef,
&cli.Command{
Name: "workspace",
Usage: "Grouping for subcommands that inspect or affect a whole workspace.",
Subcommands: []*cli.Command{
&cmdDefWorkspaceInspect,
},
},
}
return app
}
Expand Down
77 changes: 77 additions & 0 deletions cmd/warpforge/winspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"fmt"
"io/fs"
"os"
"path/filepath"

"github.com/warpfork/warpforge/pkg/dab"

"github.com/urfave/cli/v2"
)

var cmdDefWorkspaceInspect = cli.Command{
Name: "inspect",
Usage: "Inspect and report upon the situation of the current workspace (how many modules are there, have we got a cached evaluation of them, etc).",
Action: cmdFnWorkspaceInspect,
// Aliases: []string{"winspect"}, // doesn't put them at the top level. Womp.
}

func cmdFnWorkspaceInspect(c *cli.Context) error {
fsys := os.DirFS("/")

// First, find the workspace.
wss, err := openWorkspaceSet(fsys)
if err != nil {
return fmt.Errorf("failed to open workspace set: %s", err)
}

// Briefly report on the nearest workspace.
// (We could talk about the grandparents too, but 'wf status' already does that; here we want to focus more on contents than parentage.)
wsFs, wsPath := wss.Stack[0].Path()
fmt.Fprintf(c.App.Writer, "Workspace: %s%s\n", wsFs, wsPath)

// Search for modules within the workspace.
fs.WalkDir(wsFs, wsPath, func(path string, d fs.DirEntry, err error) error {
// fmt.Fprintf(c.App.Writer, "hi: %s%s\n", wsFs, path)

if err != nil {
return err
}

// Don't ever look into warpforge guts directories.
if d.Name() == dab.MagicFilename_Workspace {
return fs.SkipDir
}

// If this is a dir (beyond the root): look see if it contains a workspace marker.
// If it does, we might not want to report on it.
// TODO: a bool flag for this.
if d.IsDir() && len(path) > len(wsPath) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider making this whole block a function.

_, e2 := fs.Stat(wsFs, filepath.Join(path, dab.MagicFilename_Workspace))
if e2 == nil || os.IsNotExist(e2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invert if block to get rid of else statement.

// carry on
} else {
return fs.SkipDir
}
}

// Peek for module file.
if d.Name() == dab.MagicFilename_Module {
modPathWithinWs := path[len(wsPath)+1 : len(path)-len(dab.MagicFilename_Module)] // leave the trailing slash on. For disambig in case we support multiple module files per dir someday.
mod, err := dab.ModuleFromFile(wsFs, path)
modName := mod.Name
if err != nil {
modName = "!!Unknown!!"
}

// Tell me about it.
fmt.Fprintf(c.App.Writer, "Module found: %q -- at path %q\n", modName, modPathWithinWs)
}

return nil
})

return nil
}
5 changes: 5 additions & 0 deletions pkg/dab/workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dab

const (
MagicFilename_Workspace = ".warpforge"
)
3 changes: 2 additions & 1 deletion pkg/workspace/fsdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"os"
"path/filepath"

"github.com/warpfork/warpforge/pkg/dab"
"github.com/warpfork/warpforge/wfapi"
)

const (
magicWorkspaceDirname = ".warpforge"
magicWorkspaceDirname = dab.MagicFilename_Workspace
)

var homedir string
Expand Down
2 changes: 1 addition & 1 deletion pkg/workspace/workspace_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type WorkspaceSet struct {
Home *Workspace
Root *Workspace
Stack []*Workspace
Stack []*Workspace // the 0'th index is the closest workspace; the next is its parent, and so on.
}

// Opens a full WorkspaceSet
Expand Down