lock

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package lock provides utilities for managing the commands.lock file that tracks installed commands, their versions, and installation details.

Index

Examples

Constants

View Source
const (
	// LockFileName is the name of the lock file
	LockFileName = "commands.lock"
	// LockFileVersion is the current version of the lock file format
	LockFileVersion = "1.0"
)

Variables

View Source
var (
	// ErrNotLoaded is returned when operations are attempted before loading the lock file
	ErrNotLoaded = errors.New("lock file not loaded")

	// ErrCommandNotFound is returned when a command is not found in the lock file
	ErrCommandNotFound = errors.New("command not found")

	// ErrCommandExists is returned when trying to add a command that already exists
	ErrCommandExists = errors.New("command already exists")
)

Common errors

Functions

This section is empty.

Types

type CommandSorter

type CommandSorter struct {
	// contains filtered or unexported fields
}

CommandSorter provides different sorting methods for commands

func (CommandSorter) ByInstallDate

func (s CommandSorter) ByInstallDate()

ByInstallDate sorts commands by installation date (newest first)

func (CommandSorter) ByName

func (s CommandSorter) ByName()

ByName sorts commands alphabetically by name

func (CommandSorter) ByUpdateDate

func (s CommandSorter) ByUpdateDate()

ByUpdateDate sorts commands by update date (newest first)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles operations on the commands.lock file

Example
package main

import (
	"fmt"
	"log"

	"github.com/gifflet/ccmd/internal/lock"
	"github.com/gifflet/ccmd/internal/models"
)

func main() {
	// Create a new lock manager
	manager := lock.NewManager("/home/user/.claude/commands")

	// Load existing lock file or create new one
	if err := manager.Load(); err != nil {
		log.Fatal(err)
	}

	// Add a new command
	cmd := &models.Command{
		Name:    "git-helper",
		Version: "1.2.0",
		Source:  "github.com/example/git-helper",
		Metadata: map[string]string{
			"author":  "Example Author",
			"license": "MIT",
		},
	}

	if err := manager.AddCommand(cmd); err != nil {
		log.Fatal(err)
	}

	// Save changes to disk
	if err := manager.Save(); err != nil {
		log.Fatal(err)
	}

	// List all installed commands
	commands, err := manager.ListCommands()
	if err != nil {
		log.Fatal(err)
	}

	for _, cmd := range commands {
		fmt.Printf("Command: %s v%s\n", cmd.Name, cmd.Version)
	}
}

func NewManager

func NewManager(dir string) *Manager

NewManager creates a new lock file manager

func NewManagerWithFS

func NewManagerWithFS(dir string, fileSystem fs.FileSystem) *Manager

NewManagerWithFS creates a new lock file manager with a custom file system

func (*Manager) AddCommand

func (m *Manager) AddCommand(cmd *models.Command) error

AddCommand adds a new command to the lock file

func (*Manager) CountCommands

func (m *Manager) CountCommands() (int, error)

CountCommands returns the total number of installed commands

func (*Manager) ExportCommands

func (m *Manager) ExportCommands() (map[string]*models.Command, error)

ExportCommands exports all commands as a slice for backup or transfer

func (*Manager) GetCommand

func (m *Manager) GetCommand(name string) (*models.Command, error)

GetCommand retrieves a command from the lock file

func (*Manager) GetCommandsBySource

func (m *Manager) GetCommandsBySource(source string) ([]*models.Command, error)

GetCommandsBySource returns all commands from a specific source

func (*Manager) HasCommand

func (m *Manager) HasCommand(name string) bool

HasCommand checks if a command exists in the lock file

func (*Manager) ImportCommands

func (m *Manager) ImportCommands(commands map[string]*models.Command, overwrite bool) error

ImportCommands imports commands from a backup or transfer

func (*Manager) ListCommands

func (m *Manager) ListCommands() ([]*models.Command, error)

ListCommands returns a list of all commands in the lock file

func (*Manager) ListCommandsSorted

func (m *Manager) ListCommandsSorted(sortBy string) ([]*models.Command, error)

ListCommandsSorted returns a sorted list of commands

func (*Manager) Load

func (m *Manager) Load() error

Load reads the lock file from disk

func (*Manager) RemoveCommand

func (m *Manager) RemoveCommand(name string) error

RemoveCommand removes a command from the lock file

func (*Manager) Save

func (m *Manager) Save() error

Save writes the lock file to disk atomically

func (*Manager) SearchCommands

func (m *Manager) SearchCommands(query string) ([]*models.Command, error)

SearchCommands returns commands matching the search query

func (*Manager) UpdateCommand

func (m *Manager) UpdateCommand(name string, updateFn func(*models.Command) error) error

UpdateCommand updates an existing command in the lock file

Example
package main

import (
	"log"

	"github.com/gifflet/ccmd/internal/lock"
	"github.com/gifflet/ccmd/internal/models"
)

func main() {
	manager := lock.NewManager("/home/user/.claude/commands")

	if err := manager.Load(); err != nil {
		log.Fatal(err)
	}

	// Update a command's version
	err := manager.UpdateCommand("git-helper", func(cmd *models.Command) error {
		cmd.Version = "1.3.0"
		cmd.Metadata["updated"] = "true"
		return nil
	})

	if err != nil {
		log.Fatal(err)
	}

	// Save changes
	if err := manager.Save(); err != nil {
		log.Fatal(err)
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL