Skip to content

Commit

Permalink
refactor: rename pkgs (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas authored Oct 27, 2023
1 parent a735362 commit 0f41cab
Show file tree
Hide file tree
Showing 195 changed files with 556 additions and 975 deletions.
71 changes: 71 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"context"
"errors"
"fmt"
"io/fs"
"os"

"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/config"
"github.com/charmbracelet/soft-serve/pkg/db"
"github.com/charmbracelet/soft-serve/pkg/hooks"
"github.com/charmbracelet/soft-serve/pkg/store"
"github.com/charmbracelet/soft-serve/pkg/store/database"
"github.com/spf13/cobra"
)

// InitBackendContext initializes the backend context.
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
}

// CloseDBContext closes the database context.
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
}

// InitializeHooks initializes the hooks.
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
}
34 changes: 18 additions & 16 deletions cmd/soft/admin.go → cmd/soft/admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package main
package admin

import (
"fmt"

"github.com/charmbracelet/soft-serve/server/backend"
"github.com/charmbracelet/soft-serve/server/config"
"github.com/charmbracelet/soft-serve/server/db"
"github.com/charmbracelet/soft-serve/server/db/migrate"
"github.com/charmbracelet/soft-serve/cmd"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/config"
"github.com/charmbracelet/soft-serve/pkg/db"
"github.com/charmbracelet/soft-serve/pkg/db/migrate"
"github.com/spf13/cobra"
)

