codescout

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2025 License: MIT Imports: 7 Imported by: 0

README

CodeScout Logo

CodeScout is a Go package and CLI tool for analyzing and extracting structured representations of functions, methods, and structs from Go source files.


📁 Package Overview

The core library is available under pkg/codescout. It provides types and functions for inspecting Go source code.

✍️ Exported Functions

ScoutFunction(path string, config FuncConfig) (*FuncNode, error)

Returns the first function matching the configuration in the provided file path.

ScoutFunctions(path string, config FuncConfig) ([]*FuncNode, error)

Returns all matching functions based on the configuration.

ScoutStruct(path string, config StructConfig) (*StructNode, error)

Returns the first struct matching the configuration in the given file path.

ScoutStructs(path string, config StructConfig) ([]*StructNode, error)

Returns all structs matching the provided configuration.

ScoutMethod(path string, config MethodConfig) (*MethodNode, error)

Returns the first method matching the configuration.

ScoutMethods(path string, config MethodConfig) ([]*MethodNode, error)

Returns all methods that match the given configuration.

⚖️ Configuration Types

FuncConfig

Defines filters for scouting functions including:

  • Name
  • Parameter and return types
  • Match options: Exact, NoParams, NoReturn
MethodConfig

Used to find specific methods, with support for:

  • Receiver type
  • Pointer receiver flag
  • Accessed fields and called methods
  • Match options: Exact, NoParams, NoReturn, NoFields, NoMethods
StructConfig

Defines search criteria for structs:

  • Field name and type matches
  • Exact and NoFields options

⚖️ CLI Usage

CodeScout also offers a full-featured CLI using Cobra.

🔢 Function Command

codescout func [path] [flags]
  • --name, -n: Function name
  • --params, -p: Function parameters
  • --return, -r: Return types
  • --no-params, -s: Expect no parameters
  • --no-return, -u: Expect no return values
  • --exact, -x: Match criteria exactly
  • --output, -o: Output format (definition, body, signature, etc.)

🎓 Method Command

codescout method [path] [flags]
  • --name, -n: Method name
  • --receiver, -m: Receiver type
  • --pointer, -t: Whether it's a pointer receiver
  • --fields, -f: Fields accessed
  • --methods, -c: Methods called
  • --no-fields, -d: Must not access struct fields
  • --no-methods, -e: Must not call struct methods
  • All function flags also apply

💼 Struct Command

codescout struct [path] [flags]
  • --name, -n: Struct name
  • --fields, -f: Fields to match
  • --no-fields, -s: Struct must have no fields
  • --exact, -x: Match fields exactly
  • --output, -o: Output format (definition, body, etc.)

💡 Verbose Output

All commands support the --verbose, -v flag to list all matches instead of just the first.


📅 Example

codescout func ./example.go -name=SomeFunc -params=input:string -return=error -output=signature

🚀 Getting Started

💼 Use as a Package

go get github.com/galactixx/codescout@latest

Then import in your Go code:

import "github.com/galactixx/codescout"

🛠️ Install & Use the CLI

go install github.com/galactixx/codescout/cmd/codescout@latest

Then run:

codescout

🔮 Future Features

  • Support for scouting interfaces and their methods.
  • Ability to search for structs that implement specific interfaces via MethodConfig matching.
  • Color-coded Go syntax highlighting in CLI output.
  • Smart path resolution and recursive file scanning.
  • Integration with gopls for enhanced analysis.

🤝 License

This project is licensed under the MIT License. See the LICENSE file for more details.


📞 Contact

If you have any questions or need support, feel free to reach out by opening an issue on the GitHub repository.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseNode

type BaseNode struct {
	// Name of the code element (e.g., function or struct name)
	Name string
	// Path to the file where the element is defined
	Path string
	// Line number where the element starts
	Line int
	// Number of characters from the start of the file to the element
	Characters int
	// Whether the element is exported (starts with uppercase letter)
	Exported bool
	// Leading comment associated with the element
	Comment string
}

