Skip to content

✨ Add name field to File #6416

Closed

Description

@sagikazarmark

What are you trying to do?

Files passed to dagger call currently does not preserve the name of a file passed to it.

Why is this important to you?

Some tools often require the original file name (for example: Spectral requires the file name do determine the file type)

Discussed it with @sipsma here: https://discord.com/channels/707636530424053791/1120503349599543376/1195832958150529165

How are you currently working around this?

I pass the original file name to the module.

Activity

  1. jedevc commented on Jan 15, 2024

    @jedevc
    Contributor

    We'd probably also want this for Directory as well as File.

    Out of curiosity, would we want a path field, or a name field (that just returns the last component)? I was thinking that it could be nice to be a bit more generic and allow getting the whole path - but one issue with this is that suppose I compare dir.File("foo/bar").Path() and dir.Dir("foo").File("bar").Path(). It feels like both of these should return the same string path value, but in each case, they both have different paths within their "root" directory (the first has "/" as it's root, the second has "/foo"). Maybe just having the last component here would make this easier? @sagikazarmark can you imagine a good case for getting the whole path, or would just the filename (which contains the file extension) be enough for your use case?

  2. sagikazarmark commented on Jan 15, 2024

    @sagikazarmark
    ContributorAuthor

    Most of the cases I have are just for the name.

    Looking at Go's os.File: it also has a name, but I don't think it knows about the path.

  3. sagikazarmark commented on Jan 16, 2024

    @sagikazarmark
    ContributorAuthor

    Some of the use cases I have:

    • Some tools need the file extension to determine the content type (not a fool proof solution, but currently impossible when passing in a File)
    • GitHub releases need a file name. I tried creating a custom struct with an additional name attribute, but it's currently impossible to use custom structs as function parameters
    • When mounting several files in a container, I often need to use workarounds, like numbering (ie. file-1) due to not knowing the original file name. It makes debugging harder when there is a problem with one of the files.

    I'm sure there are other scenarios where a file name would be useful, but these are the ones I had so far and probably the most common ones, too.

  4. helderco commented on Jan 16, 2024

    @helderco
    Contributor

    I think it does preserve the name, just not exposed. If you add with empty path it should default to using that name:

    package main
    
    type Test struct{}
    
    func (m *Test) Echo(src *File) *Directory {
    	return dag.Directory().WithFile("", src)
    }
    $ dagger -m test call echo --src=README.md
    ✔ dagger call echo [0.39s]
    ┃ README.md
    • Engine: e99dcdd78dee (version devel ())
    ⧗ 1.38s ✔ 66 ∅ 9
  5. sagikazarmark commented on Jan 17, 2024

    @sagikazarmark
    ContributorAuthor

    I guess that’s a good start, but a little inconvenient if I need the file name as an argument of a command.

  6. sagikazarmark commented on Jan 17, 2024

    @sagikazarmark
    ContributorAuthor

    This doesn't work:

    func (m *Gh) FileName() *Container {
    	file := dag.Host().File("dagger.json")
    
    	return dag.Container().From("alpine:latest").WithWorkdir("/work").WithFile("/work/", file).WithExec([]string{"sh", "-c", "pwd && ls -lah"})
    }
    ✔ dagger call file-name [2.31s]
    ┃ /work                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
    ┃ total 12K                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    ┃ drwxr-xr-x    2 root     root        4.0K Jan 17 04:30 .                                                                                                                                                                                                                                                                                                                                                                                                                          
    ┃ drwxr-xr-x    1 root     root        4.0K Jan 17 04:30 ..                                                                                                                                                                                                                                                                                                                                                                                                                         
    ┃ -rw-r--r--    2 root     root          34 Jan 15 17:21 work                                                                                                                                                                                                                                                                                                                                                                                                                       
    • Engine: a045aaae0d17 (version v0.9.6)
    

    This does:

    func (m *Gh) FileName() *Container {
    	file := dag.Host().File("dagger.json")
    
    	dir := dag.Directory().WithFile("", file)
    
    	return dag.Container().From("alpine:latest").WithWorkdir("/work").WithDirectory("/work/", dir).WithExec([]string{"sh", "-c", "pwd && ls -lah"})
    }
    ┃ /work                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
    ┃ total 12K                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    ┃ drwxr-xr-x    2 root     root        4.0K Jan 17 04:32 .                                                                                                                                                                                                                                                                                                                                                                                                                          
    ┃ drwxr-xr-x    1 root     root        4.0K Jan 17 04:32 ..                                                                                                                                                                                                                                                                                                                                                                                                                         
    ┃ -rw-r--r--    2 root     root          34 Jan 15 17:21 dagger.json                                                                                                                                                                                                                                                                                                                                                                                                                
    • Engine: a045aaae0d17 (version v0.9.6)
    

    As I said, it's a little inconvenient, but I realized that file.Name() would probably require a context and a trip to the engine as well, so it doesn't matter that much if I need to call Entries on an empty directory.

  7. sagikazarmark commented on Jan 17, 2024

    @sagikazarmark
    ContributorAuthor

    After giving it a try, I think I still prefer getting the file name directly. This feels a bit hacky (see linked PR).

  8. sagikazarmark commented on Jan 17, 2024

    @sagikazarmark
    ContributorAuthor

    🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions