mirror of
https://github.com/portainer/portainer.git
synced 2025-07-22 23:09:41 +02:00
refactor(libstack): move library to portainer [EE-5474] (#9120)
This commit is contained in:
parent
11571fd6ea
commit
8c16fbb8aa
15 changed files with 691 additions and 0 deletions
60
pkg/libstack/compose/internal/utils/utils.go
Normal file
60
pkg/libstack/compose/internal/utils/utils.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"runtime"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func osProgram(program string) string {
|
||||
if runtime.GOOS == "windows" {
|
||||
program += ".exe"
|
||||
}
|
||||
return program
|
||||
}
|
||||
|
||||
func ProgramPath(rootPath, program string) string {
|
||||
return path.Join(rootPath, osProgram(program))
|
||||
}
|
||||
|
||||
// IsBinaryPresent check if docker compose binary is present
|
||||
func IsBinaryPresent(program string) bool {
|
||||
_, err := exec.LookPath(program)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// Copy copies sourcePath to destinationPath
|
||||
func Copy(sourcePath, destinationPath string) error {
|
||||
si, err := os.Stat(sourcePath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "file check failed")
|
||||
}
|
||||
|
||||
input, err := os.ReadFile(sourcePath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed reading file")
|
||||
}
|
||||
|
||||
err = os.WriteFile(destinationPath, input, si.Mode())
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed writing file")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Move sourcePath to destinationPath
|
||||
func Move(sourcePath, destinationPath string) error {
|
||||
if err := Copy(sourcePath, destinationPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Remove(sourcePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
33
pkg/libstack/compose/internal/utils/utils_test.go
Normal file
33
pkg/libstack/compose/internal/utils/utils_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsBinaryPresent(t *testing.T) {
|
||||
type testCase struct {
|
||||
Name string
|
||||
Binary string
|
||||
Expected bool
|
||||
}
|
||||
testCases := []testCase{
|
||||
{
|
||||
Name: "not existing",
|
||||
Binary: "qwgq-er-gerw",
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
Name: "docker-compose exists",
|
||||
Binary: "docker-compose",
|
||||
Expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
got := IsBinaryPresent(tc.Binary)
|
||||
if got != tc.Expected {
|
||||
t.Errorf("Error in test %s got = %v, and Expected = %v.", tc.Name, got, tc.Expected)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue