config

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package config provides multi-level settings management for GenCode. Settings are loaded from multiple sources with the following priority (lowest to highest):

  1. ~/.claude/settings.json (Claude user level - compatibility)
  2. ~/.gen/settings.json (Gen user level)
  3. .claude/settings.json (Claude project level - compatibility)
  4. .gen/settings.json (Gen project level)
  5. .claude/settings.local.json (Claude local level - compatibility)
  6. .gen/settings.local.json (Gen local level)
  7. Environment variables / CLI arguments
  8. managed-settings.json (system level - cannot be overridden)

Index

Constants

This section is empty.

Variables

View Source
var CommonAllowPatterns = []string{
	"Bash(git:*)",
	"Bash(npm:*)",
	"Bash(yarn:*)",
	"Bash(pnpm:*)",
	"Bash(go:*)",
	"Bash(make:*)",
	"Bash(ls:*)",
	"Bash(cat:*)",
	"Bash(head:*)",
	"Bash(tail:*)",
	"Bash(pwd)",
}

CommonAllowPatterns contains commonly allowed patterns.

View Source
var CommonDenyPatterns = []string{
	"Read(**/.env)",
	"Read(**/.env.*)",
	"Read(**/secrets/**)",
	"Read(**/*credentials*)",
	"Read(**/*password*)",
	"Read(**/.aws/**)",
	"Read(**/.ssh/**)",
	"Edit(**/.env)",
	"Edit(**/.env.*)",
	"Write(**/.env)",
	"Write(**/.env.*)",
}

CommonDenyPatterns contains commonly denied patterns for security.

View Source
var DestructiveCommands = []string{
	"rm:-rf",
	"rm:-fr",
	"rm:-r",
	"git:reset --hard",
	"git:clean -fd",
	"git:clean -f",
	"git:push --force",
	"git:push -f",
	"chmod:777",
	"chmod:-R 777",
	":(){ :|:& };:",
	"> /dev/",
	"dd:if=",
	"mkfs",
	"fdisk",
}

DestructiveCommands are patterns that should always require user confirmation, even when session permissions like AllowAllBash are enabled. These commands can cause irreversible data loss or system damage.

View Source
var ReadOnlyTools = map[string]bool{
	"Read":      true,
	"Glob":      true,
	"Grep":      true,
	"WebFetch":  true,
	"WebSearch": true,
}

ReadOnlyTools is a list of tools that are considered read-only. These tools don't modify any files or state.

Functions

func BuildRule

func BuildRule(toolName string, args map[string]any) string

BuildRule builds a rule string from a tool name and arguments. Format: "Tool(args)"

Different tools extract different parts of args:

  • Bash: "Bash(command)" where command is the shell command
  • Read/Edit/Write: "Read(file_path)"
  • Glob/Grep: "Glob(pattern)" or "Grep(pattern)"
  • WebFetch: "WebFetch(domain:hostname)"

func GetDisabledTools added in v1.1.0

func GetDisabledTools() map[string]bool

GetDisabledTools returns the disabled tools from loaded settings.

func GetDisabledToolsAt added in v1.1.0

func GetDisabledToolsAt(userLevel bool) map[string]bool

GetDisabledToolsAt returns the disabled tools from a specific level (not merged). If userLevel is true, loads from ~/.gen/settings.json, otherwise from .gen/settings.json.

func IsDestructiveCommand added in v1.1.0

func IsDestructiveCommand(cmd string) bool

IsDestructiveCommand checks if a bash command matches any destructive pattern. Returns true if the command should always require user confirmation.

func IsReadOnlyTool

func IsReadOnlyTool(toolName string) bool

IsReadOnlyTool returns true if the tool is read-only.

func MatchRule

func MatchRule(rule, pattern string) bool

MatchRule checks if a rule matches a pattern. Rule format: "Tool(args)" Pattern format: "Tool(pattern)" where pattern supports:

  • "*" matches any sequence of characters
  • "**" matches any sequence including path separators
  • "domain:" prefix for WebFetch domain matching

func UpdateDisabledTools added in v1.1.0

func UpdateDisabledTools(disabledTools map[string]bool) error

UpdateDisabledTools updates the disabled tools in project-level settings. It only saves the DisabledTools field, preserving other settings.

func UpdateDisabledToolsAt added in v1.1.0

func UpdateDisabledToolsAt(disabledTools map[string]bool, userLevel bool) error

