Skip to content

Commit

Permalink
fix(test): use go-git to create default config repo
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Apr 7, 2022
1 parent 67b11ee commit 0a26b94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ require (
github.com/charmbracelet/wish v0.2.1-0.20220208182816-534842b53d2a
github.com/dustin/go-humanize v1.0.0
github.com/gliderlabs/ssh v0.3.3
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.3-0.20210630082519-b4368b2a2ca4 // indirect
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-git/v5 v5.4.3-0.20210630082519-b4368b2a2ca4
github.com/matryer/is v1.2.0
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.11.0
Expand Down
50 changes: 33 additions & 17 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (

"github.com/charmbracelet/soft-serve/config"
"github.com/charmbracelet/soft-serve/internal/git"
gg "github.com/gogs/git-module"
"github.com/go-git/go-billy/v5/memfs"
ggit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage/memory"
)

// Config is the Soft Serve configuration.
Expand Down Expand Up @@ -107,10 +111,6 @@ func NewConfig(cfg *config.Config) (*Config, error) {
if err != nil {
return nil, err
}
err = c.Reload()
if err != nil {
return nil, err
}
return c, nil
}

Expand Down Expand Up @@ -181,50 +181,66 @@ func createFile(path string, content string) error {

func (cfg *Config) createDefaultConfigRepo(yaml string) error {
cn := "config"
rp := filepath.Join(cfg.Cfg.RepoPath, cn)
rs := cfg.Source
err := rs.LoadRepo(cn)
if os.IsNotExist(err) {
repo, err := rs.InitRepo(cn, true)
repo, err := ggit.PlainInit(rp, true)
if err != nil {
return err
}
repo, err = ggit.Clone(memory.NewStorage(), memfs.New(), &ggit.CloneOptions{
URL: rp,
})
if err != nil && err != transport.ErrEmptyRemoteRepository {
return err
}
wt, err := repo.Worktree()
if err != nil {
return err
}
wt := repo.Path()
defer os.RemoveAll(wt)
rm, err := os.Create(filepath.Join(wt, "README.md"))
rm, err := wt.Filesystem.Create("README.md")
if err != nil {
return err
}
_, err = rm.Write([]byte(defaultReadme))
if err != nil {
return err
}
cf, err := os.Create(filepath.Join(wt, "config.yaml"))
_, err = wt.Add("README.md")
if err != nil {
return err
}
cf, err := wt.Filesystem.Create("config.yaml")
if err != nil {
return err
}
_, err = cf.Write([]byte(yaml))
if err != nil {
return err
}
err = gg.Add(wt, gg.AddOptions{All: true})
_, err = wt.Add("config.yaml")
if err != nil {
return err
}
err = gg.CreateCommit(wt, &gg.Signature{
Name: "Soft Serve Server",
Email: "[email protected]",
}, "Default init")
_, err = wt.Commit("Default init", &ggit.CommitOptions{
All: true,
Author: &object.Signature{
Name: "Soft Serve Server",
Email: "[email protected]",
},
})
if err != nil {
return err
}
err = repo.Push("origin", "master")
err = repo.Push(&ggit.PushOptions{})
if err != nil {
return err
}
} else if err != nil {
return err
}
return nil
return cfg.Reload()
}

func (cfg *Config) isPrivate(repo string) bool {
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestMultipleInitialKeys(t *testing.T) {
})
is := is.New(t)
is.NoErr(err)
err = cfg.Reload()
is.NoErr(err)
is.Equal(cfg.Users[0].PublicKeys, []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINMwLvyV3ouVrTysUYGoJdl5Vgn5BACKov+n9PlzfPwH a@b",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxIobhwtfdwN7m1TFt9wx3PsfvcAkISGPxmbmbauST8 a@b",
Expand Down

0 comments on commit 0a26b94

Please sign in to comment.