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

Move common commands to commoncommands #2660

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Move common commands to commoncommands
  • Loading branch information
AlekSi committed May 18, 2023
commit 0f038866c063fb27ba583e5aa96f52c840cfdda5
4 changes: 2 additions & 2 deletions internal/clientconn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/FerretDB/FerretDB/internal/clientconn/conninfo"
"github.com/FerretDB/FerretDB/internal/clientconn/connmetrics"
"github.com/FerretDB/FerretDB/internal/handlers"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/proxy"
"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -541,7 +541,7 @@ func (c *conn) route(ctx context.Context, reqHeader *wire.MsgHeader, reqBody wir
}

func (c *conn) handleOpMsg(ctx context.Context, msg *wire.OpMsg, command string) (*wire.OpMsg, error) {
if cmd, ok := common.Commands[command]; ok {
if cmd, ok := commoncommands.Commands[command]; ok {
if cmd.Handler != nil {
// TODO move it to route, closer to Prometheus metrics
defer trace.StartRegion(ctx, command).End()
Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/common/aggregations/stages/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
// newStageFunc is a type for a function that creates a new aggregation stage.
type newStageFunc func(stage *types.Document) (aggregations.Stage, error)

// stages maps all supported aggregation stages.
var stages = map[string]newStageFunc{
// Stages maps all supported aggregation Stages.
var Stages = map[string]newStageFunc{
// sorted alphabetically
"$collStats": newCollStats,
"$count": newCount,
Expand Down Expand Up @@ -89,7 +89,7 @@ func NewStage(stage *types.Document) (aggregations.Stage, error) {

name := stage.Command()

f, supported := stages[name]
f, supported := Stages[name]
_, unsupported := unsupportedStages[name]

switch {
Expand Down
50 changes: 0 additions & 50 deletions internal/handlers/common/parse_osrelease.go

This file was deleted.

16 changes: 16 additions & 0 deletions internal/handlers/commoncommands/commoncommands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package commoncommands provides command handlers shared by all handlers.
package commoncommands
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
"strconv"

"github.com/FerretDB/FerretDB/build/version"
"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations/stages"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgBuildInfo is a common implementation of the buildInfo command.
func MsgBuildInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/2650
_ = stages.Stages

var reply wire.OpMsg
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
"errors"
"strconv"
"strings"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand All @@ -34,7 +35,7 @@ func MsgDebugError(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return nil, lazyerrors.Error(err)
}

expected, err := GetRequiredParam[string](document, document.Command())
expected, err := common.GetRequiredParam[string](document, document.Command())
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"bufio"
"context"
"io"
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -82,3 +85,29 @@ func MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {

return &reply, nil
}

// parseOSRelease parses the /etc/os-release or /usr/lib/os-release file content,
// returning the OS name and version.
func parseOSRelease(r io.Reader) (string, string, error) {
scanner := bufio.NewScanner(r)

configParams := map[string]string{}
for scanner.Scan() {
key, value, ok := strings.Cut(scanner.Text(), "=")
if !ok {
continue
}

if v, err := strconv.Unquote(value); err == nil {
value = v
}

configParams[key] = value
}

if err := scanner.Err(); err != nil {
return "", "", lazyerrors.Error(err)
}

return configParams["NAME"], configParams["VERSION"], nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package commoncommands

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgBuildInfo implements HandlerInterface.
func (h *Handler) MsgBuildInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgBuildInfo(ctx, msg)
return commoncommands.MsgBuildInfo(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_connectionstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgConnectionStatus implements HandlerInterface.
func (h *Handler) MsgConnectionStatus(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgConnectionStatus(ctx, msg)
return commoncommands.MsgConnectionStatus(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_currentop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgCurrentOp implements HandlerInterface.
func (h *Handler) MsgCurrentOp(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgCurrentOp(ctx, msg)
return commoncommands.MsgCurrentOp(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_debugerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgDebugError implements HandlerInterface.
func (h *Handler) MsgDebugError(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgDebugError(ctx, msg)
return commoncommands.MsgDebugError(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_getcmdlineopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgGetCmdLineOpts implements HandlerInterface.
func (h *Handler) MsgGetCmdLineOpts(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgGetCmdLineOpts(ctx, msg)
return commoncommands.MsgGetCmdLineOpts(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_hostinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgHostInfo implements HandlerInterface.
func (h *Handler) MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgHostInfo(ctx, msg)
return commoncommands.MsgHostInfo(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_listcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgListCommands implements handlers.Interface.
func (h *Handler) MsgListCommands(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgListCommands(ctx, msg)
return commoncommands.MsgListCommands(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/hana/msg_whatsmyuri.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package hana
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgWhatsMyURI implements HandlerInterface.
func (h *Handler) MsgWhatsMyURI(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgWhatsMyURI(ctx, msg)
return commoncommands.MsgWhatsMyURI(ctx, msg)
}
4 changes: 2 additions & 2 deletions internal/handlers/pg/msg_buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package pg
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commoncommands"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgBuildInfo implements HandlerInterface.
func (h *Handler) MsgBuildInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
return common.MsgBuildInfo(ctx, msg)
return commoncommands.MsgBuildInfo(ctx, msg)
}
Loading