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

Created package to use as go package #40

Merged
merged 1 commit into from
Nov 25, 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
4 changes: 2 additions & 2 deletions Giltfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ repositories:
dstDir: /tmp/tests
commands:
- cmd: touch
args: post-command-1
args: /tmp/post-command-1
- cmd: touch
args: post-command-2
args: /tmp/post-command-2
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,48 @@ The config file and/or env vars can be overriden/defined through cli flags.

## Usage

### Overlay Repository
### CLI

#### Overlay Repository

Overlay a remote repository into the destination provided.

$ gilt overlay

### Debug
#### Debug

Display the git commands being executed.

$ gilt --debug overlay

### Package

#### Overlay Repository

See example client in `test/integration/client/`.

```golang
func main() {
debug := true
logger := getLogger(debug)

c := config.Repositories{
Debug: debug,
GiltDir: "~/.gilt",
Repositories: []config.Repository{
{
Git: "https://github.com/retr0h/ansible-etcd.git",
Version: "77a95b7",
DstDir: "../tmp/retr0h.ansible-etcd",
},
},
}

var r repositoriesManager = repositories.New(c, logger)
r.Overlay()
}
```

## Building

$ task build
Expand Down
101 changes: 1 addition & 100 deletions cmd/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,90 +21,9 @@
package cmd

import (
"errors"
"fmt"
"log/slog"
"path/filepath"
"strconv"

"github.com/danjacques/gofslock/fslock"
"github.com/spf13/cobra"

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

// getGiltDir create the GiltDir if it doesn't exist.
func getGiltDir() (string, error) {
dir, err := giltpath.ExpandUser(appConfig.GiltDir)
if err != nil {
return "", err
}

if err := appFs.MkdirAll(dir, 0o755); err != nil {
return "", err
}

return dir, nil
}

// withLock is a convenience function to create a lock, execute a function while
// holding that lock, and then release the lock on completion.
func withLock(fn func() error) error {
lockDir, err := getGiltDir()
if err != nil {
return err
}

lockFile := filepath.Join(lockDir, "gilt.lock")
logger.Info(
"acquiring lock",
slog.String("lockfile", lockFile),
)

err = fslock.With(lockFile, fn)
if err != nil {
if errors.Is(err, fslock.ErrLockHeld) {
return fmt.Errorf("could not acquire lock on %s: %s", lockFile, err)
}
}

return err
}

func logRepositoriesGroup() []any {
logGroups := make([]any, 0, len(appConfig.Repositories))

for i, repo := range appConfig.Repositories {
var sourceGroups []any
for i, s := range repo.Sources {
group := slog.Group(strconv.Itoa(i),
slog.String("Src", s.Src),
slog.String("DstFile", s.DstFile),
slog.String("DstDir", s.DstDir),
)
sourceGroups = append(sourceGroups, group)
}
var cmdGroups []any
for i, s := range repo.Commands {
group := slog.Group(strconv.Itoa(i),
slog.String("Cmd", s.Cmd),
)
cmdGroups = append(cmdGroups, group)
}

group := slog.Group(strconv.Itoa(i),
slog.String("Git", repo.Git),
slog.String("Version", repo.Version),
slog.String("DstDir", repo.DstDir),
slog.Group("Sources", sourceGroups...),
slog.Group("Commands", cmdGroups...),
)
logGroups = append(logGroups, group)
}

return logGroups
}

// overlayCmd represents the overlay command
var overlayCmd = &cobra.Command{
Use: "overlay",
Expand All @@ -117,25 +36,7 @@ var overlayCmd = &cobra.Command{
// We are logging errors, no need for cobra to re-log the error
cmd.SilenceErrors = true

logger.Debug(
"current configuration",
slog.String("GiltDir", appConfig.GiltDir),
slog.String("GiltFile", appConfig.GiltFile),
slog.Bool("Debug", appConfig.Debug),
slog.Group("Repository", logRepositoriesGroup()...),
)

if err := withLock(func() error {
return repos.Overlay()
}); err != nil {
logger.Error(
"error overlaying repositories",
slog.String("err", err.Error()),
)
return err
}

return nil
return repos.Overlay()
},
}

Expand Down
40 changes: 4 additions & 36 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ import (
"time"

"github.com/lmittmann/tint"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"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"
"github.com/retr0h/go-gilt/pkg"
"github.com/retr0h/go-gilt/pkg/config"
"github.com/retr0h/go-gilt/pkg/repositories"
)

var (
repos internal.RepositoriesManager
repos pkg.RepositoriesManager
logger *slog.Logger
appConfig config.Repositories
appFs afero.Fs
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -130,35 +125,8 @@ func initConfig() {
os.Exit(1)
}

appFs = afero.NewOsFs()

repoManager := repository.NewCopy(
appFs,
logger,
)

execManager := exec.New(
appConfig.Debug,
logger,
)

g := git.New(
appFs,
appConfig.Debug,
execManager,
logger,
)

repos = repositories.New(
appFs,
appConfig,
repository.New(
appFs,
repoManager,
g,
logger,
),
execManager,
logger,
)
}
6 changes: 3 additions & 3 deletions internal/repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/spf13/afero"

"github.com/retr0h/go-gilt/internal"
"github.com/retr0h/go-gilt/internal/config"
giltpath "github.com/retr0h/go-gilt/internal/path"
intPath "github.com/retr0h/go-gilt/internal/path"
"github.com/retr0h/go-gilt/pkg/config"
)

// New factory to create a new Repository instance.
Expand Down Expand Up @@ -73,7 +73,7 @@ func (r *Repositories) getCloneHash(

// getCacheDir create the cacheDir if it doesn't exist.
func (r *Repositories) getCacheDir() (string, error) {
giltDir, err := giltpath.ExpandUser(r.config.GiltDir)
giltDir, err := intPath.ExpandUser(r.config.GiltDir)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/repositories/repositories_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
"github.com/stretchr/testify/suite"

"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/repositories"
"github.com/retr0h/go-gilt/internal/repository"
"github.com/retr0h/go-gilt/pkg/config"
)

type RepositoriesPublicTestSuite struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/repositories/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/retr0h/go-gilt/internal/config"
"github.com/retr0h/go-gilt/internal/exec"
"github.com/retr0h/go-gilt/internal/repository"
"github.com/retr0h/go-gilt/pkg/config"
)

type RepositoriesTestSuite struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/repositories/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/spf13/afero"

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

// Repositories perform repository operations.
Expand Down
2 changes: 1 addition & 1 deletion internal/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package internal

import (
"github.com/retr0h/go-gilt/internal/config"
"github.com/retr0h/go-gilt/pkg/config"
)

// RepositoryManager manager responsible for Repository operations.
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/spf13/afero"

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

// New factory to create a new Repository instance.
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/repository_mock.go

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

2 changes: 1 addition & 1 deletion internal/repository/repository_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"github.com/stretchr/testify/suite"

"github.com/retr0h/go-gilt/internal"
"github.com/retr0h/go-gilt/internal/config"
"github.com/retr0h/go-gilt/internal/git"
"github.com/retr0h/go-gilt/internal/repository"
"github.com/retr0h/go-gilt/pkg/config"
)

type RepositoryPublicTestSuite struct {
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions pkg/repositories.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 pkg

// RepositoriesManager manager responsible for public Repositories operations.
type RepositoriesManager interface {
Overlay() error
}
Loading
Loading