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

feat/371 Add YML extension support for stacks and pkgmanagers #373

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
19 changes: 12 additions & 7 deletions core/pkgManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ func NewPkgManager(name string, needSudo bool, autoRemove, clean, install, list,

// LoadPkgManager loads a package manager from the specified path.
func LoadPkgManager(name string) (*PkgManager, error) {
pkgManager, err := loadPkgManagerFromPath(filepath.Join(apx.Cnf.UserPkgManagersPath, name+".yaml"))
userPkgFile := SelectYamlFile(apx.Cnf.UserPkgManagersPath, name)
pkgManager, err := loadPkgManagerFromPath(userPkgFile)
if err != nil {
pkgManager, err = loadPkgManagerFromPath(filepath.Join(apx.Cnf.PkgManagersPath, name+".yaml"))
pkgFile := SelectYamlFile(apx.Cnf.PkgManagersPath, name)
pkgManager, err = loadPkgManagerFromPath(pkgFile)
}
return pkgManager, err
}
Expand Down Expand Up @@ -106,7 +108,7 @@ func (pkgManager *PkgManager) Save() error {
return err
}

filePath := filepath.Join(apx.Cnf.UserPkgManagersPath, pkgManager.Name+".yaml")
filePath := SelectYamlFile(apx.Cnf.UserPkgManagersPath, pkgManager.Name)
err = os.WriteFile(filePath, data, 0644)
return err
}
Expand All @@ -117,7 +119,7 @@ func (pkgManager *PkgManager) Remove() error {
return errors.New("cannot remove built-in package manager")
}

filePath := filepath.Join(apx.Cnf.UserPkgManagersPath, pkgManager.Name+".yaml")
filePath := SelectYamlFile(apx.Cnf.UserPkgManagersPath, pkgManager.Name)
err := os.Remove(filePath)
return err
}
Expand Down Expand Up @@ -173,8 +175,11 @@ func listPkgManagersFromPath(path string) []*PkgManager {
}

for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".yaml" {
pkgManagerName := file.Name()[:len(file.Name())-5] // Remove the ".yaml" extension
extension := filepath.Ext(file.Name())

if !file.IsDir() && (extension == ".yaml" || extension == ".yml") {
// Remove the ".yaml" or ".yml" extension
pkgManagerName := file.Name()[:(len(file.Name()) - len(extension))]
pkgManager, err := LoadPkgManager(pkgManagerName)
if err == nil {
pkgManagers = append(pkgManagers, pkgManager)
Expand Down Expand Up @@ -226,7 +231,7 @@ func (pkgManager *PkgManager) Export(path string) error {
}
}

filePath := filepath.Join(path, pkgManager.Name+".yaml")
filePath := SelectYamlFile(path, pkgManager.Name)
data, err := yaml.Marshal(pkgManager)
if err != nil {
return err
Expand Down
19 changes: 12 additions & 7 deletions core/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ func NewStack(name, base string, packages []string, pkgManager string, builtIn b

// LoadStack loads a stack from the specified path.
func LoadStack(name string) (*Stack, error) {
stack, err := LoadStackFromPath(filepath.Join(apx.Cnf.UserStacksPath, name+".yaml"))
usrStackFile := SelectYamlFile(apx.Cnf.UserStacksPath, name)
stack, err := LoadStackFromPath(usrStackFile)
if err != nil {
stack, err = LoadStackFromPath(filepath.Join(apx.Cnf.StacksPath, name+".yaml"))
stackFile := SelectYamlFile(apx.Cnf.StacksPath, name)
stack, err = LoadStackFromPath(stackFile)
}
return stack, err
}
Expand Down Expand Up @@ -80,7 +82,7 @@ func (stack *Stack) Save() error {
return err
}

filePath := filepath.Join(apx.Cnf.UserStacksPath, stack.Name+".yaml")
filePath := SelectYamlFile(apx.Cnf.UserStacksPath, stack.Name)
err = os.WriteFile(filePath, data, 0644)
return err
}
Expand All @@ -101,7 +103,7 @@ func (stack *Stack) Remove() error {
return errors.New("cannot remove built-in stack")
}

filePath := filepath.Join(apx.Cnf.UserStacksPath, stack.Name+".yaml")
filePath := SelectYamlFile(apx.Cnf.UserStacksPath, stack.Name)
err := os.Remove(filePath)
return err
}
Expand All @@ -115,7 +117,7 @@ func (stack *Stack) Export(path string) error {
}
}

filePath := filepath.Join(path, stack.Name+".yaml")
filePath := SelectYamlFile(path, stack.Name)
data, err := yaml.Marshal(stack)
if err != nil {
return err
Expand Down Expand Up @@ -180,8 +182,11 @@ func listStacksFromPath(path string) []*Stack {
}

for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".yaml" {
stackName := file.Name()[:len(file.Name())-5] // Remove the ".yaml" extension
extension := filepath.Ext(file.Name())

if !file.IsDir() && (extension == ".yaml" || extension == ".yml") {
// Remove the ".yaml" or ".yml" extension
stackName := file.Name()[:(len(file.Name()) - len(extension))]
stack, err := LoadStack(stackName)
if err == nil {
stacks = append(stacks, stack)
Expand Down
17 changes: 17 additions & 0 deletions core/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -119,3 +120,19 @@ func CopyFile(src, dst string) error {

return nil
}

func SelectYamlFile(basePath string, name string) string {
const (
YML string = ".yml"
YAML string = ".yaml"
)

yamlFile := filepath.Join(basePath, fmt.Sprintf("%s%s", name, YAML))
ymlFile := filepath.Join(basePath, fmt.Sprintf("%s%s", name, YML))

if _, err := os.Stat(yamlFile); errors.Is(err, os.ErrNotExist) {
return ymlFile
}

return yamlFile
}