mirror of
https://github.com/thegeeklab/wp-git-action.git
synced 2024-11-24 22:50:39 +00:00
refactor: use struct receiver (#112)
This commit is contained in:
parent
c2608f913f
commit
198aab7784
12
git/clone.go
12
git/clone.go
@ -8,15 +8,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// FetchSource fetches the source from remote.
|
// FetchSource fetches the source from remote.
|
||||||
func FetchSource(repo Repository) *types.Cmd {
|
func (r *Repository) FetchSource() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"fetch",
|
"fetch",
|
||||||
"origin",
|
"origin",
|
||||||
fmt.Sprintf("+%s:", repo.Branch),
|
fmt.Sprintf("+%s:", r.Branch),
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -24,15 +24,15 @@ func FetchSource(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckoutHead handles branch checkout.
|
// CheckoutHead handles branch checkout.
|
||||||
func CheckoutHead(repo Repository) *types.Cmd {
|
func (r *Repository) CheckoutHead() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"checkout",
|
"checkout",
|
||||||
"-qf",
|
"-qf",
|
||||||
repo.Branch,
|
r.Branch,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
|
@ -32,7 +32,7 @@ func TestFetchSource(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := FetchSource(tt.repo)
|
cmd := tt.repo.FetchSource()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -65,7 +65,7 @@ func TestCheckoutHead(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := CheckoutHead(tt.repo)
|
cmd := tt.repo.CheckoutHead()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
|
@ -5,34 +5,14 @@ import (
|
|||||||
"golang.org/x/sys/execabs"
|
"golang.org/x/sys/execabs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ForceAdd forces the addition of all dirty files.
|
// Add updates the index to match the working tree.
|
||||||
func ForceAdd(repo Repository) *types.Cmd {
|
func (r *Repository) Add() *types.Cmd {
|
||||||
cmd := execabs.Command(
|
cmd := execabs.Command(
|
||||||
gitBin,
|
gitBin,
|
||||||
"add",
|
"add",
|
||||||
"--all",
|
"--all",
|
||||||
"--force",
|
|
||||||
)
|
)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
|
||||||
Cmd: cmd,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add updates the index to match the working tree.
|
|
||||||
func Add(repo Repository) *types.Cmd {
|
|
||||||
cmd := execabs.Command(
|
|
||||||
gitBin,
|
|
||||||
"add",
|
|
||||||
)
|
|
||||||
cmd.Dir = repo.WorkDir
|
|
||||||
|
|
||||||
if repo.Add != "" {
|
|
||||||
cmd.Args = append(cmd.Args, repo.Add)
|
|
||||||
} else {
|
|
||||||
cmd.Args = append(cmd.Args, "--all")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -40,7 +20,7 @@ func Add(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestCleanTree returns non-zero if diff between index and local repository.
|
// TestCleanTree returns non-zero if diff between index and local repository.
|
||||||
func IsCleanTree(repo Repository) *types.Cmd {
|
func (r *Repository) IsCleanTree() *types.Cmd {
|
||||||
cmd := execabs.Command(
|
cmd := execabs.Command(
|
||||||
gitBin,
|
gitBin,
|
||||||
"diff-index",
|
"diff-index",
|
||||||
@ -48,47 +28,31 @@ func IsCleanTree(repo Repository) *types.Cmd {
|
|||||||
"HEAD",
|
"HEAD",
|
||||||
"--ignore-submodules",
|
"--ignore-submodules",
|
||||||
)
|
)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EmptyCommit simply create an empty commit.
|
// Commit creates a new commit with the specified commit message.
|
||||||
func EmptyCommit(repo Repository) *types.Cmd {
|
func (r *Repository) Commit() *types.Cmd {
|
||||||
args := []string{
|
|
||||||
"commit",
|
|
||||||
"--allow-empty",
|
|
||||||
"-m",
|
|
||||||
repo.CommitMsg,
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
|
||||||
cmd.Dir = repo.WorkDir
|
|
||||||
|
|
||||||
if repo.NoVerify {
|
|
||||||
cmd.Args = append(cmd.Args, "--no-verify")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &types.Cmd{
|
|
||||||
Cmd: cmd,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Commit(repo Repository) *types.Cmd {
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"commit",
|
"commit",
|
||||||
"-m",
|
"-m",
|
||||||
repo.CommitMsg,
|
r.CommitMsg,
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.EmptyCommit {
|
||||||
|
args = append(args, "--allow-empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.NoVerify {
|
||||||
|
args = append(args, "--no-verify")
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
if repo.NoVerify {
|
|
||||||
cmd.Args = append(cmd.Args, "--no-verify")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
|
@ -16,23 +16,14 @@ func TestAdd(t *testing.T) {
|
|||||||
name: "add all files",
|
name: "add all files",
|
||||||
repo: Repository{
|
repo: Repository{
|
||||||
WorkDir: "/path/to/repo",
|
WorkDir: "/path/to/repo",
|
||||||
Add: "",
|
|
||||||
},
|
},
|
||||||
want: []string{gitBin, "add", "--all"},
|
want: []string{gitBin, "add", "--all"},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "add specific file",
|
|
||||||
repo: Repository{
|
|
||||||
WorkDir: "/path/to/repo",
|
|
||||||
Add: "file.go",
|
|
||||||
},
|
|
||||||
want: []string{gitBin, "add", "file.go"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := Add(tt.repo)
|
cmd := tt.repo.Add()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -63,41 +54,50 @@ func TestIsCleanTree(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := IsCleanTree(tt.repo)
|
cmd := tt.repo.IsCleanTree()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyCommit(t *testing.T) {
|
func TestCommit(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
repo Repository
|
repo Repository
|
||||||
want []string
|
want []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty commit with default options",
|
name: "commit with message",
|
||||||
repo: Repository{
|
repo: Repository{
|
||||||
WorkDir: "/path/to/repo",
|
WorkDir: "/path/to/repo",
|
||||||
CommitMsg: "Empty commit",
|
CommitMsg: "Initial commit",
|
||||||
},
|
},
|
||||||
want: []string{gitBin, "commit", "--allow-empty", "-m", "Empty commit"},
|
want: []string{gitBin, "commit", "-m", "Initial commit"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty commit with no-verify option",
|
name: "commit with empty commit",
|
||||||
|
repo: Repository{
|
||||||
|
WorkDir: "/path/to/repo",
|
||||||
|
CommitMsg: "Empty commit",
|
||||||
|
EmptyCommit: true,
|
||||||
|
},
|
||||||
|
want: []string{gitBin, "commit", "-m", "Empty commit", "--allow-empty"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "commit with no verify",
|
||||||
repo: Repository{
|
repo: Repository{
|
||||||
WorkDir: "/path/to/repo",
|
WorkDir: "/path/to/repo",
|
||||||
CommitMsg: "Empty commit",
|
CommitMsg: "No verify commit",
|
||||||
NoVerify: true,
|
NoVerify: true,
|
||||||
},
|
},
|
||||||
want: []string{gitBin, "commit", "--allow-empty", "-m", "Empty commit", "--no-verify"},
|
want: []string{gitBin, "commit", "-m", "No verify commit", "--no-verify"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := EmptyCommit(tt.repo)
|
cmd := tt.repo.Commit()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
|
@ -9,16 +9,16 @@ import (
|
|||||||
|
|
||||||
// ConfigAutocorrect sets the local git autocorrect configuration for the given repository.
|
// ConfigAutocorrect sets the local git autocorrect configuration for the given repository.
|
||||||
// The autocorrect setting determines how git handles minor typos in commands.
|
// The autocorrect setting determines how git handles minor typos in commands.
|
||||||
func ConfigAutocorrect(repo Repository) *types.Cmd {
|
func (r *Repository) ConfigAutocorrect() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"config",
|
"config",
|
||||||
"--local",
|
"--local",
|
||||||
"help.autocorrect",
|
"help.autocorrect",
|
||||||
repo.Autocorrect,
|
r.Autocorrect,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -26,16 +26,16 @@ func ConfigAutocorrect(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConfigUserEmail sets the global git author email.
|
// ConfigUserEmail sets the global git author email.
|
||||||
func ConfigUserEmail(repo Repository) *types.Cmd {
|
func (r *Repository) ConfigUserEmail() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"config",
|
"config",
|
||||||
"--local",
|
"--local",
|
||||||
"user.email",
|
"user.email",
|
||||||
repo.Author.Email,
|
r.Author.Email,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -43,16 +43,16 @@ func ConfigUserEmail(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConfigUserName configures the user.name git config setting for the given repository.
|
// ConfigUserName configures the user.name git config setting for the given repository.
|
||||||
func ConfigUserName(repo Repository) *types.Cmd {
|
func (r *Repository) ConfigUserName() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"config",
|
"config",
|
||||||
"--local",
|
"--local",
|
||||||
"user.name",
|
"user.name",
|
||||||
repo.Author.Name,
|
r.Author.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -60,7 +60,7 @@ func ConfigUserName(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ConfigSSLVerify configures the http.sslVerify git config setting for the given repository.
|
// ConfigSSLVerify configures the http.sslVerify git config setting for the given repository.
|
||||||
func ConfigSSLVerify(repo Repository, skipVerify bool) *types.Cmd {
|
func (r *Repository) ConfigSSLVerify(skipVerify bool) *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"config",
|
"config",
|
||||||
"--local",
|
"--local",
|
||||||
@ -69,7 +69,7 @@ func ConfigSSLVerify(repo Repository, skipVerify bool) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
|
@ -32,7 +32,7 @@ func TestConfigAutocorrect(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := ConfigAutocorrect(tt.repo)
|
cmd := tt.repo.ConfigAutocorrect()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -59,7 +59,7 @@ func TestConfigUserEmail(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := ConfigUserEmail(tt.repo)
|
cmd := tt.repo.ConfigUserEmail()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -86,7 +86,7 @@ func TestConfigUserName(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := ConfigUserName(tt.repo)
|
cmd := tt.repo.ConfigUserName()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -116,7 +116,7 @@ func TestConfigSSLVerify(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := ConfigSSLVerify(tt.repo, tt.skipVerify)
|
cmd := tt.repo.ConfigSSLVerify(tt.skipVerify)
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package git
|
package git
|
||||||
|
|
||||||
const gitBin = "/usr/bin/git"
|
|
||||||
|
|
||||||
type Author struct {
|
type Author struct {
|
||||||
Name string
|
Name string
|
||||||
Email string
|
Email string
|
||||||
@ -12,7 +10,6 @@ type Repository struct {
|
|||||||
RemoteURL string
|
RemoteURL string
|
||||||
Branch string
|
Branch string
|
||||||
|
|
||||||
Add string
|
|
||||||
CommitMsg string
|
CommitMsg string
|
||||||
|
|
||||||
Autocorrect string
|
Autocorrect string
|
||||||
@ -25,3 +22,5 @@ type Repository struct {
|
|||||||
|
|
||||||
Author Author
|
Author Author
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const gitBin = "/usr/bin/git"
|
@ -6,15 +6,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Init creates a new Git repository in the specified directory.
|
// Init creates a new Git repository in the specified directory.
|
||||||
func Init(repo Repository) *types.Cmd {
|
func (r *Repository) Init() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"init",
|
"init",
|
||||||
"-b",
|
"-b",
|
||||||
repo.Branch,
|
r.Branch,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
|
@ -24,7 +24,7 @@ func TestInit(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := Init(tt.repo)
|
cmd := tt.repo.Init()
|
||||||
require.Equal(t, tt.expected, cmd.Cmd.Args)
|
require.Equal(t, tt.expected, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
|
@ -8,15 +8,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// RemoteRemove drops the defined remote from a git repo.
|
// RemoteRemove drops the defined remote from a git repo.
|
||||||
func RemoteRemove(repo Repository) *types.Cmd {
|
func (r *Repository) RemoteRemove() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"remote",
|
"remote",
|
||||||
"rm",
|
"rm",
|
||||||
repo.RemoteName,
|
r.RemoteName,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -24,16 +24,16 @@ func RemoteRemove(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemoteAdd adds an additional remote to a git repo.
|
// RemoteAdd adds an additional remote to a git repo.
|
||||||
func RemoteAdd(repo Repository) *types.Cmd {
|
func (r *Repository) RemoteAdd() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"remote",
|
"remote",
|
||||||
"add",
|
"add",
|
||||||
repo.RemoteName,
|
r.RemoteName,
|
||||||
repo.RemoteURL,
|
r.RemoteURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -41,21 +41,21 @@ func RemoteAdd(repo Repository) *types.Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RemotePush pushs the changes from the local head to a remote branch.
|
// RemotePush pushs the changes from the local head to a remote branch.
|
||||||
func RemotePush(repo Repository) *types.Cmd {
|
func (r *Repository) RemotePush() *types.Cmd {
|
||||||
args := []string{
|
args := []string{
|
||||||
"push",
|
"push",
|
||||||
repo.RemoteName,
|
r.RemoteName,
|
||||||
fmt.Sprintf("HEAD:%s", repo.Branch),
|
fmt.Sprintf("HEAD:%s", r.Branch),
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := execabs.Command(gitBin, args...)
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
if repo.ForcePush {
|
if r.ForcePush {
|
||||||
cmd.Args = append(cmd.Args, "--force")
|
cmd.Args = append(cmd.Args, "--force")
|
||||||
}
|
}
|
||||||
|
|
||||||
if repo.PushFollowTags {
|
if r.PushFollowTags {
|
||||||
cmd.Args = append(cmd.Args, "--follow-tags")
|
cmd.Args = append(cmd.Args, "--follow-tags")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ func TestRemoteRemove(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := RemoteRemove(tt.repo)
|
cmd := tt.repo.RemoteRemove()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -73,7 +73,7 @@ func TestRemoteAdd(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := RemoteAdd(tt.repo)
|
cmd := tt.repo.RemoteAdd()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -131,7 +131,7 @@ func TestRemotePush(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := RemotePush(tt.repo)
|
cmd := tt.repo.RemotePush()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
|
@ -7,13 +7,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Status returns a command that runs `git status --porcelain` for the given repository.
|
// Status returns a command that runs `git status --porcelain` for the given repository.
|
||||||
func Status(repo Repository) *types.Cmd {
|
func (r *Repository) Status() *types.Cmd {
|
||||||
cmd := execabs.Command(
|
cmd := execabs.Command(
|
||||||
gitBin,
|
gitBin,
|
||||||
"status",
|
"status",
|
||||||
"--porcelain",
|
"--porcelain",
|
||||||
)
|
)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
@ -24,9 +24,9 @@ func Status(repo Repository) *types.Cmd {
|
|||||||
// It runs `git status --porcelain` and returns true if the output is non-empty,
|
// It runs `git status --porcelain` and returns true if the output is non-empty,
|
||||||
// indicating that there are uncommitted changes in the repository.
|
// indicating that there are uncommitted changes in the repository.
|
||||||
// If there is an error running the git command, it returns false.
|
// If there is an error running the git command, it returns false.
|
||||||
func IsDirty(repo Repository) bool {
|
func (r *Repository) IsDirty() bool {
|
||||||
cmd := Status(repo)
|
cmd := r.Status()
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = r.WorkDir
|
||||||
|
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -37,7 +37,7 @@ func TestStatus(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cmd := Status(tt.repo)
|
cmd := tt.repo.Status()
|
||||||
require.Equal(t, tt.want, cmd.Cmd.Args)
|
require.Equal(t, tt.want, cmd.Cmd.Args)
|
||||||
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
})
|
})
|
||||||
@ -70,7 +70,7 @@ func TestIsDirty(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if err := Init(tt.repo).Run(); err != nil {
|
if err := tt.repo.Init().Run(); err != nil {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ func TestIsDirty(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
isDirty := IsDirty(tt.repo)
|
isDirty := tt.repo.IsDirty()
|
||||||
require.Equal(t, tt.want, isDirty)
|
require.Equal(t, tt.want, isDirty)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
56
git/util.go
56
git/util.go
@ -1,56 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
netrcFile = `machine %s
|
|
||||||
login %s
|
|
||||||
password %s
|
|
||||||
`
|
|
||||||
configFile = `Host *
|
|
||||||
StrictHostKeyChecking no
|
|
||||||
UserKnownHostsFile=/dev/null
|
|
||||||
`
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
strictFilePerm = 0o600
|
|
||||||
strictDirPerm = 0o700
|
|
||||||
)
|
|
||||||
|
|
||||||
// WriteKey writes the SSH private key.
|
|
||||||
func WriteSSHKey(path, key string) error {
|
|
||||||
sshPath := filepath.Join(path, ".ssh")
|
|
||||||
confPath := filepath.Join(sshPath, "config")
|
|
||||||
keyPath := filepath.Join(sshPath, "id_rsa")
|
|
||||||
|
|
||||||
if err := os.MkdirAll(sshPath, strictDirPerm); err != nil {
|
|
||||||
return fmt.Errorf("failed to create .ssh directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.WriteFile(confPath, []byte(configFile), strictFilePerm); err != nil {
|
|
||||||
return fmt.Errorf("failed to create .ssh/config file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.WriteFile(keyPath, []byte(key), strictFilePerm); err != nil {
|
|
||||||
return fmt.Errorf("failed to create .ssh/id_rsa file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteNetrc writes the netrc file.
|
|
||||||
func WriteNetrc(path, machine, login, password string) error {
|
|
||||||
netrcPath := filepath.Join(path, ".netrc")
|
|
||||||
netrcContent := fmt.Sprintf(netrcFile, machine, login, password)
|
|
||||||
|
|
||||||
if err := os.WriteFile(netrcPath, []byte(netrcContent), strictFilePerm); err != nil {
|
|
||||||
return fmt.Errorf("failed to create .netrc file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.22
|
|||||||
require (
|
require (
|
||||||
github.com/rs/zerolog v1.32.0
|
github.com/rs/zerolog v1.32.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
github.com/thegeeklab/wp-plugin-go/v2 v2.2.0
|
github.com/thegeeklab/wp-plugin-go/v2 v2.3.0
|
||||||
github.com/urfave/cli/v2 v2.27.2
|
github.com/urfave/cli/v2 v2.27.2
|
||||||
golang.org/x/sys v0.20.0
|
golang.org/x/sys v0.20.0
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -46,8 +46,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
|||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/thegeeklab/wp-plugin-go/v2 v2.2.0 h1:Z6UzL8N0v3J2uuk67DBnH19QNV1vXihaKC2OoH2TMAY=
|
github.com/thegeeklab/wp-plugin-go/v2 v2.3.0 h1:9LOdITzjxEEbgcH9yU0tDZL0dcDZzNYzbk2+hZ5QsBA=
|
||||||
github.com/thegeeklab/wp-plugin-go/v2 v2.2.0/go.mod h1:I/3M/4OPvr4FFS+s0aaImpX1llA/lS2KC6Bnp+qzsCs=
|
github.com/thegeeklab/wp-plugin-go/v2 v2.3.0/go.mod h1:I/3M/4OPvr4FFS+s0aaImpX1llA/lS2KC6Bnp+qzsCs=
|
||||||
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
||||||
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
||||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
||||||
|
@ -7,9 +7,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/thegeeklab/wp-git-action/git"
|
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/file"
|
"github.com/thegeeklab/wp-plugin-go/v2/file"
|
||||||
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
"github.com/thegeeklab/wp-plugin-go/v2/types"
|
||||||
|
"github.com/thegeeklab/wp-plugin-go/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -46,16 +46,14 @@ func (p *Plugin) run(ctx context.Context) error {
|
|||||||
func (p *Plugin) Validate() error {
|
func (p *Plugin) Validate() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if p.Settings.Repo.WorkDir == "" {
|
|
||||||
p.Settings.Repo.WorkDir, err = os.Getwd()
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Settings.Repo.Autocorrect = "never"
|
p.Settings.Repo.Autocorrect = "never"
|
||||||
p.Settings.Repo.RemoteName = "origin"
|
p.Settings.Repo.RemoteName = "origin"
|
||||||
p.Settings.Repo.Add = ""
|
|
||||||
|
|
||||||
if err != nil {
|
if p.Settings.Repo.WorkDir == "" {
|
||||||
return fmt.Errorf("failed to get working directory: %w", err)
|
p.Settings.Repo.WorkDir, err = os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get working directory: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, actionStr := range p.Settings.Action.Value() {
|
for _, actionStr := range p.Settings.Action.Value() {
|
||||||
@ -104,7 +102,7 @@ func (p *Plugin) Validate() error {
|
|||||||
func (p *Plugin) Execute() error {
|
func (p *Plugin) Execute() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
homeDir := getUserHomeDir()
|
homeDir := util.GetUserHomeDir()
|
||||||
batchCmd := make([]*types.Cmd, 0)
|
batchCmd := make([]*types.Cmd, 0)
|
||||||
gitEnv := []string{
|
gitEnv := []string{
|
||||||
"GIT_AUTHOR_NAME",
|
"GIT_AUTHOR_NAME",
|
||||||
@ -127,13 +125,13 @@ func (p *Plugin) Execute() error {
|
|||||||
|
|
||||||
// Write SSH key and netrc file.
|
// Write SSH key and netrc file.
|
||||||
if p.Settings.SSHKey != "" {
|
if p.Settings.SSHKey != "" {
|
||||||
if err := git.WriteSSHKey(homeDir, p.Settings.SSHKey); err != nil {
|
if err := WriteSSHKey(homeDir, p.Settings.SSHKey); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
netrc := p.Settings.Netrc
|
netrc := p.Settings.Netrc
|
||||||
if err := git.WriteNetrc(homeDir, netrc.Machine, netrc.Login, netrc.Password); err != nil {
|
if err := WriteNetrc(homeDir, netrc.Machine, netrc.Login, netrc.Password); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,14 +151,14 @@ func (p *Plugin) Execute() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !isDir {
|
if !isDir {
|
||||||
batchCmd = append(batchCmd, git.Init(p.Settings.Repo))
|
batchCmd = append(batchCmd, p.Settings.Repo.Init())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle repo configuration.
|
// Handle repo configuration.
|
||||||
batchCmd = append(batchCmd, git.ConfigAutocorrect(p.Settings.Repo))
|
batchCmd = append(batchCmd, p.Settings.Repo.ConfigAutocorrect())
|
||||||
batchCmd = append(batchCmd, git.ConfigUserName(p.Settings.Repo))
|
batchCmd = append(batchCmd, p.Settings.Repo.ConfigUserName())
|
||||||
batchCmd = append(batchCmd, git.ConfigUserEmail(p.Settings.Repo))
|
batchCmd = append(batchCmd, p.Settings.Repo.ConfigUserEmail())
|
||||||
batchCmd = append(batchCmd, git.ConfigSSLVerify(p.Settings.Repo, p.Network.InsecureSkipVerify))
|
batchCmd = append(batchCmd, p.Settings.Repo.ConfigSSLVerify(p.Network.InsecureSkipVerify))
|
||||||
|
|
||||||
for _, actionStr := range p.Settings.Action.Value() {
|
for _, actionStr := range p.Settings.Action.Value() {
|
||||||
action := Action(actionStr)
|
action := Action(actionStr)
|
||||||
@ -205,11 +203,11 @@ func (p *Plugin) handleClone() ([]*types.Cmd, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p.Settings.Repo.RemoteURL != "" {
|
if p.Settings.Repo.RemoteURL != "" {
|
||||||
cmds = append(cmds, git.RemoteAdd(p.Settings.Repo))
|
cmds = append(cmds, p.Settings.Repo.RemoteAdd())
|
||||||
}
|
}
|
||||||
|
|
||||||
cmds = append(cmds, git.FetchSource(p.Settings.Repo))
|
cmds = append(cmds, p.Settings.Repo.FetchSource())
|
||||||
cmds = append(cmds, git.CheckoutHead(p.Settings.Repo))
|
cmds = append(cmds, p.Settings.Repo.CheckoutHead())
|
||||||
|
|
||||||
return cmds, nil
|
return cmds, nil
|
||||||
}
|
}
|
||||||
@ -218,14 +216,10 @@ func (p *Plugin) handleClone() ([]*types.Cmd, error) {
|
|||||||
func (p *Plugin) handleCommit() []*types.Cmd {
|
func (p *Plugin) handleCommit() []*types.Cmd {
|
||||||
var cmds []*types.Cmd
|
var cmds []*types.Cmd
|
||||||
|
|
||||||
cmds = append(cmds, git.Add(p.Settings.Repo))
|
cmds = append(cmds, p.Settings.Repo.Add())
|
||||||
|
|
||||||
if err := git.IsCleanTree(p.Settings.Repo).Run(); err != nil {
|
if err := p.Settings.Repo.IsCleanTree().Run(); err != nil || p.Settings.Repo.EmptyCommit {
|
||||||
cmds = append(cmds, git.Commit(p.Settings.Repo))
|
cmds = append(cmds, p.Settings.Repo.Commit())
|
||||||
}
|
|
||||||
|
|
||||||
if p.Settings.Repo.EmptyCommit {
|
|
||||||
cmds = append(cmds, git.EmptyCommit(p.Settings.Repo))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmds
|
return cmds
|
||||||
@ -233,7 +227,7 @@ func (p *Plugin) handleCommit() []*types.Cmd {
|
|||||||
|
|
||||||
// HandlePush pushs changes to remote.
|
// HandlePush pushs changes to remote.
|
||||||
func (p *Plugin) handlePush() []*types.Cmd {
|
func (p *Plugin) handlePush() []*types.Cmd {
|
||||||
return []*types.Cmd{git.RemotePush(p.Settings.Repo)}
|
return []*types.Cmd{p.Settings.Repo.RemotePush()}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandlePages syncs, commits and pushes the changes from the pages directory to the pages branch.
|
// HandlePages syncs, commits and pushes the changes from the pages directory to the pages branch.
|
||||||
|
@ -1,15 +1,56 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/user"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getUserHomeDir() string {
|
const (
|
||||||
home := "/root"
|
netrcFile = `machine %s
|
||||||
|
login %s
|
||||||
|
password %s
|
||||||
|
`
|
||||||
|
configFile = `Host *
|
||||||
|
StrictHostKeyChecking no
|
||||||
|
UserKnownHostsFile=/dev/null
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
if currentUser, err := user.Current(); err == nil {
|
const (
|
||||||
home = currentUser.HomeDir
|
strictFilePerm = 0o600
|
||||||
|
strictDirPerm = 0o700
|
||||||
|
)
|
||||||
|
|
||||||
|
// WriteKey writes the SSH private key.
|
||||||
|
func WriteSSHKey(path, key string) error {
|
||||||
|
sshPath := filepath.Join(path, ".ssh")
|
||||||
|
confPath := filepath.Join(sshPath, "config")
|
||||||
|
keyPath := filepath.Join(sshPath, "id_rsa")
|
||||||
|
|
||||||
|
if err := os.MkdirAll(sshPath, strictDirPerm); err != nil {
|
||||||
|
return fmt.Errorf("failed to create .ssh directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return home
|
if err := os.WriteFile(confPath, []byte(configFile), strictFilePerm); err != nil {
|
||||||
|
return fmt.Errorf("failed to create .ssh/config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(keyPath, []byte(key), strictFilePerm); err != nil {
|
||||||
|
return fmt.Errorf("failed to create .ssh/id_rsa file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteNetrc writes the netrc file.
|
||||||
|
func WriteNetrc(path, machine, login, password string) error {
|
||||||
|
netrcPath := filepath.Join(path, ".netrc")
|
||||||
|
netrcContent := fmt.Sprintf(netrcFile, machine, login, password)
|
||||||
|
|
||||||
|
if err := os.WriteFile(netrcPath, []byte(netrcContent), strictFilePerm); err != nil {
|
||||||
|
return fmt.Errorf("failed to create .netrc file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package git
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
Loading…
Reference in New Issue
Block a user