Skip to content

Commit 87aa5db

Browse files
committed
error management for commands
1 parent b380218 commit 87aa5db

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

cmd/add.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var addCmd = &cobra.Command{
2121
//init struct project
2222
var project Project
2323

24-
if args == nil && args[0] == "" && args[1] == "" || len(args) > 2 {
24+
if args == nil && args[0] == "" && args[1] == "" || len(args) > 2 || len(args) < 2 {
2525
log.Fatal("Command error")
2626
} else if args[0] != "" && args[1] == "." {
2727
project.name = args[0]
@@ -53,27 +53,26 @@ func addProjectActually(project *Project) {
5353
func addProject(project *Project) {
5454
//the environment variable is stored in a variable in order to create and find the path.json file in the directory where the app is located
5555
filEnv := os.Getenv("goproject")
56-
_, errr := os.OpenFile(filEnv+"/path.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
57-
if errr != nil {
58-
panic(errr)
56+
_, errs := os.OpenFile(filEnv+"/path.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
57+
if errs != nil {
58+
panic(errs)
5959
}
6060

61-
vp := viper.New()
62-
vp.SetConfigName("path")
63-
vp.SetConfigType("json")
64-
vp.AddConfigPath(filEnv)
65-
err := vp.ReadInConfig()
61+
viper.SetConfigName("path")
62+
viper.SetConfigType("json")
63+
viper.AddConfigPath(filEnv)
64+
err := viper.ReadInConfig()
6665
if err != nil {
6766
fmt.Println(err)
6867
}
6968

70-
vp.Set(project.name, project.path)
71-
vp.WriteConfig()
69+
viper.Set(project.name, project.path)
70+
viper.WriteConfig()
7271

73-
vp.OnConfigChange(func(in fsnotify.Event) {
74-
fmt.Printf("le projet %s ete ajouter", in.Name)
72+
viper.OnConfigChange(func(in fsnotify.Event) {
73+
fmt.Printf("project add")
7574
})
76-
vp.WatchConfig()
75+
viper.WatchConfig()
7776
}
7877

7978
func init() {

cmd/go.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var goCmd = &cobra.Command{
1717
Long: `This command will allow you to move to the repository of a project that you have chosen
1818
example: gproject go projectA`,
1919
Run: func(cmd *cobra.Command, args []string) {
20-
if args == nil && args[0] == "" || len(args) > 1 {
20+
if args == nil && args[0] == "" || len(args) > 1 || len(args) < 1 {
2121
log.Fatal("Command error")
2222
} else {
2323
goPath(&args[0])
@@ -38,12 +38,12 @@ func goPath(project *string) {
3838
if err != nil {
3939
log.Fatal(err)
4040
}
41-
path := viper.GetString(*project)
42-
if path == "" {
41+
pathProject := viper.GetString(*project)
42+
if pathProject == "" {
4343
log.Fatal("Error, this project is not saved")
4444
}
4545

46-
os.Chdir(path)
46+
os.Chdir(pathProject)
4747
dir, err := os.Getwd()
4848
if err != nil {
4949
panic(err)

cmd/ls.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6+
"log"
67
"os"
78

89
"github.com/spf13/cobra"
@@ -15,16 +16,16 @@ var lsCmd = &cobra.Command{
1516
Long: `This command will list all the project saved in the path.json file
1617
example: gproject ls`,
1718
Run: func(cmd *cobra.Command, args []string) {
18-
//the environment variable is stored in a variable in order to create and find the path.json file in the directory where the app is located
19+
//the environment variable is stored in a variable in order to create and find the path.json file in the directory where the app is located;w
1920
filEnv := os.Getenv("goproject")
2021
file, err := os.ReadFile(filEnv + "/path.json")
2122
if err != nil {
22-
panic(err)
23+
log.Fatal("Error, there are no projects saved")
2324
}
2425
var data map[string]interface{}
2526
err = json.Unmarshal(file, &data)
2627
if err != nil {
27-
panic(err)
28+
log.Fatal(err)
2829
}
2930
for k, _ := range data {
3031
fmt.Println(k)

0 commit comments

Comments
 (0)