From da4219a3ca638fc166caa64b8c2f651c5d24481e Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 17 Jun 2023 08:44:18 -0600 Subject: [PATCH] wip --- vicuna-tools.d/main.go | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 vicuna-tools.d/main.go diff --git a/vicuna-tools.d/main.go b/vicuna-tools.d/main.go new file mode 100644 index 0000000..0c0b340 --- /dev/null +++ b/vicuna-tools.d/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "context" + "flag" + "log" + "os" +) + +var Config struct { + Port int + SessionD string +} + +func main() { + ctx, cleanup := contextWithCleanup(context.Background()) + defer cleanup() + config(ctx) + + log.Printf("%+v", Config) +} + +func contextWithCleanup(ctx context.Context) (context.Context, func()) { + m := map[int]func(){} + ctx = context.WithValue(ctx, "__cleanup__", m) + return ctx, func() { + defer func() { + recover() + }() + for _, v := range m { + v() + } + } +} + +func contextWithCleanupFunc(ctx context.Context, foo func()) { + v := ctx.Value("__cleanup__") + if v == nil { + panic("cannot get context with cleanup func that doesnt have cleanup init") + } + m := v.(map[int]func()) + m[len(m)] = foo +} + +func config(ctx context.Context) { + d, err := os.MkdirTemp(os.TempDir(), "ai.*") + if err != nil { + panic(err) + } + contextWithCleanupFunc(ctx, func() { os.RemoveAll(d) }) + + flag.IntVar(&Config.Port, "p", 8080, "port to listen on") + flag.StringVar(&Config.SessionD, "d", d, "dir to store sessions") + flag.Parse() +}