package main

import (
	"os"

	"github.com/spf13/cobra"

	"github.com/barakmich/kubelwagen"
)

var (
	hostport string
	nonempty bool
	readonly bool
)

func init() {
	rootCmd.AddCommand(serveCmd)
	serveCmd.PersistentFlags().StringVar(&hostport, "listen", "0.0.0.0:8889", "Host and port to listen on")
	serveCmd.PersistentFlags().BoolVar(&nonempty, "nonempty", false, "Allow mounting over a nonempty directory (default behavior in libfuse 3.x)")
	serveCmd.PersistentFlags().BoolVar(&readonly, "readonly", false, "Mount read-only")
}

var serveCmd = &cobra.Command{
	Use:   "serve DIR",
	Short: "Create a FUSE mount at DIR",
	Run:   serveRun,
}

func serveRun(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(1)
	}
	dir := args[0]
	opts := &kubelwagen.WsFsOpts{
		ReadOnly: readonly,
		NonEmpty: nonempty,
	}
	kubelwagen.Serve(hostport, dir, opts)
}