UpdateDisabledToolsAt updates the disabled tools at the specified level. If userLevel is true, saves to ~/.gen/settings.json, otherwise to .gen/settings.json.

Types

type Hook

type Hook struct {
	// Matcher is a pattern to match against the event
	Matcher string `json:"matcher,omitempty"`

	// Hooks are the commands to execute when matched
	Hooks []HookCmd `json:"hooks,omitempty"`
}

Hook defines an event hook configuration

type HookCmd

type HookCmd struct {
	// Type is the hook type (e.g., "command", "prompt", "agent")
	// Currently only "command" is supported.
	Type string `json:"type"`

	// Command is the shell command to execute (for type=command)
	Command string `json:"command,omitempty"`

	// Prompt is the LLM prompt (for type=prompt/agent, not yet supported)
	Prompt string `json:"prompt,omitempty"`

	// Model specifies the model for prompt/agent hooks
	Model string `json:"model,omitempty"`

	// Async runs the hook asynchronously (fire-and-forget)
	Async bool `json:"async,omitempty"`

	// Timeout in seconds (default: 600)
	Timeout int `json:"timeout,omitempty"`

	// StatusMessage is displayed while the hook executes
	StatusMessage string `json:"statusMessage,omitempty"`

	// Once ensures the hook only runs once per session
	Once bool `json:"once,omitempty"`
}

HookCmd defines a single hook command

type Loader

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

Loader handles loading and merging settings from multiple sources.

func NewLoader

func NewLoader() *Loader

NewLoader creates a new settings loader. It defaults to:

  • userDir: ~/.gen
  • projectDir: .gen
  • claudeCompat: true (also loads from .claude directories)

func NewLoaderWithOptions

func NewLoaderWithOptions(userDir, projectDir string, claudeCompat bool) *Loader

NewLoaderWithOptions creates a loader with custom options.

func (*Loader) EnsureProjectDir

func (l *Loader) EnsureProjectDir() error

EnsureProjectDir creates the project config directory if it doesn't exist.

func (*Loader) EnsureUserDir

func (l *Loader) EnsureUserDir() error

EnsureUserDir creates the user config directory if it doesn't exist.

func (*Loader) GetProjectDir

func (l *Loader) GetProjectDir() string

GetProjectDir returns the project config directory path.

func (*Loader) GetUserDir

func (l *Loader) GetUserDir() string

GetUserDir returns the user config directory path.

func (*Loader) Load

func (l *Loader) Load() (*Settings, error)

Load loads and merges settings from all sources. Priority (lowest to highest):

  1. ~/.claude/settings.json (Claude user level)
  2. ~/.gen/settings.json (Gen user level)
  3. .claude/settings.json (Claude project level)
  4. .gen/settings.json (Gen project level)
  5. .claude/settings.local.json (Claude local level)
  6. .gen/settings.local.json (Gen local level)

Later sources override earlier ones.

func (*Loader) LoadFile

func (l *Loader) LoadFile(path string) (*Settings, error)

LoadFile loads settings from a specific file.

func (*Loader) SaveToProject added in v1.1.0

func (l *Loader) SaveToProject(settings *Settings) error

SaveToProject saves settings to the project-level settings file. It merges with existing settings if the file exists.

func (*Loader) SaveToUser added in v1.1.0

func (l *Loader) SaveToUser(settings *Settings) error

SaveToUser saves settings to the user-level settings file. It merges with existing settings if the file exists.

type OperationMode added in v1.1.0

type OperationMode int

OperationMode defines the current operation mode

const (
	ModeNormal     OperationMode = iota // Normal: permission prompts enabled
	ModeAutoAccept                      // Auto-accept: auto-approve edits/writes
	ModePlan                            // Plan: read-only tools only
)

func (OperationMode) Next added in v1.1.0

func (m OperationMode) Next() OperationMode

Next returns the next mode in the cycle

func (OperationMode) String added in v1.1.0

func (m OperationMode) String() string

String returns the display name for the mode

type PermissionResult

type PermissionResult int

PermissionResult represents the result of a permission check.

const (
	// PermissionAllow means the action is automatically allowed.
	PermissionAllow PermissionResult = iota

	// PermissionDeny means the action is automatically denied.
	PermissionDeny

	// PermissionAsk means the action requires user confirmation.
	PermissionAsk
)

func (PermissionResult) String

func (p PermissionResult) String() string

String returns a human-readable representation of the permission result.

type PermissionSettings

