2023-03-04 22:22:03 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/abs3ntdev/haunt/src/haunt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var removeCmd = &cobra.Command{
|
2023-03-08 21:46:44 +00:00
|
|
|
Use: "remove [names]",
|
|
|
|
Aliases: []string{"delete", "r"},
|
|
|
|
Short: "Removes all projects by name from config file",
|
|
|
|
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
|
2023-03-04 22:22:03 +00:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return getProjectNames(toComplete), cobra.ShellCompDirectiveNoFileComp
|
|
|
|
},
|
|
|
|
RunE: remove,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(removeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProjectNames(input string) []string {
|
2023-03-06 07:39:01 +00:00
|
|
|
h := haunt.NewHaunt()
|
2023-03-04 22:22:03 +00:00
|
|
|
// read a config if exist
|
2023-03-06 07:39:01 +00:00
|
|
|
err := h.Settings.Read(&h)
|
2023-03-04 22:22:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
names := []string{}
|
2023-03-06 07:39:01 +00:00
|
|
|
for _, project := range h.Projects {
|
2023-03-04 22:22:03 +00:00
|
|
|
if strings.HasPrefix(project.Name, input) {
|
|
|
|
names = append(names, project.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a project from an existing config
|
|
|
|
func remove(cmd *cobra.Command, args []string) (err error) {
|
2023-03-06 07:39:01 +00:00
|
|
|
h := haunt.NewHaunt()
|
2023-03-04 22:22:03 +00:00
|
|
|
// read a config if exist
|
2023-03-06 07:39:01 +00:00
|
|
|
err = h.Settings.Read(&h)
|
2023-03-04 22:22:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
2023-03-06 07:39:01 +00:00
|
|
|
err = h.Remove(arg)
|
2023-03-04 22:22:03 +00:00
|
|
|
if err != nil {
|
2023-03-06 07:39:01 +00:00
|
|
|
log.Println(h.Prefix(haunt.Red.Bold(arg + " project not found")))
|
2023-03-04 22:22:03 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-03-06 07:39:01 +00:00
|
|
|
log.Println(h.Prefix(haunt.Green.Bold(arg + " successfully removed")))
|
2023-03-04 22:22:03 +00:00
|
|
|
}
|
|
|
|
// update config
|
2023-03-06 07:39:01 +00:00
|
|
|
err = h.Settings.Write(h)
|
2023-03-04 22:22:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|