NewAddCommand function

NewAddCommand creates a new 'add' command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	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
}

runBeforeAdd function

Parameters:

- cmd: A pointer to the command.Command that is being executed.

- args: A slice of strings representing the command-line arguments.

Parameters:

  • cmd *command.Command
  • rootFlags *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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
}

runAfterAdd function

Parameters:

- cmd: A pointer to the command.Command that is being executed.

- args: A slice of strings representing the command-line arguments.

Parameters:

  • cmd *command.Command
  • rootFlags *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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 function

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.

Parameters:

  • cmd *command.Command
  • rootFlags *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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 function

NewListCommand creates a new 'list' command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	cmd := &command.Command{
		Name:        "list",
		Usage:       "list",
		Description: "List all items",
		Run:         runList,
	}
	return cmd
}

runList function

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.

Parameters:

  • cmd *command.Command
  • rootFlags *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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 function

NewRemoveCommand creates a new 'remove' command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	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 function

NewDeleteAllCommand creates a new 'delete all' command.

Returns:

  • *command.Command
Show/Hide Function Body
{
	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 function

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.

Parameters:

  • cmd *command.Command
  • rootFlags *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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 function

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.

Parameters:

  • cmd *command.Command
  • rootFlags *command.RootFlags
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	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
}

fmt import

Import example:

import "fmt"

github.com/mirkobrombin/go-cli-builder/examples/v1/core import

Import example:

import "github.com/mirkobrombin/go-cli-builder/examples/v1/core"

github.com/mirkobrombin/go-cli-builder/v1/command import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/command"

fmt import

Import example:

import "fmt"

strings import

Import example:

import "strings"

github.com/mirkobrombin/go-cli-builder/examples/v1/core import

Import example:

import "github.com/mirkobrombin/go-cli-builder/examples/v1/core"

github.com/mirkobrombin/go-cli-builder/v1/command import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/command"

fmt import

Import example:

import "fmt"

github.com/mirkobrombin/go-cli-builder/examples/v1/core import

Import example:

import "github.com/mirkobrombin/go-cli-builder/examples/v1/core"

github.com/mirkobrombin/go-cli-builder/v1/command import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/command"