Skip to content

Commit 724dcca

Browse files
author
permito
committed
UX improvements, proper command usage and errors, small update v0.3.1
1 parent 505ae5c commit 724dcca

File tree

12 files changed

+119
-53
lines changed

12 files changed

+119
-53
lines changed

hp/cmd/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ var ListCmd = &cli.Command{
1111
Name: "list",
1212
Aliases: []string{"l"},
1313
Usage: "Lists currently pulled packages in workspace",
14+
UsageText: "hp list/l",
1415
Description: "List all installed packages along with their identifiers. Ids correspond to order the packages have been added by and names are git repository names stripped of the author.",
1516
Action: func(cCtx *cli.Context) error {
17+
if cCtx.Args().Present() {
18+
return hp.ErrArg
19+
}
20+
1621
if !pkg.Initialized() {
1722
return hp.ErrNotInWorkspace
1823
}

hp/cmd/pull.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"HeaderPuller/hp/internal/ops"
77
"HeaderPuller/hp/internal/pkg"
88
"HeaderPuller/hp/internal/repo"
9+
"errors"
910
"github.com/go-git/go-billy/v5/memfs"
1011
"github.com/go-git/go-git/v5"
1112
"github.com/go-git/go-git/v5/storage/memory"
@@ -17,16 +18,19 @@ var PullCmd = &cli.Command{
1718
Aliases: []string{"p"},
1819
Flags: []cli.Flag{
1920
&cli.BoolFlag{
20-
Name: "ignore-extensions",
21-
Aliases: []string{"i"},
22-
Usage: "ignore file extensions, allows pulling any type of file.",
21+
Name: "ignore-extensions",
22+
Aliases: []string{"i"},
23+
Usage: "ignore file extensions, allows pulling any type of file",
24+
DisableDefaultText: true,
2325
},
2426
&cli.BoolFlag{
25-
Name: "force",
26-
Aliases: []string{"f"},
27-
Usage: "force pull.",
27+
Name: "force",
28+
Aliases: []string{"f"},
29+
Usage: "force pull",
30+
DisableDefaultText: true,
2831
}},
29-
Usage: "Pull headers in specified folder and update/create the ops file",
32+
Usage: "Pull headers in specified folder and update/create the package log file",
33+
UsageText: "hp pull/p <repo-link> [file]/[from]",
3034
Description: `There are 3 variations of this command:
3135
- pull <repo-link> - providing the repo link will copy every valid file from <repo-link>/include/ to ./include/
3236
- pull <repo-link> <file> - will copy that exact file if valid from <repo-link>/ to ./include/
@@ -35,6 +39,8 @@ var PullCmd = &cli.Command{
3539
Action: func(cCtx *cli.Context) error {
3640
if !cCtx.Args().Present() {
3741
return hp.ErrNoArg
42+
} else if cCtx.Args().Len() > 2 {
43+
return errors.New("requires one or two arguments")
3844
}
3945

4046
repoLink, headerDir := cCtx.Args().First(), cCtx.Args().Get(1)

hp/cmd/remove.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ const (
1919
)
2020

2121
var RemoveCmd = &cli.Command{
22-
Name: "remove",
23-
Aliases: []string{"rm", "r"},
24-
Usage: "Removes a package and updates the ops file",
22+
Name: "remove",
23+
Aliases: []string{"rm", "r"},
24+
Usage: "Removes a package and updates the package log file",
25+
UsageText: "hp remove/rm/r <id>/<name>/<repo-link>",
2526
Description: `Removes files and folders of all header files encompassing a package. There are 3 variations of this command:
2627
- remove <id> - delete by id
2728
- remove <name> - remove by package name
@@ -30,12 +31,12 @@ var RemoveCmd = &cli.Command{
3031
The ids and packages names are provided by the list command.
3132
`,
3233
Action: func(cCtx *cli.Context) error {
33-
if !pkg.Initialized() {
34-
return hp.ErrNotInWorkspace
34+
if cCtx.Args().Len() != 1 {
35+
return hp.ErrNoArg
3536
}
3637

37-
if !cCtx.Args().Present() {
38-
return hp.ErrNoArg
38+
if !pkg.Initialized() {
39+
return hp.ErrNotInWorkspace
3940
}
4041

4142
arg := cCtx.Args().First()

hp/cmd/sync.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ import (
99
)
1010

1111
var SyncCmd = &cli.Command{
12-
Name: "sync",
13-
Aliases: []string{"s"},
14-
Usage: "Updates every package to the latest version.",
12+
Name: "sync",
13+
Aliases: []string{"s"},
14+
Usage: "Updates every package to the latest version",
15+
UsageText: "hp sync/s",
1516
Action: func(cCtx *cli.Context) error {
17+
if cCtx.Args().Present() {
18+
return hp.ErrArg
19+
}
20+
1621
if !pkg.Initialized() {
1722
return hp.ErrNotInWorkspace
1823
}

hp/cmd/uninstall.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"HeaderPuller/hp"
5+
"HeaderPuller/hp/internal/ops"
46
"bufio"
57
"fmt"
68
"github.com/urfave/cli/v2"
@@ -9,25 +11,43 @@ import (
911
)
1012

1113
var UninstallCmd = &cli.Command{
12-
Name: "uninstall",
13-
Usage: "Upon confirmation, wipes hp from the computer entirely",
14-
Action: Uninstall,
14+
Name: "uninstall",
15+
Flags: []cli.Flag{
16+
&cli.BoolFlag{
17+
Name: "no-confirmation",
18+
Usage: "doesn't ask for uninstall confirmation.",
19+
DisableDefaultText: true,
20+
},
21+
},
22+
Usage: "Upon confirmation, wipes hp from the computer entirely",
23+
UsageText: "hp uninstall",
24+
Action: func(cCtx *cli.Context) error {
25+
if cCtx.Args().Present() {
26+
return hp.ErrArg
27+
}
28+
29+
conf := ops.New()
30+
conf.SetNoConfirm(cCtx.Bool("no-confirmation"))
31+
32+
return Uninstall(conf)
33+
},
1534
}
1635

17-
var Uninstall cli.ActionFunc = func(cCtx *cli.Context) error {
18-
scanner := bufio.NewScanner(os.Stdin)
19-
fmt.Print("Are you sure? [Y/N] ")
20-
scanner.Scan()
21-
answer := scanner.Text()
22-
if strings.ToUpper(answer) != "Y" {
23-
return nil
36+
func Uninstall(c *ops.Config) error {
37+
if !c.NoConfirm() {
38+
scanner := bufio.NewScanner(os.Stdin)
39+
fmt.Print("Are you sure? [Y/N] ")
40+
scanner.Scan()
41+
answer := scanner.Text()
42+
if strings.ToUpper(answer) != "Y" {
43+
return nil
44+
}
2445
}
2546

2647
path := fmt.Sprintf("%v/bin/hp", os.Getenv("GOPATH"))
2748
err := os.Remove(path)
2849
if err != nil {
2950
return fmt.Errorf("%v\nhp executable must have been moved or already removed", err)
3051
}
31-
fmt.Println("Uninstalled successfully")
32-
return nil
52+
return hp.NoErrUninstalled
3353
}

hp/cmd/update.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ import (
1111
)
1212

1313
var UpdateCmd = &cli.Command{
14-
Name: "update",
15-
Aliases: []string{"u"},
16-
Usage: "Update to the latest git commit",
14+
Name: "update",
15+
Aliases: []string{"u"},
16+
Usage: "Update to the latest git commit",
17+
UsageText: "hp update/u",
1718
Action: func(cCtx *cli.Context) error {
19+
if cCtx.Args().Present() {
20+
return hp.ErrArg
21+
}
22+
1823
executablePath, err := exec.LookPath("hp")
1924
if err != nil {
2025
return errors.New("hp executable not found, update unable to finish")
2126
}
2227

2328
tmp := strconv.Itoa(time.Now().Nanosecond())
24-
if err := exec.Command("git", "clone", "https://github.com/Depermitto/HeaderPuller", tmp).Run(); err != nil {
29+
if err := exec.Command("git", "clone", hp.RepoLink, tmp).Run(); err != nil {
2530
return errors.New("cannot clone repo, update unable to finish")
2631
}
2732
defer os.RemoveAll(tmp)

hp/cmd/version.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package cmd
22

33
import (
4+
"HeaderPuller/hp"
45
"fmt"
56
"github.com/urfave/cli/v2"
67
)
78

89
var VersionCmd = &cli.Command{
9-
Name: "version",
10-
Aliases: []string{"v"},
11-
Usage: "Get program version",
10+
Name: "version",
11+
Aliases: []string{"v"},
12+
Usage: "Get program version",
13+
UsageText: "hp version/v",
1214
Action: func(cCtx *cli.Context) error {
15+
if cCtx.Args().Present() {
16+
return hp.ErrArg
17+
}
18+
1319
fmt.Printf("hp version %v\n", cCtx.App.Version)
1420
return nil
1521
},

hp/cmd/wipe.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import (
99
)
1010

1111
var WipeCmd = &cli.Command{
12-
Name: "wipe",
13-
Usage: "Removes all pulled packages and the the *hp.yaml* file itself.",
12+
Name: "wipe",
13+
Usage: "Removes all pulled packages and the the *hp.yaml* file itself",
14+
UsageText: "hp wipe",
1415
Action: func(cCtx *cli.Context) error {
15-
if !pkg.Initialized() {
16-
return hp.ErrNotInWorkspace
17-
}
18-
1916
if cCtx.Args().Present() {
2017
return hp.ErrArg
2118
}
2219

20+
if !pkg.Initialized() {
21+
return hp.ErrNotInWorkspace
22+
}
23+
2324
return Wipe()
2425
},
2526
}

hp/hp.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ const (
1313
)
1414

1515
var (
16-
ErrNoArg = errors.New("requires one argument")
16+
ErrNoArg = errors.New("no arguments provided")
17+
ErrArg = errors.New("command doesn't take any arguments")
1718
ErrNoFilesFound = errors.New("no files found")
18-
ErrArg = errors.New("this configuration doesn't take any arguments")
1919
ErrNotInWorkspace = errors.New("not in hp workspace")
2020
ErrAlreadyDownloaded = errors.New("already downloaded this package")
21-
NoErrPulled = errors.New("pull successful")
22-
NoErrWiped = errors.New("wipe successful")
23-
NoErrRemoved = errors.New("removal successful")
24-
NoErrSynced = errors.New("sync successful")
25-
NoErrUpdated = errors.New("update successful")
21+
NoErrPulled = errors.New("pulled")
22+
NoErrWiped = errors.New("wiped")
23+
NoErrRemoved = errors.New("removed")
24+
NoErrSynced = errors.New("synced")
25+
NoErrUpdated = errors.New("updated")
26+
NoErrUninstalled = errors.New("uninstalled")
2627
)

hp/internal/ops/ops.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ops
33
type Config struct {
44
force bool
55
ignoreExt bool
6+
noConfirm bool
67
}
78

89
func (c *Config) Force() bool {
@@ -21,6 +22,14 @@ func (c *Config) SetIgnoreExt(ignoreExt bool) {
2122
c.ignoreExt = ignoreExt
2223
}
2324

25+
func (c *Config) NoConfirm() bool {
26+
return c.noConfirm
27+
}
28+
29+
func (c *Config) SetNoConfirm(noConfirm bool) {
30+
c.noConfirm = noConfirm
31+
}
32+
2433
func New(configFuncs ...func(*Config)) *Config {
2534
config := &Config{}
2635
for _, c := range configFuncs {
@@ -33,6 +42,10 @@ func WithIgnoreExt(config *Config) {
3342
config.ignoreExt = true
3443
}
3544

45+
func WithNoConfirm(config *Config) {
46+
config.noConfirm = true
47+
}
48+
3649
func WithForce(config *Config) {
3750
config.force = true
3851
}

0 commit comments

Comments
 (0)