mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-03 07:55:05 +01:00
sync
This commit is contained in:
53
pkg/app/app.go
Normal file
53
pkg/app/app.go
Normal file
@@ -0,0 +1,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
|
||||
}
|
||||
45
pkg/app/config.go
Normal file
45
pkg/app/config.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/marcozac/go-jsonc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
}
|
||||
|
||||
type ConfigMCP struct {
|
||||
}
|
||||
|
||||
type ConfigProvider struct {
|
||||
}
|
||||
|
||||
type ErrInvalidConfig struct {
|
||||
source error
|
||||
}
|
||||
|
||||
func (e ErrInvalidConfig) Error() string {
|
||||
return "ErrInvalidConfig"
|
||||
}
|
||||
|
||||
func (e ErrInvalidConfig) Unwrap() error {
|
||||
return e.source
|
||||
}
|
||||
|
||||
func initConfig(directory string) (*Config, error) {
|
||||
configPath := filepath.Join(directory, "opencode.jsonc")
|
||||
_, err := os.Stat(configPath)
|
||||
result := &Config{}
|
||||
if err == nil {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err == nil {
|
||||
err := jsonc.Unmarshal(data, result)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidConfig{err}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
15
pkg/app/paths/paths.go
Normal file
15
pkg/app/paths/paths.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package paths
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
func Data(project string) string {
|
||||
return filepath.Join(project, ".opencode")
|
||||
}
|
||||
|
||||
func Storage(project string) string {
|
||||
return filepath.Join(Data(project), "storage")
|
||||
}
|
||||
|
||||
func Log(project string) string {
|
||||
return filepath.Join(Data(project), "log")
|
||||
}
|
||||
42
pkg/app/storage.go
Normal file
42
pkg/app/storage.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/sst/opencode/pkg/app/paths"
|
||||
"gocloud.dev/blob"
|
||||
"gocloud.dev/blob/fileblob"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
bucket *blob.Bucket
|
||||
app *App
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func initStorage(app *App) (*Storage, error) {
|
||||
var err error
|
||||
result := &Storage{
|
||||
app: app,
|
||||
}
|
||||
storageDir := paths.Storage(app.directory)
|
||||
if err := os.MkdirAll(storageDir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.bucket, err = fileblob.OpenBucket(storageDir, &fileblob.Options{
|
||||
NoTempDir: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type StorageWriterOptions = blob.WriterOptions
|
||||
|
||||
func (s *Storage) NewWriter(opts *StorageWriterOptions, path ...string) (*blob.Writer, error) {
|
||||
joined := filepath.Join(path...)
|
||||
return s.bucket.NewWriter(s.app.ctx, joined, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user