BaseNode contains shared metadata about code elements like structs, methods, and functions.

type CallableOps

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

CallableOps contains logic for extracting code and metadata from AST function declarations.

func (CallableOps) Body

func (c CallableOps) Body() string

Body returns the string representation of the function body.

func (CallableOps) Code

func (c CallableOps) Code() string

Code returns the full source code of the function, optionally including comments.

func (CallableOps) Comments

func (c CallableOps) Comments() string

Comments returns the associated documentation comments for the function.

func (CallableOps) Parameters

func (c CallableOps) Parameters() []NamedType

Parameters returns a slice of parameter names and types for the function.

func (CallableOps) PrintBody

func (c CallableOps) PrintBody()

PrintBody prints the body of the function.

func (CallableOps) PrintReturnType

func (c CallableOps) PrintReturnType()

PrintReturnType prints the function's return type(s).

func (CallableOps) PrintSignature

func (c CallableOps) PrintSignature()

PrintSignature prints the function's signature (name + parameters).

func (CallableOps) ReturnType

func (c CallableOps) ReturnType() string

ReturnType returns a string representing the function's return type(s).

func (CallableOps) ReturnTypes

func (c CallableOps) ReturnTypes() []string

ReturnTypes returns a slice of string representations of all return types.

func (CallableOps) Signature

func (c CallableOps) Signature() string

Signature returns the function's name and parameter list.

type FuncConfig

type FuncConfig struct {
	// Name of the function.
	Name string
	// Expected parameter types (a subset unless exact is specified).
	ParamTypes []NamedType
	// Expected return types (a subset unless exact is specified).
	ReturnTypes []string
	// If true, function should have no parameters.
	NoParams *bool
	// If true, function should have no return values.
	NoReturn *bool
	// If true, all criteria slices must match exactly.
	Exact bool
}

FuncConfig holds configuration for scouting a function in source code.

type FuncNode

type FuncNode struct {
	// Node contains metadata such as name, path, line number, etc.
	Node BaseNode
	// CallableOps provides operations and data tied to the function's AST node
	CallableOps CallableOps
}

FuncNode represents a top-level function with its metadata and operations.

func ScoutFunction

func ScoutFunction(path string, config FuncConfig) (*FuncNode, error)

ScoutFunction returns the first function in the given path matching the config.

func ScoutFunctions

func ScoutFunctions(path string, config FuncConfig) ([]*FuncNode, error)

ScoutFunctions returns all functions in the given path matching the config.

func (FuncNode) Code

func (f FuncNode) Code() string

Code returns the full source code of the function.

func (FuncNode) Name

func (f FuncNode) Name() string

Name returns the function name.

func (FuncNode) PrintComments

func (f FuncNode) PrintComments()

PrintComments prints the function's associated comments.

func (FuncNode) PrintNode

func (f FuncNode) PrintNode()

PrintNode prints the function's code.

type MethodConfig

type MethodConfig struct {
	// Name of the method.
	Name string
	// Expected parameter types (a subset unless exact is specified).
	ParamTypes []NamedType
	// Expected return types (a subset unless exact is specified).
	ReturnTypes []string
	// Type of the receiver.
	Receiver string
	// If true, method must have pointer receiver.
	IsPointerRec *bool
	// Struct fields that must be accessed within method.
	Fields []string
	// Struct methods that must be called within method.
	Methods []string
	// If true, method should have no parameters.
	NoParams *bool
	// If true, method should have no return values.
	NoReturn *bool
	// If true, the method must not access any of the struct fields.
	NoFields *bool
	// If true, the method must not call any of the struct methods.
	NoMethods *bool
	// If true, all criteria slices must match exactly.
	Exact bool
}

MethodConfig holds configuration for scouting a method in source code.

type MethodNode

