Skip to content

Commit

Permalink
Move exec manager out into exec internal package (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Nov 24, 2023
1 parent 8827f6d commit 92d9709
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ tasks:
cmds:
- mockgen -source=internal/git.go -destination=internal/git/git_mock.go -package=git
- mockgen -source=internal/repository.go -destination=internal/repository/repository_mock.go -package=repository
- mockgen -source=internal/git/types.go -destination=internal/git/exec_mock.go -package=git
- mockgen -source=internal/exec.go -destination=internal/exec/exec_mock.go -package=exec
- mockgen -source=internal/repository/types.go -destination=internal/repository/copy_mock.go -package=repository
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/retr0h/go-gilt/internal"
"github.com/retr0h/go-gilt/internal/config"
"github.com/retr0h/go-gilt/internal/exec"
"github.com/retr0h/go-gilt/internal/git"
"github.com/retr0h/go-gilt/internal/repositories"
"github.com/retr0h/go-gilt/internal/repository"
Expand Down Expand Up @@ -135,7 +136,7 @@ func initConfig() {
logger,
)

execManager := git.NewExecManagerCmd(
execManager := exec.New(
appConfig.Debug,
logger,
)
Expand Down
26 changes: 26 additions & 0 deletions internal/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package internal

// ExecManager manager responsible for exec operations.
type ExecManager interface {
RunCmd(name string, args ...string) error
}
12 changes: 6 additions & 6 deletions internal/git/exec.go → internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package git
package exec

import (
"log/slog"
"os/exec"
"strings"
)

// NewExecManagerCmd factory to create a new exec manager instance.
func NewExecManagerCmd(
// New factory to create a new Exec instance.
func New(
debug bool,
logger *slog.Logger,
) *ExecManagerCmd {
return &ExecManagerCmd{
) *Exec {
return &Exec{
debug: debug,
logger: logger,
}
}

// RunCmd execute the provided command with args.
// Yeah, yeah, yeah, I know I cheated by using Exec in this package.
func (e *ExecManagerCmd) RunCmd(
func (e *Exec) RunCmd(
name string,
args ...string,
) error {
Expand Down
6 changes: 3 additions & 3 deletions internal/git/exec_mock.go → internal/exec/exec_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package git_test
package exec_test

import (
"log/slog"
Expand All @@ -28,7 +28,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/retr0h/go-gilt/internal/git"
"github.com/retr0h/go-gilt/internal"
"github.com/retr0h/go-gilt/internal/exec"
)

type ExecManagerPublicTestSuite struct {
Expand All @@ -37,8 +38,8 @@ type ExecManagerPublicTestSuite struct {

func (suite *ExecManagerPublicTestSuite) NewTestExecManager(
debug bool,
) git.ExecManager {
return git.NewExecManagerCmd(
) internal.ExecManager {
return exec.New(
debug,
slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
Expand Down
31 changes: 31 additions & 0 deletions internal/exec/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2023 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package exec

import (
"log/slog"
)

// Exec disk implementation.
type Exec struct {
debug bool
logger *slog.Logger
}
4 changes: 3 additions & 1 deletion internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import (
"path/filepath"

"github.com/spf13/afero"

"github.com/retr0h/go-gilt/internal"
)

// New factory to create a new Git instance.
func New(
appFs afero.Fs,
debug bool,
execManager ExecManager,
execManager internal.ExecManager,
logger *slog.Logger,
) *Git {
return &Git{
Expand Down
5 changes: 3 additions & 2 deletions internal/git/git_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import (
"github.com/stretchr/testify/suite"

"github.com/retr0h/go-gilt/internal"
"github.com/retr0h/go-gilt/internal/exec"
"github.com/retr0h/go-gilt/internal/git"
)

type GitManagerPublicTestSuite struct {
suite.Suite

ctrl *gomock.Controller
mockExec *git.MockExecManager
mockExec *exec.MockExecManager

gitURL string
gitVersion string
Expand All @@ -60,7 +61,7 @@ func (suite *GitManagerPublicTestSuite) NewTestGitManager() internal.GitManager

func (suite *GitManagerPublicTestSuite) SetupTest() {
suite.ctrl = gomock.NewController(suite.T())
suite.mockExec = git.NewMockExecManager(suite.ctrl)
suite.mockExec = exec.NewMockExecManager(suite.ctrl)
defer suite.ctrl.Finish()

suite.gitURL = "https://example.com/user/repo.git"
Expand Down
15 changes: 3 additions & 12 deletions internal/git/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ import (
"log/slog"

"github.com/spf13/afero"

"github.com/retr0h/go-gilt/internal"
)

// Git implementation responsible for Git operations.
type Git struct {
appFs afero.Fs
debug bool
execManager ExecManager
execManager internal.ExecManager
logger *slog.Logger
}

// ExecManager interface to managing exec calls.
type ExecManager interface {
RunCmd(name string, args ...string) error
}

// ExecManagerCmd disk implementation.
type ExecManagerCmd struct {
debug bool
logger *slog.Logger
}
12 changes: 12 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,15 @@ func (r *Repository) CopySources(

return nil
}

// RunCmd run the provided command.
func (r *Repository) RunCmd(
cmd string,
) error {
fmt.Println(cmd)
fmt.Println(cmd)
fmt.Println(cmd)
fmt.Println(cmd)
// return r.gitManager.CheckoutIndex(c.DstDir, cloneDir)
return nil
}

0 comments on commit 92d9709

Please sign in to comment.