var (
adminCmd = &cobra.Command{
// Command is the admin command.
Command = &cobra.Command{
Use: "admin",
Short: "Administrate the server",
}

migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate the database to the latest version",
PersistentPreRunE: initBackendContext,
PersistentPostRunE: closeDBContext,
PersistentPreRunE: cmd.InitBackendContext,
PersistentPostRunE: cmd.CloseDBContext,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
db := db.FromContext(ctx)
Expand All @@ -35,8 +37,8 @@ var (
rollbackCmd = &cobra.Command{
Use: "rollback",
Short: "Rollback the database to the previous version",
PersistentPreRunE: initBackendContext,
PersistentPostRunE: closeDBContext,
PersistentPreRunE: cmd.InitBackendContext,
PersistentPostRunE: cmd.CloseDBContext,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
db := db.FromContext(ctx)
Expand All @@ -51,13 +53,13 @@ var (
syncHooksCmd = &cobra.Command{
Use: "sync-hooks",
Short: "Update repository hooks",
PersistentPreRunE: initBackendContext,
PersistentPostRunE: closeDBContext,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
PersistentPreRunE: cmd.InitBackendContext,
PersistentPostRunE: cmd.CloseDBContext,
RunE: func(c *cobra.Command, _ []string) error {
ctx := c.Context()
cfg := config.FromContext(ctx)
be := backend.FromContext(ctx)
if err := initializeHooks(ctx, cfg, be); err != nil {
if err := cmd.InitializeHooks(ctx, cfg, be); err != nil {
return fmt.Errorf("initialize hooks: %w", err)
}

Expand All @@ -67,7 +69,7 @@ var (
)

func init() {
adminCmd.AddCommand(
Command.AddCommand(
syncHooksCmd,
migrateCmd,
rollbackCmd,
Expand Down
14 changes: 7 additions & 7 deletions cmd/soft/browse.go → cmd/soft/browse/browse.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package browse

import (
"fmt"
Expand All @@ -9,15 +9,16 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/server/proto"
"github.com/charmbracelet/soft-serve/server/ui/common"
"github.com/charmbracelet/soft-serve/server/ui/components/footer"
"github.com/charmbracelet/soft-serve/server/ui/pages/repo"
"github.com/charmbracelet/soft-serve/pkg/proto"
"github.com/charmbracelet/soft-serve/pkg/ui/common"
"github.com/charmbracelet/soft-serve/pkg/ui/components/footer"
"github.com/charmbracelet/soft-serve/pkg/ui/pages/repo"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)

var browseCmd = &cobra.Command{
// Command is the browse command.
var Command = &cobra.Command{
Use: "browse PATH",
Short: "Browse a repository",
Args: cobra.MaximumNArgs(1),
Expand Down Expand Up @@ -72,7 +73,6 @@ func init() {
// HACK: This is a hack to hide the clone url
// TODO: Make this configurable
common.CloneCmd = func(publicURL, name string) string { return "" }
rootCmd.AddCommand(browseCmd)
}

type state int
Expand Down
72 changes: 15 additions & 57 deletions cmd/soft/hook.go → cmd/soft/hook/hook.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package hook

import (
"bufio"
Expand All @@ -13,9 +13,10 @@ import (
"strings"

"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/server/backend"
"github.com/charmbracelet/soft-serve/server/config"
"github.com/charmbracelet/soft-serve/server/hooks"
"github.com/charmbracelet/soft-serve/cmd"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/config"
"github.com/charmbracelet/soft-serve/pkg/hooks"
"github.com/spf13/cobra"
)

Expand All @@ -26,23 +27,24 @@ var (
// Deprecated: this flag is ignored.
configPath string

hookCmd = &cobra.Command{
// Command is the hook command.
Command = &cobra.Command{
Use: "hook",
Short: "Run git server hooks",
Long: "Handles Soft Serve git server hooks.",
Hidden: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
logger := log.FromContext(cmd.Context())
if err := initBackendContext(cmd, args); err != nil {
PersistentPreRunE: func(c *cobra.Command, args []string) error {
logger := log.FromContext(c.Context())
if err := cmd.InitBackendContext(c, args); err != nil {
logger.Error("failed to initialize backend context", "err", err)
return ErrInternalServerError
}

return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
logger := log.FromContext(cmd.Context())
if err := closeDBContext(cmd, args); err != nil {
PersistentPostRunE: func(c *cobra.Command, args []string) error {
logger := log.FromContext(c.Context())
if err := cmd.CloseDBContext(c, args); err != nil {
logger.Error("failed to close backend", "err", err)
return ErrInternalServerError
}
Expand Down Expand Up @@ -147,8 +149,8 @@ var (
)

func init() {
hookCmd.PersistentFlags().StringVar(&configPath, "config", "", "path to config file (deprecated)")
hookCmd.AddCommand(
Command.PersistentFlags().StringVar(&configPath, "config", "", "path to config file (deprecated)")
Command.AddCommand(
preReceiveCmd,
updateCmd,
postReceiveCmd,
Expand All @@ -163,47 +165,3 @@ func runCommand(ctx context.Context, in io.Reader, out io.Writer, err io.Writer,
cmd.Stderr = err
return cmd.Run()
}

const updateHookExample = `#!/bin/sh
#
# An example hook script to echo information about the push
# and send it to the client.
#
# To enable this hook, rename this file to "update" and make it executable.
refname="$1"
oldrev="$2"
newrev="$3"
# Safety check
if [ -z "$GIT_DIR" ]; then
echo "Don't run this script from the command line." >&2
echo " (if you want, you could supply GIT_DIR then run" >&2
echo " $0 <ref> <oldrev> <newrev>)" >&2
exit 1
fi
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
echo "usage: $0 <ref> <oldrev> <newrev>" >&2
exit 1
fi
# Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
zero=$(git hash-object --stdin </dev/null | tr '[0-9a-f]' '0')
if [ "$newrev" = "$zero" ]; then
newrev_type=delete
else
newrev_type=$(git cat-file -t $newrev)
fi
echo "Hi from Soft Serve update hook!"
echo
echo "Repository: $SOFT_SERVE_REPO_NAME"
echo "RefName: $refname"
echo "Change Type: $newrev_type"
echo "Old SHA1: $oldrev"
echo "New SHA1: $newrev"
exit 0
`
Loading

0 comments on commit 0f41cab

Please sign in to comment.