Skip to content

Commit

Permalink
error management for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Dar-rius committed Nov 3, 2022
1 parent b380218 commit 87aa5db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
27 changes: 13 additions & 14 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions cmd/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/spf13/cobra"
Expand All @@ -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)
Expand Down

0 comments on commit 87aa5db

Please sign in to comment.