Skip to content

Commit

Permalink
refactor: rename pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 27, 2023
1 parent a735362 commit 4d5fddb
Show file tree
Hide file tree
Showing 195 changed files with 549 additions and 975 deletions.
68 changes: 68 additions & 0 deletions cmd/cmd.go
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"
"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"
)

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)
}

Check warning on line 25 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L19-L25

Added lines #L19 - L25 were not covered by tests
}
dbx, err := db.Open(ctx, cfg.DB.Driver, cfg.DB.DataSource)
if err != nil {
return fmt.Errorf("open database: %w", err)
}

Check warning on line 30 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L27-L30

Added lines #L27 - L30 were not covered by tests

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

Check warning on line 40 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L32-L40

Added lines #L32 - L40 were not covered by tests
}

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)
}

Check warning on line 49 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L43-L49

Added lines #L43 - L49 were not covered by tests
}

return nil

Check warning on line 52 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L52

Added line #L52 was not covered by tests
}

func InitializeHooks(ctx context.Context, cfg *config.Config, be *backend.Backend) error {
repos, err := be.Repositories(ctx)
if err != nil {
return err

Check failure on line 58 in cmd/cmd.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func (*github.com/charmbracelet/soft-serve/pkg/backend.Backend).Repositories(ctx context.Context) ([]github.com/charmbracelet/soft-serve/pkg/proto.Repository, error) (wrapcheck)
}

Check warning on line 59 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L55-L59

Added lines #L55 - L59 were not covered by tests

for _, repo := range repos {
if err := hooks.GenerateHooks(ctx, cfg, repo.Name()); err != nil {
return err

Check failure on line 63 in cmd/cmd.go

View workflow job for this annotation

GitHub Actions / lint-soft

error returned from external package is unwrapped: sig: func github.com/charmbracelet/soft-serve/pkg/hooks.GenerateHooks(_ context.Context, cfg *github.com/charmbracelet/soft-serve/pkg/config.Config, repo string) error (wrapcheck)
}

Check warning on line 64 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L61-L64

Added lines #L61 - L64 were not covered by tests
}

return nil

Check warning on line 67 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L67

Added line #L67 was not covered by tests
}
33 changes: 17 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,27 @@
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 = &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 +36,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 +52,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 +68,7 @@ var (
)

func init() {
adminCmd.AddCommand(
Command.AddCommand(
syncHooksCmd,
migrateCmd,
rollbackCmd,
Expand Down
13 changes: 6 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,15 @@ 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{
var Command = &cobra.Command{
Use: "browse PATH",
Short: "Browse a repository",
Args: cobra.MaximumNArgs(1),
Expand Down Expand Up @@ -72,7 +72,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
71 changes: 14 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,23 @@ var (
// Deprecated: this flag is ignored.
configPath string

hookCmd = &cobra.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 +148,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 +164,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 4d5fddb

Please sign in to comment.