Skip to content

Commit 4bdb236

Browse files
committed
feat: ✅ improve error handling
1 parent c6193b7 commit 4bdb236

File tree

6 files changed

+74
-56
lines changed

6 files changed

+74
-56
lines changed

commands/install.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package commands
22

33
import (
4-
"log"
4+
"errors"
55
"os/exec"
66
"path/filepath"
77

@@ -11,48 +11,51 @@ import (
1111
"ui/cli/module"
1212
)
1313

14-
func Install(c *cli.Context) {
14+
func Install(c *cli.Context) error {
1515
var cmd = exec.Command("sudo", "-v")
1616
cmd.Stdout = nil
1717
err := cmd.Run()
1818
if err != nil {
19-
log.Fatal(err)
19+
return err
2020
}
2121

2222
module.CreateCacheDotFolder()
2323
config, err := module.GetSource()
2424
if err != nil {
25-
log.Fatal(err)
25+
return err
2626
}
2727

2828
packageName := c.Args().Get(0)
2929
if packageName != "" {
3030
for _, file := range config.Source {
3131
if file.Name == packageName {
3232
filePath := module.DownloadFileToCache(file.URL)
33-
installPackage(filePath)
34-
return
33+
err = installPackage(filePath)
34+
if err != nil {
35+
return err
36+
}
37+
return nil
3538
}
3639
}
37-
log.Fatal("Package not found")
38-
}
39-
40-
for _, file := range config.Source {
41-
filePath := module.DownloadFileToCache(file.URL)
42-
installPackage(filePath)
40+
color.Redln("Package not found.")
41+
return errors.New("1")
42+
} else {
43+
color.Redln("No package specified.")
44+
return errors.New("1")
4345
}
4446
}
4547

46-
func installPackage(filePath string) {
48+
func installPackage(filePath string) error {
4749
fileName := filepath.Base(filePath)
4850

4951
color.Yellowln("Installing", fileName)
5052
module.FullWidthMessage("installation log start", color.Gray)
5153
err := module.InstallPackageWithFilePath(filePath)
5254
if err != nil {
53-
log.Fatal(err)
55+
return err
5456
}
5557

5658
module.FullWidthMessage("installation log end", color.Gray)
5759
color.Greenln("Successfully installed", fileName)
60+
return nil
5861
}

commands/source.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
package commands
22

33
import (
4-
"log"
5-
"ui/cli/module"
6-
74
"github.com/gookit/color"
85
"github.com/urfave/cli/v2"
6+
7+
"ui/cli/module"
98
)
109

11-
func Source(c *cli.Context) {
10+
func Source(c *cli.Context) error {
1211
if c.Args().Get(0) == "list" || (c.String("name") == "") || (c.String("url") == "") {
1312
config, err := module.GetSource()
1413
if err != nil {
15-
log.Fatal(err)
14+
return err
1615
}
1716

1817
if len(config.Source) == 0 {
19-
color.Yellowln("No files are installed.")
20-
return
18+
color.Yellowln("There are no package in source.")
19+
return nil
2120
}
2221

2322
for _, file := range config.Source {
2423
color.Cyanf("%s: ", file.Name)
2524
color.Greenln(file.URL)
2625
}
2726

28-
return
27+
return nil
2928
}
3029

3130
config, err := module.SetSource(c.String("name"), c.String("url"))
3231
if err != nil {
33-
log.Fatal(err)
32+
return err
3433
}
3534

3635
module.SaveToConfigFile(config)
36+
return nil
3737
}

commands/update-self.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package commands
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"os/exec"
8-
"ui/cli/module"
97

108
"github.com/gookit/color"
119
"github.com/urfave/cli/v2"
10+
11+
"ui/cli/module"
1212
)
1313

14-
func UpdateSelf(c *cli.Context) {
14+
func UpdateSelf(c *cli.Context) error {
1515
var cmd = exec.Command("sudo", "-v")
1616
cmd.Stdout = nil
1717
err := cmd.Run()
1818
if err != nil {
19-
log.Fatal(err)
19+
return err
2020
}
2121

2222
color.Yellowln("Updating ui-cli")
@@ -30,17 +30,17 @@ func UpdateSelf(c *cli.Context) {
3030
cmd.Stderr = os.Stderr
3131
err = cmd.Run()
3232
if err != nil {
33-
log.Fatal(err)
33+
return err
3434
}
35-
fmt.Println("Moved", filePath, "to /usr/local/bin")
35+
color.Greenln("Moved", filePath, "to /usr/local/bin")
3636
cmd = exec.Command("sudo", "chmod", "+x", "/usr/local/bin/ui")
3737
cmd.Stdout = os.Stdout
3838
cmd.Stderr = os.Stderr
3939
err = cmd.Run()
4040
if err != nil {
41-
log.Fatal(err)
41+
return err
4242
}
43-
fmt.Println("Changed permission for /usr/local/bin/ui")
43+
color.Greenln("Changed permission for /usr/local/bin/ui")
4444
module.FullWidthMessage("installation log end", color.Gray)
4545

4646
color.Greenf("Successfully updated ui-cli to ")
@@ -49,6 +49,7 @@ func UpdateSelf(c *cli.Context) {
4949
cmd.Stderr = os.Stderr
5050
err = cmd.Run()
5151
if err != nil {
52-
log.Fatal(err)
52+
return err
5353
}
54+
return nil
5455
}

commands/version.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
func Version(ctx *cli.Context) error {
10+
fmt.Println(ctx.App.Version)
11+
return nil
12+
}

main.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4-
"fmt"
5-
"log"
64
"os"
5+
"strconv"
76

7+
"github.com/gookit/color"
88
"github.com/urfave/cli/v2"
99

1010
"ui/cli/commands"
@@ -21,10 +21,7 @@ func main() {
2121
Name: "version",
2222
Aliases: []string{"v"},
2323
Usage: "show the version of ui",
24-
Action: func(ctx *cli.Context) error {
25-
fmt.Println(ctx.App.Version)
26-
return nil
27-
},
24+
Action: commands.Version,
2825
},
2926
{
3027
Name: "install",
@@ -38,10 +35,7 @@ func main() {
3835
Usage: "name of the file to install",
3936
},
4037
},
41-
Action: func(ctx *cli.Context) error {
42-
commands.Install(ctx)
43-
return nil
44-
},
38+
Action: commands.Install,
4539
},
4640
{
4741
Name: "source",
@@ -60,10 +54,7 @@ func main() {
6054
Usage: "url of the source",
6155
},
6256
},
63-
Action: func(ctx *cli.Context) error {
64-
commands.Source(ctx)
65-
return nil
66-
},
57+
Action: commands.Source,
6758
},
6859
{
6960
Name: "update-self",
@@ -78,6 +69,13 @@ func main() {
7869
}
7970

8071
if err := app.Run(os.Args); err != nil {
81-
log.Fatal(err)
72+
var errorString = err.Error()
73+
74+
if errorNumber, err := strconv.Atoi(errorString); err == nil {
75+
os.Exit(errorNumber)
76+
}
77+
78+
color.Redln(errorString)
79+
os.Exit(1)
8280
}
8381
}

module/config.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"log"
87
"os"
98
"path/filepath"
9+
10+
"github.com/gookit/color"
1011
)
1112

1213
type SourceObject struct {
@@ -18,13 +19,12 @@ type SourceJSON struct {
1819
Source []SourceObject `json:"source"`
1920
}
2021

21-
func CreateConfigFile() {
22+
func CreateConfigFile() error {
2223
home, err := os.UserHomeDir()
2324
if err != nil {
24-
log.Fatal(err)
25+
return err
2526
}
2627

27-
// create dot folder if it is not exist.
2828
CreateCacheDotFolder()
2929

3030
path := filepath.Join(home, ".ui")
@@ -33,14 +33,15 @@ func CreateConfigFile() {
3333
if os.IsNotExist(err) {
3434
file, err := os.Create(configPath)
3535
if err != nil {
36-
log.Fatal(err)
36+
return err
3737
}
3838
defer file.Close()
3939
_, err = file.WriteString("{}")
4040
if err != nil {
41-
log.Fatal(err)
41+
return err
4242
}
4343
}
44+
return nil
4445
}
4546

4647
func GetSource() (SourceJSON, error) {
@@ -51,7 +52,10 @@ func GetSource() (SourceJSON, error) {
5152
return config, err
5253
}
5354

54-
CreateConfigFile()
55+
err = CreateConfigFile()
56+
if err != nil {
57+
return config, err
58+
}
5559

5660
configFilePath := filepath.Join(home, ".ui", "config.json")
5761

@@ -80,15 +84,15 @@ func SetSource(name string, URL string) (SourceJSON, error) {
8084

8185
for i, file := range config.Source {
8286
if file.Name == name {
83-
fmt.Printf("The URL of the name is already exist. Would you like to change the URL of %s? (y/N) ", name)
87+
color.Yellowf("The URL of the name is already exist. Would you like to change the URL of %s? (y/N) ", name)
8488
var answer string
8589
fmt.Scanln(&answer)
8690

8791
if answer == "y" || answer == "Y" {
8892
config.Source[i].URL = URL
8993
return config, nil
9094
} else {
91-
fmt.Println("No changes were made")
95+
color.Redln("No changes were made.")
9296
return config, nil
9397
}
9498
}

0 commit comments

Comments
 (0)