package main

// GENERATED BY BUD. DO NOT EDIT.

{{- if $.Imports }}

import (
	{{- range $import := $.Imports }}
	{{$import.Name}} "{{$import.Path}}"
	{{- end }}
)
{{- end }}

// main entrypoint
func main() {
	ctx := context.Background()
	if err := run(ctx); err != nil {
		if errors.Is(err, context.Canceled) {
			return
		}
		console.Error(err.Error())
		os.Exit(1)
	}
}

func run(ctx context.Context) error {
	cmd := new(Command)
	cli := commander.New("afs")
	cli.Flag("chdir", "change the working directory").Short('C').String(&cmd.Dir).Default(".")
	cli.Flag("embed", "embed assets").Bool(&cmd.Flag.Embed).Default(false)
	cli.Flag("hot", "hot reloading").Bool(&cmd.Flag.Hot).Default(true)
	cli.Flag("minify", "minify assets").Bool(&cmd.Flag.Minify).Default(false)
	cli.Flag("listen", "address to serve from").String(&cmd.Listen).Default(":65000")
	cli.Flag("log", "filter logs with this pattern").Short('L').String(&cmd.Log).Default("info")
	cli.Run(cmd.Serve)
	return cli.Parse(ctx, os.Args[1:])
}

type Command struct {
	Dir    string
	Flag   framework.Flag
	Listen string
	Log    string
}


// Serve the filesystem
func (c *Command) Serve(ctx context.Context) (err error) {
	module, err := gomod.Find(c.Dir)
	if err != nil {
		return err
	}
	log, err := afsrt.Logger(c.Log)
	if err != nil {
		return err
	}
	gfs, err := afsrt.GenFS(module, log)
	if err != nil {
		return err
	}
	parser := parser.New(gfs, module)
	injector := di.New(gfs, log, module, parser)
	_ = injector
	budClient, err := budhttp.Try(log, os.Getenv("BUD_DEV_URL"))
	if err != nil {
		return err
	}
	fsys, err := {{ $.Provider.Name }}(
		{{- /* Order matters. Ordered by package name (e.g. budfs.*Server > context.Context) */}}
		{{- if $.Provider.Variable "github.com/livebud/bud/package/budhttp.Client" }}budClient,{{ end }}
		{{- if $.Provider.Variable "context.Context" }}ctx,{{ end }}
		{{- if $.Provider.Variable "github.com/livebud/bud/package/di.*Injector" }}injector,{{ end }}
		{{- if $.Provider.Variable "github.com/livebud/bud/framework.*Flag" }}&c.Flag,{{ end }}
		{{- if $.Provider.Variable "github.com/livebud/bud/package/genfs.*FileSystem" }}gfs,{{ end }}
		{{- if $.Provider.Variable "github.com/livebud/bud/package/gomod.*Module" }}module,{{ end }}
		{{- if $.Provider.Variable "github.com/livebud/bud/package/log.Log" }}log,{{ end }}
		{{- if $.Provider.Variable "github.com/livebud/bud/package/parser.*Parser" }}parser,{{ end }}
	)
	if err != nil {
		return err
	}
	return afsrt.Serve(ctx, log, fsys, c.Listen)
}

// {{ $.Provider.Name }} provides the generator filesystem
{{ $.Provider.Function }}