From 7b21727285e50b0b050c4ddd70d07542229c5366 Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:17:03 -0700 Subject: init --- go/gofs.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 go/gofs.go (limited to 'go/gofs.go') diff --git a/go/gofs.go b/go/gofs.go new file mode 100644 index 0000000..4b47c81 --- /dev/null +++ b/go/gofs.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "io/fs" + "log" + "net/url" + "os" + "path/filepath" + + "github.com/pkg/sftp" +) + +func main() { + // Parse the command-line arguments + if len(os.Args) < 2 { + log.Fatalf("Usage: %v [user@domain.com:/directory/i/want/to/mount]", os.Args[0]) + } + remoteAddr := os.Args[1] + + // Connect to the remote server using SFTP + sshConfig := &ssh.ClientConfig{ + User: "username", + Auth: []ssh.AuthMethod{ + ssh.Password("password"), + }, + } + conn, err := ssh.Dial("tcp", remoteAddr, sshConfig) + if err != nil { + log.Fatalf("Failed to connect to remote server: %v", err) + } + client, err := sftp.NewClient(conn) + if err != nil { + log.Fatalf("Failed to create SFTP client: %v", err) + } + + // List the contents of the remote directory + entries, err := client.ReadDir("/path/to/remote/directory") + if err != nil { + log.Fatalf("Failed to list remote directory: %v", err) + } + + // Create a local filesystem for the remote directory + fs := os.DirFS("/path/to/local/directory") + + // Add each file from the remote directory to the local filesystem + for _, entry := range entries { + filename := filepath.Join(fs, entry.Name()) + if err = fs.MkdirAll(filepath.Dir(filename), 0755); err != nil { + log.Fatalf("Failed to create directory: %v", err) + } + if _, err := client.OpenFile(entry.Name(), os.O_RDONLY, fs); err != nil { + log.Fatalf("Failed to open remote file: %v", err) + } + } + + // Mount the local filesystem as a directory in the client's namespace + if err = os.MkdirAll("/path/to/mounted/directory", 0755); err != nil { + log.Fatalf("Failed to create mount point: %v", err) + } + if err := syscall.Mount(fs, "/path/to/mounted/directory", "", syscall.MS_RDONLY, ""); err != nil { + log.Fatalf("Failed to mount filesystem: %v", err) + } + + // List the contents of the local directory to verify that it has been mounted correctly + entries, err = os.ReadDir("/path/to/mounted/directory") + if err != nil { + log.Fatalf("Failed to list mounted directory: %v", err) + } + fmt.Println(entries) +} -- cgit v1.2.3