Skip to content

Commit bc2ea1e

Browse files
committed
add delete command, close #21
1 parent da17b38 commit bc2ea1e

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed

cmd/configure.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/clivern/poodle/core/module"
1414
"github.com/clivern/poodle/core/util"
1515

16+
. "github.com/logrusorgru/aurora/v3"
1617
log "github.com/sirupsen/logrus"
1718
"github.com/spf13/cobra"
1819
)
@@ -194,7 +195,7 @@ var configureCmd = &cobra.Command{
194195
"file": Config,
195196
}).Debug("Configs Updated")
196197

197-
fmt.Println("Configs Updated")
198+
fmt.Println(Green("Configs Updated"))
198199
},
199200
}
200201

cmd/delete.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2020 Clivern. All rights reserved.
2+
// Use of this source code is governed by the MIT
3+
// license that can be found in the LICENSE file.
4+
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
"time"
11+
12+
"github.com/clivern/poodle/core/model"
13+
"github.com/clivern/poodle/core/module"
14+
"github.com/clivern/poodle/core/util"
15+
16+
"github.com/briandowns/spinner"
17+
. "github.com/logrusorgru/aurora/v3"
18+
log "github.com/sirupsen/logrus"
19+
"github.com/spf13/cobra"
20+
)
21+
22+
var deleteCmd = &cobra.Command{
23+
Use: "delete",
24+
Short: "Delete a service definition file",
25+
Run: func(cmd *cobra.Command, args []string) {
26+
var err error
27+
28+
if Verbose {
29+
log.SetLevel(log.DebugLevel)
30+
}
31+
32+
log.Debug("Delete command got called.")
33+
34+
if !util.FileExists(Config) {
35+
fmt.Printf(
36+
"Config file is missing %s, Please start with $ poodle configure",
37+
Config,
38+
)
39+
return
40+
}
41+
42+
conf := model.NewConfigs()
43+
err = conf.Decode(Config)
44+
45+
if err != nil {
46+
fmt.Printf(
47+
"Error while decoding configs %s: %s",
48+
Config,
49+
err.Error(),
50+
)
51+
return
52+
}
53+
54+
files, err := util.ListFiles(util.EnsureTrailingSlash(conf.Services.Directory))
55+
56+
if err != nil {
57+
fmt.Printf(
58+
"Error while listing services under %s: %s",
59+
util.EnsureTrailingSlash(conf.Services.Directory),
60+
err.Error(),
61+
)
62+
return
63+
}
64+
65+
data := []string{}
66+
index := map[string]string{}
67+
service := &model.Service{}
68+
69+
for _, v := range files {
70+
if strings.Contains(v.Name, ".toml") {
71+
72+
service = model.NewService(v.Name)
73+
err = service.Decode(v.Path)
74+
75+
if err != nil {
76+
fmt.Printf(
77+
"Error while decoding service %s: %s",
78+
v.Path,
79+
err.Error(),
80+
)
81+
return
82+
}
83+
84+
data = append(
85+
data,
86+
service.Main.ID,
87+
)
88+
89+
index[service.Main.ID] = v.Path
90+
}
91+
}
92+
93+
result := ""
94+
finder := module.FuzzyFinder{}
95+
prompt := module.Prompt{}
96+
97+
if finder.Available() {
98+
result, err = finder.Show(data)
99+
} else {
100+
result, err = prompt.Select(
101+
fmt.Sprintf("Select an Endpoint"),
102+
data,
103+
)
104+
}
105+
106+
if err != nil {
107+
fmt.Printf("Error: %s", err.Error())
108+
return
109+
}
110+
111+
spin := spinner.New(spinner.CharSets[26], 100*time.Millisecond)
112+
spin.Color("green")
113+
spin.Start()
114+
115+
err = util.DeleteFile(index[result])
116+
117+
spin.Stop()
118+
119+
if err != nil {
120+
fmt.Printf("Error: %s", err.Error())
121+
}
122+
123+
fmt.Println(Green("Service file deleted successfully"))
124+
},
125+
}
126+
127+
func init() {
128+
rootCmd.AddCommand(deleteCmd)
129+
}

cmd/edit.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/clivern/poodle/core/module"
1313
"github.com/clivern/poodle/core/util"
1414

15+
. "github.com/logrusorgru/aurora/v3"
1516
log "github.com/sirupsen/logrus"
1617
"github.com/spf13/cobra"
1718
)
@@ -106,7 +107,7 @@ var editCmd = &cobra.Command{
106107
"file": absPath,
107108
}).Debug("Service file updated")
108109

109-
fmt.Println("Service file updated")
110+
fmt.Println(Green("Service file updated"))
110111
},
111112
}
112113

cmd/new.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/clivern/poodle/core/module"
1414
"github.com/clivern/poodle/core/util"
1515

16+
. "github.com/logrusorgru/aurora/v3"
1617
log "github.com/sirupsen/logrus"
1718
"github.com/spf13/cobra"
1819
)
@@ -113,7 +114,7 @@ var newCmd = &cobra.Command{
113114
"file": absPath,
114115
}).Debug("Service file created")
115116

116-
fmt.Println("Service file created")
117+
fmt.Println(Green("Service file created successfully"))
117118
},
118119
}
119120

cmd/sync.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/clivern/poodle/core/util"
1616

1717
"github.com/briandowns/spinner"
18+
. "github.com/logrusorgru/aurora/v3"
1819
log "github.com/sirupsen/logrus"
1920
"github.com/spf13/cobra"
2021
)
@@ -171,7 +172,7 @@ var syncCmd = &cobra.Command{
171172

172173
spin.Stop()
173174

174-
fmt.Println("Already up-to-date")
175+
fmt.Println(Green("Already up-to-date"))
175176
},
176177
}
177178

core/util/helpers.go

+5
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,8 @@ func EnsureDir(dirName string, mode int) (bool, error) {
220220
}
221221
return false, err
222222
}
223+
224+
// DeleteFile deletes a file
225+
func DeleteFile(path string) error {
226+
return os.Remove(path)
227+
}

0 commit comments

Comments
 (0)