metadata

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information. Package metadata provides functionality for managing command metadata files.

The package implements reading, writing, and updating ccmd.yaml files that contain command metadata information such as name, version, description, author, and repository.

Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information.

Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information.

Example (PackageLevelFunctions)
package main

import (
	"fmt"
	"log"

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

func main() {
	commandDir := "/path/to/command"

	// Check if metadata exists
	if metadata.Exists(commandDir) {
		// Read metadata
		meta, err := metadata.ReadCommandMetadata(commandDir)
		if err != nil {
			log.Fatal(err)
		}

		// Extract command info
		info := metadata.ExtractCommandInfo(meta)
		fmt.Printf("Command info: %v\n", info)

		// Update metadata
		err = metadata.UpdateCommandMetadata(commandDir, func(m *models.CommandMetadata) error {
			m.Version = "1.1.0"
			return nil
		})
		if err != nil {
			log.Fatal(err)
		}
	} else {
		// Create new metadata
		meta := &models.CommandMetadata{
			Name:        "new-command",
			Version:     "1.0.0",
			Description: "A new command",
			Author:      "Author Name",
			Repository:  "https://github.com/author/new-command",
			Entry:       "new-command",
		}

		if err := metadata.WriteCommandMetadata(commandDir, meta); err != nil {
			log.Fatal(err)
		}
	}
}

Index

Examples

Constants

View Source
const (
	// MetadataFileName is the standard name for command metadata files
	MetadataFileName = "ccmd.yaml"
)

Variables

This section is empty.

Functions

func Exists

func Exists(commandDir string) bool

Exists checks if a metadata file exists in the specified directory

func ExtractCommandInfo

func ExtractCommandInfo(metadata *models.CommandMetadata) map[string]interface{}

ExtractCommandInfo extracts basic command information from metadata

func ReadCommandMetadata

func ReadCommandMetadata(commandDir string) (*models.CommandMetadata, error)

ReadCommandMetadata reads and parses a command metadata file from the specified directory

func UpdateCommandMetadata

func UpdateCommandMetadata(commandDir string, updates func(*models.CommandMetadata) error) error

UpdateCommandMetadata reads existing metadata, applies updates, and writes it back

func WriteCommandMetadata

func WriteCommandMetadata(commandDir string, metadata *models.CommandMetadata) error

WriteCommandMetadata writes command metadata to a file in the specified directory

Types

type Manager

type Manager struct{}

Manager provides methods for reading and writing command metadata

func NewManager

func NewManager() *Manager

NewManager creates a new metadata manager

func (*Manager) Exists

func (m *Manager) Exists(commandDir string) bool

Exists checks if a metadata file exists in the specified directory

Example
package main

import (
	"fmt"
	"path/filepath"

	"github.com/gifflet/ccmd/internal/metadata"
)

func main() {
	manager := metadata.NewManager()

	commandDirs := []string{
		"/path/to/command1",
		"/path/to/command2",
		"/path/to/command3",
	}

	for _, dir := range commandDirs {
		if manager.Exists(dir) {
			fmt.Printf("Metadata found in %s\n", filepath.Base(dir))
		} else {
			fmt.Printf("No metadata in %s\n", filepath.Base(dir))
		}
	}
}

func (*Manager) ExtractCommandInfo

func (m *Manager) ExtractCommandInfo(metadata *models.CommandMetadata) map[string]interface{}

ExtractCommandInfo extracts basic command information from metadata

func (*Manager) ReadCommandMetadata

func (m *Manager) ReadCommandMetadata(commandDir string) (*models.CommandMetadata, error)

ReadCommandMetadata reads and parses a command metadata file from the specified directory

Example
package main

import (
	"fmt"
	"log"

	"github.com/gifflet/ccmd/internal/metadata"
)

func main() {
	manager := metadata.NewManager()

	// Read metadata from a command directory
	commandDir := "/path/to/command"
	meta, err := manager.ReadCommandMetadata(commandDir)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Command: %s v%s\n", meta.Name, meta.Version)
	fmt.Printf("Author: %s\n", meta.Author)
	fmt.Printf("Description: %s\n", meta.Description)
}

func (*Manager) UpdateCommandMetadata

func (m *Manager) UpdateCommandMetadata(commandDir string, updates func(*models.CommandMetadata) error) error

UpdateCommandMetadata reads existing metadata, applies updates, and writes it back

Example
package main

import (
	"log"

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

func main() {
	manager := metadata.NewManager()
	commandDir := "/path/to/command"

	// Update existing metadata
	err := manager.UpdateCommandMetadata(commandDir, func(meta *models.CommandMetadata) error {
		meta.Version = "2.0.0"
		meta.Tags = append(meta.Tags, "updated")
		return nil
	})

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

func (*Manager) WriteCommandMetadata

func (m *Manager) WriteCommandMetadata(commandDir string, metadata *models.CommandMetadata) error

WriteCommandMetadata writes command metadata to a file in the specified directory

Example
package main

import (
	"log"

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

func main() {
	manager := metadata.NewManager()

	// Create new metadata
	meta := &models.CommandMetadata{
		Name:        "my-command",
		Version:     "1.0.0",
		Description: "My awesome command",
		Author:      "John Doe",
		Repository:  "https://github.com/johndoe/my-command",
		Entry:       "main.go",
		Tags:        []string{"cli", "tool"},
		License:     "MIT",
	}

	// Write to command directory
	commandDir := "/path/to/command"
	if err := manager.WriteCommandMetadata(commandDir, meta); err != nil {
		log.Fatal(err)
	}
}

Jump to

Keyboard shortcuts

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