From 87aa5db9880cef689c47c0e8660e91fecda449e6 Mon Sep 17 00:00:00 2001 From: Dar-rius Date: Thu, 3 Nov 2022 13:53:47 +0000 Subject: [PATCH] error management for commands --- cmd/add.go | 27 +++++++++++++-------------- cmd/go.go | 8 ++++---- cmd/ls.go | 7 ++++--- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 81f1622..e5359d2 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -21,7 +21,7 @@ var addCmd = &cobra.Command{ //init struct project var project Project - if args == nil && args[0] == "" && args[1] == "" || len(args) > 2 { + if args == nil && args[0] == "" && args[1] == "" || len(args) > 2 || len(args) < 2 { log.Fatal("Command error") } else if args[0] != "" && args[1] == "." { project.name = args[0] @@ -53,27 +53,26 @@ func addProjectActually(project *Project) { func addProject(project *Project) { //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 filEnv := os.Getenv("goproject") - _, errr := os.OpenFile(filEnv+"/path.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) - if errr != nil { - panic(errr) + _, errs := os.OpenFile(filEnv+"/path.json", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + if errs != nil { + panic(errs) } - vp := viper.New() - vp.SetConfigName("path") - vp.SetConfigType("json") - vp.AddConfigPath(filEnv) - err := vp.ReadInConfig() + viper.SetConfigName("path") + viper.SetConfigType("json") + viper.AddConfigPath(filEnv) + err := viper.ReadInConfig() if err != nil { fmt.Println(err) } - vp.Set(project.name, project.path) - vp.WriteConfig() + viper.Set(project.name, project.path) + viper.WriteConfig() - vp.OnConfigChange(func(in fsnotify.Event) { - fmt.Printf("le projet %s ete ajouter", in.Name) + viper.OnConfigChange(func(in fsnotify.Event) { + fmt.Printf("project add") }) - vp.WatchConfig() + viper.WatchConfig() } func init() { diff --git a/cmd/go.go b/cmd/go.go index 8c81039..9a37c8b 100644 --- a/cmd/go.go +++ b/cmd/go.go @@ -17,7 +17,7 @@ var goCmd = &cobra.Command{ Long: `This command will allow you to move to the repository of a project that you have chosen example: gproject go projectA`, Run: func(cmd *cobra.Command, args []string) { - if args == nil && args[0] == "" || len(args) > 1 { + if args == nil && args[0] == "" || len(args) > 1 || len(args) < 1 { log.Fatal("Command error") } else { goPath(&args[0]) @@ -38,12 +38,12 @@ func goPath(project *string) { if err != nil { log.Fatal(err) } - path := viper.GetString(*project) - if path == "" { + pathProject := viper.GetString(*project) + if pathProject == "" { log.Fatal("Error, this project is not saved") } - os.Chdir(path) + os.Chdir(pathProject) dir, err := os.Getwd() if err != nil { panic(err) diff --git a/cmd/ls.go b/cmd/ls.go index ffd332d..a434c76 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "log" "os" "github.com/spf13/cobra" @@ -15,16 +16,16 @@ var lsCmd = &cobra.Command{ Long: `This command will list all the project saved in the path.json file example: gproject ls`, Run: func(cmd *cobra.Command, args []string) { - //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 + //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 filEnv := os.Getenv("goproject") file, err := os.ReadFile(filEnv + "/path.json") if err != nil { - panic(err) + log.Fatal("Error, there are no projects saved") } var data map[string]interface{} err = json.Unmarshal(file, &data) if err != nil { - panic(err) + log.Fatal(err) } for k, _ := range data { fmt.Println(k)