type MethodNode struct {
	// Node contains metadata such as name, path, line number, etc.
	Node BaseNode
	// CallableOps provides operations and data tied to the method's AST node
	CallableOps CallableOps
	// contains filtered or unexported fields
}

MethodNode represents a method with its associated metadata and interactions.

func ScoutMethod

func ScoutMethod(path string, config MethodConfig) (*MethodNode, error)

ScoutMethod returns the first method in the given path matching the config.

func ScoutMethods

func ScoutMethods(path string, config MethodConfig) ([]*MethodNode, error)

ScoutMethods returns all methods in the given path matching the config.

func (MethodNode) Code

func (m MethodNode) Code() string

Code returns the full source code of the method.

func (MethodNode) FieldsAccessed

func (m MethodNode) FieldsAccessed() []string

FieldsAccessed returns a slice of field names accessed by this method.

func (MethodNode) HasPointerReceiver

func (m MethodNode) HasPointerReceiver() bool

HasPointerReceiver checks whether the method has a pointer receiver.

func (MethodNode) MethodsCalled

func (m MethodNode) MethodsCalled() []string

MethodsCalled returns a slice of method names called by this method.

func (MethodNode) Name

func (m MethodNode) Name() string

Name returns the method name.

func (MethodNode) PrintComments

func (m MethodNode) PrintComments()

PrintComments prints the method's associated documentation comments.

func (MethodNode) PrintNode

func (m MethodNode) PrintNode()

PrintNode prints the method's code.

func (MethodNode) ReceiverName

func (m MethodNode) ReceiverName() string

ReceiverName returns the name of the receiver variable (e.g., "m" in "func (m *MyStruct) ...").

func (MethodNode) ReceiverType

func (m MethodNode) ReceiverType() string

ReceiverType returns the type name of the method's receiver.

type NamedType

type NamedType struct {
	Name string
	Type string
}

NamedType represents a named parameter or field with its associated type.

type NodeInfo

type NodeInfo interface {
	Code() string
	PrintNode()
	PrintComments()
	Name() string
}

NodeInfo provides a generic interface for inspecting code entities.

type StructConfig

type StructConfig struct {
	// Name of the struct.
	Name string
	// Expected field names and types.
	FieldTypes []NamedType
	// If true, struct should not have fields.
	NoFields *bool
	// If true, all criteria slices must match exactly.
	Exact bool
}

StructConfig holds configuration for scouting a struct type in source code.

type StructNode

type StructNode struct {
	// Node contains metadata such as name, path, line number, etc.
	Node BaseNode
	// Methods holds all methods associated with this struct
	Methods []*MethodNode
	// contains filtered or unexported fields
}

StructNode represents a Go struct declaration in the AST.

func ScoutStruct

func ScoutStruct(path string, config StructConfig) (*StructNode, error)

ScoutStruct returns the first struct in the given path matching the config.

func ScoutStructs

func ScoutStructs(path string, config StructConfig) ([]*StructNode, error)

ScoutStructs returns all structs in the given path matching the config.

func (StructNode) Body

func (s StructNode) Body() string

Body returns the string representation of the struct's fields only.

func (StructNode) Code

func (s StructNode) Code() string

Code returns the source code representation of the struct declaration.

func (StructNode) Comments

func (s StructNode) Comments() string

Comments returns documentation comments associated with the struct declaration.

func (StructNode) Fields

func (s StructNode) Fields() []NamedType

Fields extracts all named fields from the struct definition.

func (StructNode) Name

func (s StructNode) Name() string

Name returns the name of the struct.

func (StructNode) PrintComments

func (s StructNode) PrintComments()

PrintComments prints comments associated with the struct.

func (StructNode) PrintNode

func (s StructNode) PrintNode()

PrintNode prints the full code of the struct.

func (StructNode) Signature

func (s StructNode) Signature() string

Signature returns the struct name along with any generic type parameters.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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