-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
|
||
"github.com/charmbracelet/soft-serve/pkg/backend" | ||
Check failure on line 10 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 10 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 10 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 10 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 10 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 10 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 10 in cmd/cmd.go GitHub Actions / test_postgres
Check failure on line 10 in cmd/cmd.go GitHub Actions / test_postgres
|
||
"github.com/charmbracelet/soft-serve/pkg/config" | ||
Check failure on line 11 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 11 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 11 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 11 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 11 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 11 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 11 in cmd/cmd.go GitHub Actions / test_postgres
Check failure on line 11 in cmd/cmd.go GitHub Actions / test_postgres
|
||
"github.com/charmbracelet/soft-serve/pkg/db" | ||
Check failure on line 12 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 12 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 12 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 12 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 12 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 12 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 12 in cmd/cmd.go GitHub Actions / test_postgres
Check failure on line 12 in cmd/cmd.go GitHub Actions / test_postgres
|
||
"github.com/charmbracelet/soft-serve/pkg/hooks" | ||
Check failure on line 13 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 13 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 13 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 13 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 13 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 13 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 13 in cmd/cmd.go GitHub Actions / test_postgres
Check failure on line 13 in cmd/cmd.go GitHub Actions / test_postgres
|
||
"github.com/charmbracelet/soft-serve/pkg/store" | ||
Check failure on line 14 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 14 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 14 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 14 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 14 in cmd/cmd.go GitHub Actions / test_postgres
Check failure on line 14 in cmd/cmd.go GitHub Actions / test_postgres
|
||
"github.com/charmbracelet/soft-serve/pkg/store/database" | ||
Check failure on line 15 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 15 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 15 in cmd/cmd.go GitHub Actions / build / build (^1, ubuntu-latest)
Check failure on line 15 in cmd/cmd.go GitHub Actions / build / govulncheck
Check failure on line 15 in cmd/cmd.go GitHub Actions / test_postgres
Check failure on line 15 in cmd/cmd.go GitHub Actions / test_postgres
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func InitBackendContext(cmd *cobra.Command, _ []string) error { | ||
ctx := cmd.Context() | ||
cfg := config.FromContext(ctx) | ||
if _, err := os.Stat(cfg.DataPath); errors.Is(err, fs.ErrNotExist) { | ||
if err := os.MkdirAll(cfg.DataPath, os.ModePerm); err != nil { | ||
return fmt.Errorf("create data directory: %w", err) | ||
} | ||
} | ||
dbx, err := db.Open(ctx, cfg.DB.Driver, cfg.DB.DataSource) | ||
if err != nil { | ||
return fmt.Errorf("open database: %w", err) | ||
} | ||
|
||
ctx = db.WithContext(ctx, dbx) | ||
dbstore := database.New(ctx, dbx) | ||
ctx = store.WithContext(ctx, dbstore) | ||
be := backend.New(ctx, cfg, dbx) | ||
ctx = backend.WithContext(ctx, be) | ||
|
||
cmd.SetContext(ctx) | ||
|
||
return nil | ||
} | ||
|
||
func CloseDBContext(cmd *cobra.Command, _ []string) error { | ||
ctx := cmd.Context() | ||
dbx := db.FromContext(ctx) | ||
if dbx != nil { | ||
if err := dbx.Close(); err != nil { | ||
return fmt.Errorf("close database: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func InitializeHooks(ctx context.Context, cfg *config.Config, be *backend.Backend) error { | ||
repos, err := be.Repositories(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, repo := range repos { | ||
if err := hooks.GenerateHooks(ctx, cfg, repo.Name()); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |