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)
}
