Skip to content

initial Directory + File implementations - #3168

Merged
vito merged 7 commits into
dagger:cloakfrom
vito:core-directory
Sep 28, 2022
Merged

vito merged 7 commits into
dagger:cloakfrom
vito:core-directory

Conversation

@vito

@vito vito commented Sep 28, 2022 •

Copy link
Copy Markdown
Contributor

Implements the following portions of the API:

directory(id: DirectoryID)

type Directory {
  id: DirectoryID!
  contents(path: String!): String!
  file(path: String!): File!
  withNewFile(path: String!, contents: String): Directory!
  withCopiedFile(path: String!, source: FileID!): Directory!
  directory(path: String!): Directory!
  withDirectory(path: String!, directory: DirectoryID!): Directory!
}

type File {
  id: FileID!
  contents: String!
  size: Int!
}

The rest of the spec is stubbed out to return errors. Also changes git { tree } to return a Directory! instead of a Filesystem!.

Also introduces some utilities for encoding/decoding IDs and defining ID scalar types in the schema. I went a little ham with generics here, probably didn't need to, but they're shiny.

@netlify

netlify Bot commented Sep 28, 2022 •

Copy link
Copy Markdown

👷 Deploy request for cloak-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 50ead15

@vito
vito requested review from aluzzardi, shykes and sipsma and removed request for aluzzardi September 28, 2022 16:48
+ File { id, contents, size }
+ FileID
+ file(FileID!)
+ directory { file }

Signed-off-by: Alex Suraci <[email protected]>
@vito vito changed the title initial directory schema initial Directory + File implementations Sep 28, 2022

@aluzzardi aluzzardi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great! Left a minor question.

Linter is complaining about a couple of things, LGTM otherwise

Comment thread core/directory.go
)

// Directory is a content-addressed directory.
type Directory struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the goal to move away from "models" being in a separate package (e.g. retire core/filesystem)?

I'm totally onboard with that, this is simpler, just wanted to double check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm open to rearranging things, but yeah I figured I'd start with just putting it all together.

One option could be to move the schema glue out, but I think I would still prefer one big package for all the models to live in since they're inter-dependent (possibly with circles at some point?) and share a lot of common behavior (e.g. FooID scalar definitions and encoding/decoding). If we were to do per-type packages I think we'd end up with a /util package somewhere which feels a little awkward to me.

also remove unnecessary empty ID check

Signed-off-by: Alex Suraci <[email protected]>
Comment thread core/directory.go
}

// be sure to create the file under the working directory
dest = path.Join(cwd, dest)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My expectation would have been that if I provide a relative dest then it should be relative to cwd, but if dest is absolute then it should just be what I tell it. Not sure if that's aligned with everyone else's expectations, so not a blocker if others disagree.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm trying to form an opinion on this, but don't understand what cwd is. Seems like an implementation detail?

@vito vito Sep 28, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This line is to handle this possibly-edge case:

directory {
  withNewFile(path: "foo/bar", contents: "bar") {
    directory(path: "foo") {
      withNewFile(path: "baz", contents: "baz") {
        id
      }
    }
  }
}

The intention above is to create this file tree, since I did the withNewFile relative to the inner directory {}:

foo/bar
foo/baz

But without this line it would end up creating it in the root of the outer directory:

foo/bar
baz

And I think what @sipsma is suggesting is that if I explicitly pass an absolute path, like this:

directory {
  withNewFile(path: "foo/bar", contents: "bar") {
    directory(path: "foo") {
      withNewFile(path: "/baz", contents: "baz") { # note absolute path
        id
      }
    }
  }
}

...then it should end up with the above result, because I explicitly told it to create the file in the root of the directory.

foo/bar
baz

I think that makes sense from the standpoint of respecting the path syntax, but I wonder if it'll be hard to tell where exactly the "root" is in practice. 🤔 For example, if I take the id of the inner directory and hop into it from a context-free directory(id: $id) it feels a little weird to allow the user to create files outside of it.

directory(id: $subdir) {
  withNewFile(path: "/absolute", content: "abs") {
    id
  }
}

I suppose you'll still get an ID back with the outer directory modified, but I don't know how you would gain access to the file you created, unless we add an accessor to the parent dir.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ohhhh sorry, I totally misunderstood what cwd was actually. I just instantly assumed it was the implementation of "workdir" since that's usually what I think of when I see cwd, but that doesn't even make sense in Directory yet.

I agree that this should always be relative to the subdir (aka cwd var here) no matter whether it's relative or absolute. We should probably treat going to a subdir of a Directory spiritually the same as a chroot, shouldn't be able to access any files above that subdir once it's been selected.

@vito vito Sep 28, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We should probably treat going to a subdir of a Directory spiritually the same as a chroot, shouldn't be able to access any files above that subdir once it's been selected.

Agreed!

And yeah the cwd name might be a bit misleading.

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No blocking comments, just little things. Looks amazing, a huge improvement over our previous implementation already!

Comment thread core/directory.go Outdated
return nil, err
}

st = st.File(llb.Copy(srcSt, srcCwd, path.Join(cwd, subdir)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nb: does this create any missing parent dirs if they don't exist yet?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, it didn't; fixed and added a test. I think it makes sense for it to create the parents to be consistent with withNewFile, but we can reconsider both in the future.

Comment thread core/util.go
}

// stringResolver is used to generate a scalar resolver for a stringable type.
func stringResolver[T ~string](sample T) router.ScalarResolver {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🆒

@vito
vito merged commit 03ea15e into dagger:cloak Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants