mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
feat: initial release of skillshare
Share skills across AI CLI tools (Claude Code, Codex, Cursor, Gemini, OpenCode) with a single source directory and symlinks. Commands: - init: Initialize with source directory, auto-detect CLI tools - sync: Migrate existing skills and create symlinks - status: Show target status - target: Manage targets (add/remove/list)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.23"
|
||||
|
||||
- name: Run GoReleaser
|
||||
uses: goreleaser/goreleaser-action@v6
|
||||
with:
|
||||
version: latest
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -0,0 +1,25 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.23"
|
||||
|
||||
- name: Build
|
||||
run: go build -v ./...
|
||||
|
||||
- name: Test
|
||||
run: go test -v ./...
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
# Binary
|
||||
/skillshare
|
||||
/dist/
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# Test
|
||||
coverage.out
|
||||
.claude/
|
||||
@@ -0,0 +1,53 @@
|
||||
version: 2
|
||||
|
||||
project_name: skillshare
|
||||
|
||||
before:
|
||||
hooks:
|
||||
- go mod tidy
|
||||
|
||||
builds:
|
||||
- main: ./cmd/skillshare
|
||||
binary: skillshare
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- linux
|
||||
- darwin
|
||||
- windows
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
ldflags:
|
||||
- -s -w -X main.version={{.Version}}
|
||||
|
||||
archives:
|
||||
- format: tar.gz
|
||||
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
format: zip
|
||||
|
||||
checksum:
|
||||
name_template: "checksums.txt"
|
||||
|
||||
changelog:
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- "^docs:"
|
||||
- "^test:"
|
||||
- "^ci:"
|
||||
|
||||
brews:
|
||||
- repository:
|
||||
owner: runkids
|
||||
name: homebrew-tap
|
||||
directory: Formula
|
||||
homepage: https://github.com/runkids/skillshare
|
||||
description: Share skills across AI CLI tools
|
||||
license: MIT
|
||||
install: |
|
||||
bin.install "skillshare"
|
||||
test: |
|
||||
system "#{bin}/skillshare", "version"
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 runkids
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,151 @@
|
||||
# skillshare
|
||||
|
||||
Share skills across AI CLI tools (Claude Code, Codex CLI, Cursor, Gemini CLI, OpenCode).
|
||||
|
||||
## The Problem
|
||||
|
||||
Each AI CLI tool has its own skills directory:
|
||||
|
||||
```
|
||||
~/.claude/skills/
|
||||
~/.codex/skills/
|
||||
~/.cursor/skills/
|
||||
~/.gemini/antigravity/skills/
|
||||
~/.config/opencode/skills/
|
||||
```
|
||||
|
||||
Keeping them in sync manually is tedious.
|
||||
|
||||
## The Solution
|
||||
|
||||
`skillshare` maintains a single source directory and symlinks it to all CLI tools:
|
||||
|
||||
```
|
||||
~/.skills/ <- Your skills live here (single source of truth)
|
||||
├── my-skill/
|
||||
└── another-skill/
|
||||
|
||||
~/.claude/skills -> ~/.skills (symlink)
|
||||
~/.codex/skills -> ~/.skills (symlink)
|
||||
~/.cursor/skills -> ~/.skills (symlink)
|
||||
...
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
### Using Go
|
||||
|
||||
```bash
|
||||
go install github.com/runkids/skillshare/cmd/skillshare@latest
|
||||
```
|
||||
|
||||
### Using Homebrew (macOS/Linux)
|
||||
|
||||
```bash
|
||||
brew install runkids/tap/skillshare
|
||||
```
|
||||
|
||||
### Download Binary
|
||||
|
||||
Download from [Releases](https://github.com/runkids/skillshare/releases).
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
# 1. Initialize with default source directory (~/.skills)
|
||||
skillshare init
|
||||
|
||||
# 2. Check detected targets
|
||||
skillshare status
|
||||
|
||||
# 3. Sync (migrate existing skills + create symlinks)
|
||||
skillshare sync
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Initialize
|
||||
|
||||
```bash
|
||||
# Use default source (~/.skills)
|
||||
skillshare init
|
||||
|
||||
# Or specify custom source
|
||||
skillshare init --source ~/my-skills
|
||||
```
|
||||
|
||||
This will:
|
||||
- Create the source directory
|
||||
- Detect installed CLI tools
|
||||
- Create config at `~/.config/skillshare/config.yaml`
|
||||
|
||||
### Sync
|
||||
|
||||
```bash
|
||||
# Preview what will happen
|
||||
skillshare sync --dry-run
|
||||
|
||||
# Actually sync
|
||||
skillshare sync
|
||||
```
|
||||
|
||||
On first sync, existing skills are **migrated** to the source directory, then symlinks are created.
|
||||
|
||||
### Status
|
||||
|
||||
```bash
|
||||
skillshare status
|
||||
```
|
||||
|
||||
Shows:
|
||||
- Source directory and skill count
|
||||
- Each target's status (linked, has files, not exist, conflict)
|
||||
|
||||
### Manage Targets
|
||||
|
||||
```bash
|
||||
# List targets
|
||||
skillshare target list
|
||||
|
||||
# Add custom target
|
||||
skillshare target add myapp ~/.myapp/skills
|
||||
|
||||
# Remove target
|
||||
skillshare target remove myapp
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Config file: `~/.config/skillshare/config.yaml`
|
||||
|
||||
```yaml
|
||||
source: ~/.skills
|
||||
mode: symlink
|
||||
targets:
|
||||
claude:
|
||||
path: ~/.claude/skills
|
||||
codex:
|
||||
path: ~/.codex/skills
|
||||
cursor:
|
||||
path: ~/.cursor/skills
|
||||
gemini:
|
||||
path: ~/.gemini/antigravity/skills
|
||||
opencode:
|
||||
path: ~/.config/opencode/skills
|
||||
ignore:
|
||||
- "**/.DS_Store"
|
||||
- "**/.git/**"
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **init**: Detects CLI tools and creates config
|
||||
2. **sync**:
|
||||
- If target has files → migrate to source, then symlink
|
||||
- If target is empty/missing → just create symlink
|
||||
- If already linked → skip
|
||||
3. **status**: Check symlink health
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
@@ -0,0 +1,355 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/runkids/skillshare/internal/config"
|
||||
"github.com/runkids/skillshare/internal/sync"
|
||||
"github.com/runkids/skillshare/internal/ui"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
printUsage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cmd := os.Args[1]
|
||||
args := os.Args[2:]
|
||||
|
||||
var err error
|
||||
switch cmd {
|
||||
case "init":
|
||||
err = cmdInit(args)
|
||||
case "sync":
|
||||
err = cmdSync(args)
|
||||
case "status":
|
||||
err = cmdStatus(args)
|
||||
case "target":
|
||||
err = cmdTarget(args)
|
||||
case "version", "-v", "--version":
|
||||
fmt.Printf("skillshare %s\n", version)
|
||||
case "help", "-h", "--help":
|
||||
printUsage()
|
||||
default:
|
||||
ui.Error("Unknown command: %s", cmd)
|
||||
printUsage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
ui.Error("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func printUsage() {
|
||||
fmt.Println(`skillshare - Share skills across AI CLI tools
|
||||
|
||||
Usage:
|
||||
skillshare <command> [options]
|
||||
|
||||
Commands:
|
||||
init [--source PATH] Initialize skillshare with a source directory
|
||||
sync [--dry-run] [--force] Sync skills to all targets
|
||||
status Show status of all targets
|
||||
target add <name> <path> Add a target
|
||||
target remove <name> Remove a target
|
||||
target list List all targets
|
||||
version Show version
|
||||
help Show this help
|
||||
|
||||
Examples:
|
||||
skillshare init --source ~/.skills
|
||||
skillshare target add claude ~/.claude/skills
|
||||
skillshare sync
|
||||
skillshare status`)
|
||||
}
|
||||
|
||||
func cmdInit(args []string) error {
|
||||
home, _ := os.UserHomeDir()
|
||||
sourcePath := filepath.Join(home, ".skills") // Default source
|
||||
|
||||
// Parse args
|
||||
for i := 0; i < len(args); i++ {
|
||||
switch args[i] {
|
||||
case "--source", "-s":
|
||||
if i+1 >= len(args) {
|
||||
return fmt.Errorf("--source requires a path argument")
|
||||
}
|
||||
sourcePath = args[i+1]
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// Expand ~ in path
|
||||
if len(sourcePath) > 0 && sourcePath[0] == '~' {
|
||||
sourcePath = filepath.Join(home, sourcePath[1:])
|
||||
}
|
||||
|
||||
// Check if already initialized
|
||||
if _, err := os.Stat(config.ConfigPath()); err == nil {
|
||||
return fmt.Errorf("already initialized. Config at: %s", config.ConfigPath())
|
||||
}
|
||||
|
||||
// Create source directory
|
||||
if err := os.MkdirAll(sourcePath, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create source directory: %w", err)
|
||||
}
|
||||
|
||||
// Detect existing targets
|
||||
ui.Header("Detecting CLI skills directories")
|
||||
defaultTargets := config.DefaultTargets()
|
||||
targets := make(map[string]config.TargetConfig)
|
||||
|
||||
for name, target := range defaultTargets {
|
||||
if info, err := os.Stat(target.Path); err == nil {
|
||||
if info.IsDir() {
|
||||
targets[name] = target
|
||||
ui.Success("Found: %s (%s)", name, target.Path)
|
||||
}
|
||||
} else {
|
||||
// Check if parent exists (CLI is installed but no skills yet)
|
||||
parent := filepath.Dir(target.Path)
|
||||
if _, err := os.Stat(parent); err == nil {
|
||||
targets[name] = target
|
||||
ui.Info("Available: %s (%s)", name, target.Path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(targets) == 0 {
|
||||
ui.Warning("No CLI skills directories detected. You can add targets manually.")
|
||||
}
|
||||
|
||||
// Create config
|
||||
cfg := &config.Config{
|
||||
Source: sourcePath,
|
||||
Mode: "symlink",
|
||||
Targets: targets,
|
||||
Ignore: []string{
|
||||
"**/.DS_Store",
|
||||
"**/.git/**",
|
||||
},
|
||||
}
|
||||
|
||||
if err := cfg.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ui.Header("Initialized successfully")
|
||||
ui.Success("Source: %s", sourcePath)
|
||||
ui.Success("Config: %s", config.ConfigPath())
|
||||
ui.Info("Run 'skillshare sync' to sync your skills")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdSync(args []string) error {
|
||||
dryRun := false
|
||||
force := false
|
||||
|
||||
for _, arg := range args {
|
||||
switch arg {
|
||||
case "--dry-run", "-n":
|
||||
dryRun = true
|
||||
case "--force", "-f":
|
||||
force = true
|
||||
}
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure source exists
|
||||
if _, err := os.Stat(cfg.Source); os.IsNotExist(err) {
|
||||
return fmt.Errorf("source directory does not exist: %s", cfg.Source)
|
||||
}
|
||||
|
||||
ui.Header("Syncing skills")
|
||||
if dryRun {
|
||||
ui.Warning("Dry run mode - no changes will be made")
|
||||
}
|
||||
|
||||
hasError := false
|
||||
for name, target := range cfg.Targets {
|
||||
status := sync.CheckStatus(target.Path, cfg.Source)
|
||||
|
||||
// Handle conflicts
|
||||
if status == sync.StatusConflict && !force {
|
||||
link, _ := os.Readlink(target.Path)
|
||||
ui.Error("%s: conflict - symlink points to %s (use --force to override)", name, link)
|
||||
hasError = true
|
||||
continue
|
||||
}
|
||||
|
||||
if status == sync.StatusConflict && force {
|
||||
if !dryRun {
|
||||
os.Remove(target.Path)
|
||||
}
|
||||
}
|
||||
|
||||
if err := sync.SyncTarget(name, target, cfg.Source, dryRun); err != nil {
|
||||
ui.Error("%s: %v", name, err)
|
||||
hasError = true
|
||||
continue
|
||||
}
|
||||
|
||||
switch status {
|
||||
case sync.StatusLinked:
|
||||
ui.Success("%s: already linked", name)
|
||||
case sync.StatusNotExist:
|
||||
ui.Success("%s: symlink created", name)
|
||||
case sync.StatusHasFiles:
|
||||
ui.Success("%s: files migrated and linked", name)
|
||||
case sync.StatusBroken:
|
||||
ui.Success("%s: broken link fixed", name)
|
||||
case sync.StatusConflict:
|
||||
ui.Success("%s: conflict resolved (forced)", name)
|
||||
}
|
||||
}
|
||||
|
||||
if hasError {
|
||||
return fmt.Errorf("some targets failed to sync")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdStatus(args []string) error {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ui.Header("Source")
|
||||
if info, err := os.Stat(cfg.Source); err == nil {
|
||||
entries, _ := os.ReadDir(cfg.Source)
|
||||
skillCount := 0
|
||||
for _, e := range entries {
|
||||
if e.IsDir() && e.Name()[0] != '.' {
|
||||
skillCount++
|
||||
}
|
||||
}
|
||||
ui.Success("%s (%d skills, %s)", cfg.Source, skillCount, info.ModTime().Format("2006-01-02 15:04"))
|
||||
} else {
|
||||
ui.Error("%s (not found)", cfg.Source)
|
||||
}
|
||||
|
||||
ui.Header("Targets")
|
||||
for name, target := range cfg.Targets {
|
||||
status := sync.CheckStatus(target.Path, cfg.Source)
|
||||
detail := target.Path
|
||||
|
||||
switch status {
|
||||
case sync.StatusConflict:
|
||||
link, _ := os.Readlink(target.Path)
|
||||
detail = fmt.Sprintf("%s -> %s", target.Path, link)
|
||||
}
|
||||
|
||||
ui.Status(name, status.String(), detail)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdTarget(args []string) error {
|
||||
if len(args) < 1 {
|
||||
return fmt.Errorf("usage: skillshare target <add|remove|list> [args]")
|
||||
}
|
||||
|
||||
subcmd := args[0]
|
||||
subargs := args[1:]
|
||||
|
||||
switch subcmd {
|
||||
case "add":
|
||||
return targetAdd(subargs)
|
||||
case "remove", "rm":
|
||||
return targetRemove(subargs)
|
||||
case "list", "ls":
|
||||
return targetList()
|
||||
default:
|
||||
return fmt.Errorf("unknown target subcommand: %s", subcmd)
|
||||
}
|
||||
}
|
||||
|
||||
func targetAdd(args []string) error {
|
||||
if len(args) < 2 {
|
||||
return fmt.Errorf("usage: skillshare target add <name> <path>")
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
path := args[1]
|
||||
|
||||
// Expand ~
|
||||
if len(path) > 0 && path[0] == '~' {
|
||||
home, _ := os.UserHomeDir()
|
||||
path = filepath.Join(home, path[1:])
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, exists := cfg.Targets[name]; exists {
|
||||
return fmt.Errorf("target '%s' already exists", name)
|
||||
}
|
||||
|
||||
cfg.Targets[name] = config.TargetConfig{Path: path}
|
||||
if err := cfg.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ui.Success("Added target: %s -> %s", name, path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func targetRemove(args []string) error {
|
||||
if len(args) < 1 {
|
||||
return fmt.Errorf("usage: skillshare target remove <name>")
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, exists := cfg.Targets[name]; !exists {
|
||||
return fmt.Errorf("target '%s' not found", name)
|
||||
}
|
||||
|
||||
delete(cfg.Targets, name)
|
||||
if err := cfg.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ui.Success("Removed target: %s", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func targetList() error {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ui.Header("Configured Targets")
|
||||
for name, target := range cfg.Targets {
|
||||
mode := target.Mode
|
||||
if mode == "" {
|
||||
mode = "symlink"
|
||||
}
|
||||
fmt.Printf(" %-12s %s (%s)\n", name, target.Path, mode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
module github.com/runkids/skillshare
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1
|
||||
@@ -0,0 +1,4 @@
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,98 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// TargetConfig holds configuration for a single target
|
||||
type TargetConfig struct {
|
||||
Path string `yaml:"path"`
|
||||
Mode string `yaml:"mode,omitempty"` // symlink (default), copy
|
||||
}
|
||||
|
||||
// Config holds the application configuration
|
||||
type Config struct {
|
||||
Source string `yaml:"source"`
|
||||
Mode string `yaml:"mode,omitempty"` // default mode: symlink
|
||||
Targets map[string]TargetConfig `yaml:"targets"`
|
||||
Ignore []string `yaml:"ignore,omitempty"`
|
||||
}
|
||||
|
||||
// DefaultTargets returns the well-known CLI skills directories
|
||||
func DefaultTargets() map[string]TargetConfig {
|
||||
home, _ := os.UserHomeDir()
|
||||
return map[string]TargetConfig{
|
||||
"claude": {Path: filepath.Join(home, ".claude", "skills")},
|
||||
"codex": {Path: filepath.Join(home, ".codex", "skills")},
|
||||
"cursor": {Path: filepath.Join(home, ".cursor", "skills")},
|
||||
"gemini": {Path: filepath.Join(home, ".gemini", "antigravity", "skills")},
|
||||
"opencode": {Path: filepath.Join(home, ".config", "opencode", "skills")},
|
||||
}
|
||||
}
|
||||
|
||||
// ConfigPath returns the default config file path
|
||||
func ConfigPath() string {
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, ".config", "skillshare", "config.yaml")
|
||||
}
|
||||
|
||||
// Load reads the config from the default location
|
||||
func Load() (*Config, error) {
|
||||
path := ConfigPath()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("config not found: run 'skillshare init' first")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to read config: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||||
}
|
||||
|
||||
// Expand ~ in paths
|
||||
cfg.Source = expandPath(cfg.Source)
|
||||
for name, target := range cfg.Targets {
|
||||
target.Path = expandPath(target.Path)
|
||||
cfg.Targets[name] = target
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// Save writes the config to the default location
|
||||
func (c *Config) Save() error {
|
||||
path := ConfigPath()
|
||||
|
||||
// Ensure directory exists
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create config directory: %w", err)
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write config: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// expandPath expands ~ to home directory
|
||||
func expandPath(path string) string {
|
||||
if len(path) > 0 && path[0] == '~' {
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, path[1:])
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/runkids/skillshare/internal/config"
|
||||
)
|
||||
|
||||
// TargetStatus represents the state of a target
|
||||
type TargetStatus int
|
||||
|
||||
const (
|
||||
StatusUnknown TargetStatus = iota
|
||||
StatusLinked // Target is a symlink pointing to source
|
||||
StatusNotExist // Target doesn't exist
|
||||
StatusHasFiles // Target exists with files (needs migration)
|
||||
StatusConflict // Target is a symlink pointing elsewhere
|
||||
StatusBroken // Target is a broken symlink
|
||||
)
|
||||
|
||||
func (s TargetStatus) String() string {
|
||||
switch s {
|
||||
case StatusLinked:
|
||||
return "linked"
|
||||
case StatusNotExist:
|
||||
return "not exist"
|
||||
case StatusHasFiles:
|
||||
return "has files"
|
||||
case StatusConflict:
|
||||
return "conflict"
|
||||
case StatusBroken:
|
||||
return "broken"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// CheckStatus checks the status of a target
|
||||
func CheckStatus(targetPath, sourcePath string) TargetStatus {
|
||||
info, err := os.Lstat(targetPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return StatusNotExist
|
||||
}
|
||||
return StatusUnknown
|
||||
}
|
||||
|
||||
// Check if it's a symlink
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
link, err := os.Readlink(targetPath)
|
||||
if err != nil {
|
||||
return StatusUnknown
|
||||
}
|
||||
|
||||
// Check if symlink points to our source
|
||||
absLink := link
|
||||
if !filepath.IsAbs(link) {
|
||||
absLink = filepath.Join(filepath.Dir(targetPath), link)
|
||||
}
|
||||
absSource, _ := filepath.Abs(sourcePath)
|
||||
absLink, _ = filepath.Abs(absLink)
|
||||
|
||||
if absLink == absSource {
|
||||
// Verify the link is not broken
|
||||
if _, err := os.Stat(targetPath); err != nil {
|
||||
return StatusBroken
|
||||
}
|
||||
return StatusLinked
|
||||
}
|
||||
return StatusConflict
|
||||
}
|
||||
|
||||
// It's a directory with files
|
||||
if info.IsDir() {
|
||||
return StatusHasFiles
|
||||
}
|
||||
|
||||
return StatusUnknown
|
||||
}
|
||||
|
||||
// MigrateToSource moves files from target to source, then creates symlink
|
||||
func MigrateToSource(targetPath, sourcePath string) error {
|
||||
// Ensure source parent directory exists
|
||||
if err := os.MkdirAll(filepath.Dir(sourcePath), 0755); err != nil {
|
||||
return fmt.Errorf("failed to create source parent: %w", err)
|
||||
}
|
||||
|
||||
// Check if source already exists
|
||||
if _, err := os.Stat(sourcePath); err == nil {
|
||||
// Source exists - merge files
|
||||
if err := mergeDirectories(targetPath, sourcePath); err != nil {
|
||||
return fmt.Errorf("failed to merge directories: %w", err)
|
||||
}
|
||||
// Remove original target
|
||||
if err := os.RemoveAll(targetPath); err != nil {
|
||||
return fmt.Errorf("failed to remove target after merge: %w", err)
|
||||
}
|
||||
} else {
|
||||
// Source doesn't exist - just move
|
||||
if err := os.Rename(targetPath, sourcePath); err != nil {
|
||||
// Cross-device? Try copy then delete
|
||||
if err := copyDirectory(targetPath, sourcePath); err != nil {
|
||||
return fmt.Errorf("failed to copy to source: %w", err)
|
||||
}
|
||||
if err := os.RemoveAll(targetPath); err != nil {
|
||||
return fmt.Errorf("failed to remove original after copy: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateSymlink creates a symlink from target to source
|
||||
func CreateSymlink(targetPath, sourcePath string) error {
|
||||
// Ensure target parent exists
|
||||
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
|
||||
return fmt.Errorf("failed to create target parent: %w", err)
|
||||
}
|
||||
|
||||
// Create symlink
|
||||
if err := os.Symlink(sourcePath, targetPath); err != nil {
|
||||
return fmt.Errorf("failed to create symlink: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncTarget performs the sync operation for a single target
|
||||
func SyncTarget(name string, target config.TargetConfig, sourcePath string, dryRun bool) error {
|
||||
status := CheckStatus(target.Path, sourcePath)
|
||||
|
||||
switch status {
|
||||
case StatusLinked:
|
||||
// Already correct
|
||||
return nil
|
||||
|
||||
case StatusNotExist:
|
||||
if dryRun {
|
||||
fmt.Printf("[dry-run] Would create symlink: %s -> %s\n", target.Path, sourcePath)
|
||||
return nil
|
||||
}
|
||||
return CreateSymlink(target.Path, sourcePath)
|
||||
|
||||
case StatusHasFiles:
|
||||
if dryRun {
|
||||
fmt.Printf("[dry-run] Would migrate files from %s to %s, then create symlink\n", target.Path, sourcePath)
|
||||
return nil
|
||||
}
|
||||
if err := MigrateToSource(target.Path, sourcePath); err != nil {
|
||||
return err
|
||||
}
|
||||
return CreateSymlink(target.Path, sourcePath)
|
||||
|
||||
case StatusConflict:
|
||||
link, _ := os.Readlink(target.Path)
|
||||
return fmt.Errorf("target is symlink to different location: %s -> %s", target.Path, link)
|
||||
|
||||
case StatusBroken:
|
||||
if dryRun {
|
||||
fmt.Printf("[dry-run] Would remove broken symlink and recreate: %s\n", target.Path)
|
||||
return nil
|
||||
}
|
||||
os.Remove(target.Path)
|
||||
return CreateSymlink(target.Path, sourcePath)
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unknown target status: %s", status)
|
||||
}
|
||||
}
|
||||
|
||||
// mergeDirectories copies files from src to dst, skipping existing files
|
||||
func mergeDirectories(src, dst string) error {
|
||||
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
relPath, _ := filepath.Rel(src, path)
|
||||
dstPath := filepath.Join(dst, relPath)
|
||||
|
||||
if info.IsDir() {
|
||||
return os.MkdirAll(dstPath, info.Mode())
|
||||
}
|
||||
|
||||
// Skip if destination exists
|
||||
if _, err := os.Stat(dstPath); err == nil {
|
||||
fmt.Printf(" skip (exists): %s\n", relPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
return copyFile(path, dstPath)
|
||||
})
|
||||
}
|
||||
|
||||
// copyDirectory copies a directory recursively
|
||||
func copyDirectory(src, dst string) error {
|
||||
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
relPath, _ := filepath.Rel(src, path)
|
||||
dstPath := filepath.Join(dst, relPath)
|
||||
|
||||
if info.IsDir() {
|
||||
return os.MkdirAll(dstPath, info.Mode())
|
||||
}
|
||||
|
||||
return copyFile(path, dstPath)
|
||||
})
|
||||
}
|
||||
|
||||
// copyFile copies a single file
|
||||
func copyFile(src, dst string) error {
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
srcInfo, err := srcFile.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, srcInfo.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
_, err = io.Copy(dstFile, srcFile)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Colors for terminal output
|
||||
const (
|
||||
Reset = "\033[0m"
|
||||
Red = "\033[31m"
|
||||
Green = "\033[32m"
|
||||
Yellow = "\033[33m"
|
||||
Blue = "\033[34m"
|
||||
Magenta = "\033[35m"
|
||||
Cyan = "\033[36m"
|
||||
Gray = "\033[90m"
|
||||
)
|
||||
|
||||
// Success prints a success message
|
||||
func Success(format string, args ...interface{}) {
|
||||
fmt.Printf(Green+"✓ "+Reset+format+"\n", args...)
|
||||
}
|
||||
|
||||
// Error prints an error message
|
||||
func Error(format string, args ...interface{}) {
|
||||
fmt.Printf(Red+"✗ "+Reset+format+"\n", args...)
|
||||
}
|
||||
|
||||
// Warning prints a warning message
|
||||
func Warning(format string, args ...interface{}) {
|
||||
fmt.Printf(Yellow+"! "+Reset+format+"\n", args...)
|
||||
}
|
||||
|
||||
// Info prints an info message
|
||||
func Info(format string, args ...interface{}) {
|
||||
fmt.Printf(Cyan+"→ "+Reset+format+"\n", args...)
|
||||
}
|
||||
|
||||
// Status prints a status line
|
||||
func Status(name, status, detail string) {
|
||||
statusColor := Gray
|
||||
switch status {
|
||||
case "linked":
|
||||
statusColor = Green
|
||||
case "not exist":
|
||||
statusColor = Yellow
|
||||
case "has files":
|
||||
statusColor = Blue
|
||||
case "conflict", "broken":
|
||||
statusColor = Red
|
||||
}
|
||||
|
||||
fmt.Printf(" %-12s %s%-12s%s %s\n", name, statusColor, status, Reset, Gray+detail+Reset)
|
||||
}
|
||||
|
||||
// Header prints a section header
|
||||
func Header(text string) {
|
||||
fmt.Printf("\n%s%s%s\n", Cyan, text, Reset)
|
||||
fmt.Println(Gray + "─────────────────────────────────────────" + Reset)
|
||||
}
|
||||
Reference in New Issue
Block a user