Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename pkgs #408

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}

Check warning on line 26 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L20-L26

Added lines #L20 - L26 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 31 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L28-L31

Added lines #L28 - L31 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 41 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L33-L41

Added lines #L33 - L41 were not covered by tests
}

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

Check warning on line 51 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L45-L51

Added lines #L45 - L51 were not covered by tests
}

return nil

Check warning on line 54 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L54

Added line #L54 was not covered by tests
}

// 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

Check failure on line 61 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 62 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L58-L62

Added lines #L58 - L62 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 66 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 67 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L64-L67

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

return nil

Check warning on line 70 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L70

Added line #L70 was not covered by tests
}
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
Loading