Skip to content

Commit d458c03

Browse files
committed
Use utility.Paths instead of the CWD
1 parent be7d4f1 commit d458c03

File tree

4 files changed

+20
-53
lines changed

4 files changed

+20
-53
lines changed

pkg/commands/install.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package commands
33
import (
44
"errors"
55
"fmt"
6-
"os"
7-
"path/filepath"
86

97
"github.com/Glow-Project/ppm/pkg/utility"
108
"github.com/fatih/color"
@@ -17,20 +15,12 @@ func install(ctx *cli.Context) error {
1715
return err
1816
}
1917

20-
config, err := utility.ParsePpmConfig(filepath.Join(paths.Root, "ppm.json"))
18+
config, err := utility.ParsePpmConfig(paths.ConfigFile)
2119
if err != nil {
2220
return errors.New("could not find ppm.json file - try to run: ppm init")
2321
}
2422

25-
if !config.IsPlugin {
26-
pathExists, _ := utility.DoesPathExist(paths.Addons)
27-
if !pathExists {
28-
err := os.Mkdir(paths.Addons, 0755)
29-
if err != nil {
30-
return err
31-
}
32-
}
33-
}
23+
utility.CheckOrCreateDir(paths.Addons)
3424

3525
dependencies := ctx.Args()
3626
if dependencies.Len() > 0 {

pkg/commands/show.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ package commands
22

33
import (
44
"errors"
5-
"os"
6-
"path/filepath"
75

86
"github.com/Glow-Project/ppm/pkg/utility"
97
"github.com/urfave/cli/v2"
108
)
119

1210
func showConfig(ctx *cli.Context) error {
13-
// currentPath is the root directory of the project
14-
currentPath, err := os.Getwd()
11+
paths, err := utility.CreatePathsFromCwd()
1512
if err != nil {
1613
return err
1714
}
1815

19-
config, err := utility.ParsePpmConfig(filepath.Join(currentPath, "ppm.json"))
16+
config, err := utility.ParsePpmConfig(paths.ConfigFile)
2017
if err != nil {
2118
return errors.New("could not find ppm.json file - try to run: ppm init")
2219
}

pkg/commands/uninstall.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ import (
55
"fmt"
66
"os"
77
"path"
8-
"path/filepath"
98

109
"github.com/Glow-Project/ppm/pkg/utility"
1110
"github.com/fatih/color"
1211
"github.com/urfave/cli/v2"
1312
)
1413

1514
func uninstall(ctx *cli.Context) error {
16-
// currentPath is the root directory of the project
17-
currentPath, err := os.Getwd()
15+
paths, err := utility.CreatePathsFromCwd()
1816
if err != nil {
1917
return err
2018
}
2119

22-
config, err := utility.ParsePpmConfig(filepath.Join(currentPath, "ppm.json"))
20+
config, err := utility.ParsePpmConfig(paths.ConfigFile)
2321
if err != nil {
2422
return errors.New("could not find ppm.json file - try to run: ppm init")
2523
}
@@ -31,24 +29,24 @@ func uninstall(ctx *cli.Context) error {
3129
if !config.HasDependency(dep) && !config.HasSubDependency(dep) {
3230
fmt.Println(color.RedString("the plugin"), color.YellowString(dep), color.RedString("is not installed"))
3331
} else {
34-
uninstallDependency(&config, currentPath, dep, false)
32+
uninstallDependency(&config, paths, dep, false)
3533
}
3634

3735
}
3836

3937
} else {
40-
uninstallAllDependencies(config, currentPath, ctx.Bool("hard"))
38+
uninstallAllDependencies(config, paths, ctx.Bool("hard"))
4139
}
4240

4341
return nil
4442
}
4543

46-
func uninstallAllDependencies(config utility.PpmConfig, currentPath string, hard bool) error {
44+
func uninstallAllDependencies(config utility.PpmConfig, paths utility.Paths, hard bool) error {
4745
loading := make(chan interface{}, 1)
4846
go utility.PlayLoadingAnim(loading)
4947

5048
// path: root/addons
51-
err := os.RemoveAll(path.Join(currentPath, "addons"))
49+
err := os.RemoveAll(paths.Addons)
5250
if err != nil {
5351
return err
5452
}
@@ -62,7 +60,7 @@ func uninstallAllDependencies(config utility.PpmConfig, currentPath string, hard
6260
return nil
6361
}
6462

65-
func uninstallDependency(config *utility.PpmConfig, currentPath string, dependency string, isSubDependency bool) error {
63+
func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency string, isSubDependency bool) error {
6664
dep := utility.GetPluginName(dependency)
6765
if !isSubDependency {
6866
fmt.Println("\runinstalling", color.YellowString(dep))
@@ -72,18 +70,18 @@ func uninstallDependency(config *utility.PpmConfig, currentPath string, dependen
7270
loading := make(chan interface{}, 1)
7371
go utility.PlayLoadingAnim(loading)
7472

75-
subConfig, err := utility.GetPluginConfig(path.Join(currentPath, "addons"), dep)
73+
subConfig, err := utility.GetPluginConfig(paths.Addons, dep)
7674
if err == nil {
7775
for i := 0; i < len(subConfig.Dependencies); i++ {
7876
subDep := subConfig.Dependencies[i]
7977
if !config.HasDependency(subDep) {
80-
uninstallDependency(config, currentPath, subDep, true)
78+
uninstallDependency(config, paths, subDep, true)
8179
}
8280
}
8381
}
8482

8583
// path: root/addons/dependency
86-
err = os.RemoveAll(path.Join(currentPath, "addons", dep))
84+
err = os.RemoveAll(path.Join(paths.Addons, dep))
8785
loading <- nil
8886
if err != nil {
8987
return err

pkg/commands/update.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package commands
22

33
import (
44
"errors"
5-
"os"
65
"path/filepath"
76

87
"github.com/Glow-Project/ppm/pkg/utility"
@@ -12,53 +11,36 @@ import (
1211
// The update method is called by the cli
1312
// It updates all dependencies
1413
func update(ctx *cli.Context) error {
15-
currentPath, err := os.Getwd()
14+
paths, err := utility.CreatePathsFromCwd()
1615
if err != nil {
1716
return err
1817
}
1918

20-
config, err := utility.ParsePpmConfig(filepath.Join(currentPath, "ppm.json"))
19+
config, err := utility.ParsePpmConfig(paths.ConfigFile)
2120
if err != nil {
2221
return errors.New("could not find ppm.json file - try to run: ppm init")
2322
}
2423

25-
if config.IsPlugin {
26-
os.Chdir(filepath.Dir(currentPath))
27-
} else {
28-
addonPath := filepath.Join(currentPath, "addons")
29-
pathExists, _ := utility.DoesPathExist(addonPath)
30-
if !pathExists {
31-
err := os.Mkdir("addons", 0755)
32-
if err != nil {
33-
return err
34-
}
35-
}
36-
os.Chdir(addonPath)
37-
}
38-
39-
newPath, err := os.Getwd()
40-
if err != nil {
41-
return err
42-
}
24+
utility.CheckOrCreateDir(paths.Addons)
4325

4426
loading := make(chan interface{}, 1)
4527
go utility.PlayLoadingAnim(loading)
46-
updateAllDependencies(config, newPath)
28+
updateAllDependencies(config, paths)
4729
loading <- nil
4830

4931
utility.PrintDone()
5032

5133
return nil
5234
}
5335

54-
func updateAllDependencies(config utility.PpmConfig, currentPath string) error {
36+
func updateAllDependencies(config utility.PpmConfig, paths utility.Paths) error {
5537
for _, dependency := range config.Dependencies {
5638
_, version := utility.GetVersionOrNot(dependency)
5739
if len(version) > 0 {
5840
continue
5941
}
6042

61-
err := utility.Update(filepath.Join(currentPath, dependency))
43+
err := utility.Update(filepath.Join(paths.Addons, dependency))
6244
if err != nil {
6345
return err
6446
}

0 commit comments

Comments
 (0)