summaryrefslogtreecommitdiffhomepage
path: root/pkg/app/app.go
blob: cb5f5e0276ca70173cbf61203823da7f47a03ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package app

import (
	"context"
	"log/slog"
	"os"
	"path/filepath"

	"github.com/sst/opencode/pkg/app/paths"
)

type App struct {
	ctx       context.Context
	directory string
	config    *Config
	storage   *Storage
}

func New(ctx context.Context, directory string) (*App, error) {
	var err error
	app := &App{
		directory: directory,
		ctx:       ctx,
	}

	data := paths.Data(directory)
	if err := os.MkdirAll(data, 0755); err != nil {
		return nil, err
	}

	err = os.MkdirAll(paths.Log(directory), 0755)
	if err != nil {
		return nil, err
	}
	logFile, err := os.Create(filepath.Join(paths.Log(directory), "opencode.log"))
	if err != nil {
		return nil, err
	}
	slog.SetDefault(slog.New(slog.NewTextHandler(logFile, &slog.HandlerOptions{})))
	slog.Info("log created")

	app.config, err = initConfig(directory)
	if err != nil {
		return nil, err
	}

	app.storage, err = initStorage(app)
	if err != nil {
		return nil, err
	}

	return app, nil
}