type PermissionSettings struct {
	// Allow contains patterns that are automatically allowed
	Allow []string `json:"allow,omitempty"`

	// Deny contains patterns that are automatically denied
	Deny []string `json:"deny,omitempty"`

	// Ask contains patterns that require user confirmation
	Ask []string `json:"ask,omitempty"`
}

PermissionSettings defines permission rules for tool execution. Rules use the format "Tool(pattern)" where pattern uses glob-like syntax.

Example rules:

  • "Bash(npm:*)" - Match npm commands
  • "Read(**/.env)" - Match .env files in any directory
  • "Edit(/path/**)" - Match files under /path
  • "WebFetch(domain:github.com)" - Match specific domain

type SessionPermissions

type SessionPermissions struct {
	// AllowAllEdits allows all edit operations without prompting
	AllowAllEdits bool

	// AllowAllWrites allows all write operations without prompting
	AllowAllWrites bool

	// AllowAllBash allows all bash commands without prompting
	AllowAllBash bool

	// AllowAllSkills allows all skill invocations without prompting
	AllowAllSkills bool

	// AllowAllTasks allows all task/agent spawning without prompting
	AllowAllTasks bool

	// AllowedTools contains tools that have been allowed for this session
	AllowedTools map[string]bool

	// AllowedPatterns contains specific patterns that have been allowed
	AllowedPatterns map[string]bool
}

SessionPermissions tracks runtime permission state for the current session. This allows "allow all" type responses to persist during a session.

func NewSessionPermissions

func NewSessionPermissions() *SessionPermissions

NewSessionPermissions creates a new session permissions instance

func (*SessionPermissions) AllowPattern

func (sp *SessionPermissions) AllowPattern(pattern string)

AllowPattern marks a specific pattern as allowed for this session

func (*SessionPermissions) AllowTool

func (sp *SessionPermissions) AllowTool(toolName string)

AllowTool marks a tool as allowed for this session

func (*SessionPermissions) IsPatternAllowed

func (sp *SessionPermissions) IsPatternAllowed(pattern string) bool

IsPatternAllowed checks if a specific pattern is allowed for this session

func (*SessionPermissions) IsToolAllowed

func (sp *SessionPermissions) IsToolAllowed(toolName string) bool

IsToolAllowed checks if a tool is allowed for this session

type Settings

type Settings struct {
	// Permissions defines permission rules for tools
	Permissions PermissionSettings `json:"permissions,omitempty"`

	// Model is the default model to use (e.g., "claude-sonnet-4-20250514")
	Model string `json:"model,omitempty"`

	// Hooks defines event hooks (e.g., PreToolUse, PostToolUse)
	Hooks map[string][]Hook `json:"hooks,omitempty"`

	// Env defines environment variables to set
	Env map[string]string `json:"env,omitempty"`

	// EnabledPlugins defines which plugins are enabled
	EnabledPlugins map[string]bool `json:"enabledPlugins,omitempty"`

	// DisabledTools defines which tools are disabled
	// Key is tool name, value true means disabled
	// Project-level settings can override user-level by setting to false
	DisabledTools map[string]bool `json:"disabledTools,omitempty"`
}

Settings represents the complete GenCode configuration. Compatible with Claude Code settings format.

func Default

func Default() *Settings

Default returns the default settings without loading from files.

func Load

func Load() (*Settings, error)

Load is a convenience function that loads settings using the default loader.

func MergeSettings

func MergeSettings(base, overlay *Settings) *Settings

MergeSettings merges two Settings objects. Values from 'overlay' override values in 'base'. For slices (like permission rules), overlay values replace base values. For maps, overlay values are merged with base values.

func NewSettings

func NewSettings() *Settings

NewSettings creates a new Settings instance with default values

func Reload

func Reload() (*Settings, error)

Reload forces reloading of settings, clearing the cache.

func (*Settings) CheckPermission

func (s *Settings) CheckPermission(toolName string, args map[string]any, session *SessionPermissions) PermissionResult

CheckPermission checks if a tool action is allowed based on settings and session permissions. Priority:

  1. Deny rules (highest priority - cannot be bypassed by session permissions)
  2. Destructive command protection (always ask for dangerous bash commands)
  3. Session permissions (runtime, e.g., "allow all edits this session")
  4. Allow rules
  5. Ask rules
  6. Default behavior (read-only tools allowed, others need confirmation)

Jump to

Keyboard shortcuts

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