NewAddCommand creates a new 'add' command.
{
cmd := &command.Command{
Name: "add",
Usage: "add <item>",
Description: "Add a new item to the list",
BeforeRun: runBeforeAdd,
AfterRun: runAfterAdd,
Run: runAdd,
}
cmd.AddFlag("name", "n", "Name of the item to add", "", true, true)
return cmd
}
Parameters:
- cmd: A pointer to the command.Command that is being executed.
- args: A slice of strings representing the command-line arguments.
{
data, err := core.LoadData()
if err != nil {
if rootFlags.GetBool("verbose") {
cmd.Logger.Error("Error loading data")
}
return err
}
if len(data.Items) >= 3 {
cmd.Logger.Info("Last 3 items before addition: %s", data.Items[len(data.Items)-3:])
}
return nil
}
Parameters:
- cmd: A pointer to the command.Command that is being executed.
- args: A slice of strings representing the command-line arguments.
{
data, err := core.LoadData()
if err != nil {
if rootFlags.GetBool("verbose") {
cmd.Logger.Error("Error loading data")
}
return err
}
if len(data.Items) >= 3 {
cmd.Logger.Info("Last 3 items after addition: %s", data.Items[len(data.Items)-3:])
}
return nil
}
runAdd executes the 'add' command logic.
It loads data from the JSON file, appends the new item, and saves the updated data.
Parameters:
- cmd: A pointer to the command.Command that is being executed.
- args: A slice of strings representing the command-line arguments.
{
if rootFlags.GetBool("verbose") {
cmd.Logger.Info("You see this because the 'verbose' flag is set in the root command")
}
data, err := core.LoadData()
if err != nil {
cmd.Logger.Error("Error loading data")
return err
}
name := cmd.GetFlagString("name")
if name == "" {
cmd.Logger.Error("Missing item name")
return fmt.Errorf("missing item name")
}
data.Items = append(data.Items, name)
if err := core.SaveData(data); err != nil {
cmd.Logger.Error("Error adding item: %s", name)
return err
}
cmd.Logger.Success(fmt.Sprintf("Added item: %s", name))
return nil
}
NewListCommand creates a new 'list' command.
{
cmd := &command.Command{
Name: "list",
Usage: "list",
Description: "List all items",
Run: runList,
}
return cmd
}
runList executes the 'list' command logic.
It loads data from the JSON file and prints all items.
Parameters:
- cmd: A pointer to the command.Command that is being executed.
- args: A slice of strings representing the command-line arguments.
{
if rootFlags.GetBool("verbose") {
cmd.Logger.Info("You see this because the 'verbose' flag is set in the root command")
}
data, err := core.LoadData()
if err != nil {
if rootFlags.GetBool("verbose") {
cmd.Logger.Error("Error loading data")
}
return err
}
if len(data.Items) == 0 {
cmd.Logger.Info("No items found")
return nil
}
var sb strings.Builder
sb.WriteString("Items:\n")
for _, item := range data.Items {
sb.WriteString(fmt.Sprintf("· %s\n", item))
}
cmd.Logger.Success(sb.String())
return nil
}
NewRemoveCommand creates a new 'remove' command.
{
cmd := &command.Command{
Name: "remove",
Usage: "remove <item>",
Description: "Remove an item from the list",
Run: runRemove,
}
cmd.AddFlag("name", "n", "Name of the item to add", "", true, false)
cmd.AddCommand(newDeleteAllCommand())
return cmd
}
NewDeleteAllCommand creates a new 'delete all' command.
{
cmd := &command.Command{
Name: "all",
Usage: "all",
Description: "Delete all items from the list",
Run: runDeleteAll,
}
cmd.AddBoolFlag("confirm", "c", "Confirm deletion", false, false, true)
return cmd
}
runDeleteAll executes the 'delete all' command logic.
It loads data from the JSON file, removes all items, and saves the updated data.
Parameters:
- cmd: A pointer to the command.Command that is being executed.
- rootFlags: A pointer to the command.RootFlags containing root-level flags.
- args: A slice of strings representing the command-line arguments.
{
data, err := core.LoadData()
if err != nil {
cmd.Logger.Error("Error loading data")
return err
}
data.Items = []string{}
if err := core.SaveData(data); err != nil {
cmd.Logger.Error("Error deleting all items")
return err
}
cmd.Logger.Success("All items deleted")
return nil
}
runRemove executes the 'remove' command logic.
It loads data from the JSON file, removes the specified item, and saves the updated data.
Parameters:
- cmd: A pointer to the command.Command that is being executed.
- args: A slice of strings representing the command-line arguments.
{
if rootFlags.GetBool("verbose") {
cmd.Logger.Info("You see this because the 'verbose' flag is set in the root command")
}
name := cmd.GetFlagString("name")
if name == "" {
cmd.Logger.Error("Missing item name")
return fmt.Errorf("missing item name")
}
data, err := core.LoadData()
if err != nil {
cmd.Logger.Error("Error loading data")
return err
}
var updatedItems []string
for _, existingItem := range data.Items {
if existingItem != name {
updatedItems = append(updatedItems, existingItem)
}
}
data.Items = updatedItems
if err := core.SaveData(data); err != nil {
cmd.Logger.Error("Error removing item: %s", name)
return err
}
cmd.Logger.Success(fmt.Sprintf("Removed item: %s", name))
return nil
}
import "fmt"
import "github.com/mirkobrombin/go-cli-builder/examples/v1/core"
import "github.com/mirkobrombin/go-cli-builder/v1/command"
import "fmt"
import "strings"
import "github.com/mirkobrombin/go-cli-builder/examples/v1/core"
import "github.com/mirkobrombin/go-cli-builder/v1/command"
import "fmt"
import "github.com/mirkobrombin/go-cli-builder/examples/v1/core"
import "github.com/mirkobrombin/go-cli-builder/v1/command"