feat(hook): write marked profile blocks and register zsh completion

`netssh hook install` and the setup wizard now write the same marked
`# >>> netssh >>>` block as install.sh and replace unmarked entries of
older versions instead of appending duplicates. Uninstall shares the
legacy patterns with the installer (eval/alias/source variants).

For zsh the block adds ~/.zfunc to fpath and calls compdef, so
completion also works when compinit already ran earlier in .zshrc,
e.g. with oh-my-zsh. The installer reports which completion it set up
per shell and how to reset ~/.zcompdump.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-10-28 11:22:53 +01:00
1 parent 8fb7633783
commit 129a7201bc
6 files changed
+221 -55

No files matched your search

+107 -27
View File
@@ -62,27 +62,63 @@ func IsInstalled(profilePath string) bool {
return false
}
// Install appends the hook line to the profile if it is not already present.
// Returns (true, nil) when newly installed, (false, nil) when already present.
// Install writes the managed netssh block into the profile. Entries written by
// older versions without markers are replaced instead of duplicated; an
// existing managed block (e.g. from install.sh) is left untouched.
// Returns (true, nil) when the profile changed, (false, nil) when already present.
// Creates the profile file (and any parent directories) if they do not exist.
func Install(profilePath string) (installed bool, err error) {
if IsInstalled(profilePath) {
return false, nil
func Install(profilePath, shell string) (installed bool, err error) {
data, err := os.ReadFile(profilePath)
if err != nil && !os.IsNotExist(err) {
return false, fmt.Errorf("reading profile: %w", err)
}
for _, line := range strings.Split(string(data), "\n") {
if strings.TrimSpace(line) == BlockStart {
return false, nil
}
}
cleaned, _, err := clean(string(data), profilePath)
if err != nil {
return false, err
}
if cleaned != "" && !strings.HasSuffix(cleaned, "\n") {
cleaned += "\n"
}
if cleaned != "" {
cleaned += "\n"
}
cleaned += Block(shell)
if err := os.MkdirAll(filepath.Dir(profilePath), 0o755); err != nil {
return false, fmt.Errorf("creating directory: %w", err)
}
f, err := os.OpenFile(profilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return false, fmt.Errorf("opening profile: %w", err)
}
defer f.Close()
if _, err := fmt.Fprintf(f, "\n%s\n", Line); err != nil {
if err := writeProfile(profilePath, cleaned); err != nil {
return false, err
}
return true, nil
}
// Block returns the managed profile block for shell.
func Block(shell string) string {
var b strings.Builder
b.WriteString(BlockStart + "\n")
b.WriteString("netssh shell-init\n")
if shell != "fish" {
aliases := shellQuote(AliasesPath(), "bash")
fmt.Fprintf(&b, "[ ! -f %s ] || . %s\n", aliases, aliases)
}
if shell == "zsh" {
b.WriteString(ZshRegistration(ZshFuncDir()))
}
b.WriteString(BlockEnd + "\n")
return b.String()
}
// ZshRegistration adds dir to fpath and registers the completion even when
// compinit already ran earlier in .zshrc (e.g. via oh-my-zsh).
func ZshRegistration(dir string) string {
return fmt.Sprintf("fpath=(%s $fpath)\nif (( $+functions[compdef] )); then autoload -Uz _netssh && compdef _netssh netssh; fi\n", shellQuote(dir, "bash"))
}
// InstallAliasesSource appends the aliases source line to a bash/zsh profile.
// This is a no-op for fish (which uses conf.d auto-sourcing instead).
// Returns (true, nil) when newly added, (false, nil) when already present.
@@ -110,6 +146,26 @@ func InstallAliasesSource(profilePath string) (installed bool, err error) {
var hookLinePattern = regexp.MustCompile(`^(?:[^\s]*/)?netssh\s+shell-init\s*(?:#.*)?$`)
var aliasesLinePattern = regexp.MustCompile(`^(?:source|\.)\s+["']?[^;|&]*netssh/aliases\.sh["']?(?:\s+2>/dev/null)?\s*(?:#.*)?$`)
// legacyPatterns match the unmarked lines written by older versions and by
// manual setup instructions. They mirror the patterns in install.sh.
var legacyPatterns = []*regexp.Regexp{
hookLinePattern,
aliasesLinePattern,
regexp.MustCompile(`^eval\s+["']?\$\((?:[^\s]*/)?netssh\s+alias(?:\s+--shell\s+(?:bash|zsh))?\)["']?\s*(?:#.*)?$`),
regexp.MustCompile(`^(?:[^\s]*/)?netssh\s+alias(?:\s+--shell\s+fish)?\s*\|\s*source\s*(?:#.*)?$`),
regexp.MustCompile(`^source\s+<\((?:[^\s]*/)?netssh\s+alias\)\s*(?:#.*)?$`),
regexp.MustCompile(`^alias\s+[A-Za-z0-9_.-]+(?:=|\s+)["']netssh(?:\s+[^"']+)?["']\s*(?:#.*)?$`),
}
func isLegacyLine(line string) bool {
for _, pattern := range legacyPatterns {
if pattern.MatchString(line) {
return true
}
}
return false
}
// Uninstall removes legacy hook lines and complete installer-managed blocks.
// Missing profiles are a no-op; malformed blocks leave the file unchanged.
func Uninstall(profilePath string) (removed bool, err error) {
@@ -120,21 +176,33 @@ func Uninstall(profilePath string) (removed bool, err error) {
if err != nil {
return false, fmt.Errorf("reading profile: %w", err)
}
cleaned, removed, err := clean(string(data), profilePath)
if err != nil || !removed {
return false, err
}
return true, writeProfile(profilePath, cleaned)
}
// clean strips managed blocks and legacy lines from profile content.
func clean(data, profilePath string) (cleaned string, removed bool, err error) {
if data == "" {
return "", false, nil
}
var kept []string
inside := false
for _, line := range strings.Split(string(data), "\n") {
for _, line := range strings.Split(data, "\n") {
value := strings.TrimSpace(line)
switch value {
case BlockStart:
if inside {
return false, fmt.Errorf("nested netssh block in %s", profilePath)
return "", false, fmt.Errorf("nested netssh block in %s", profilePath)
}
inside = true
removed = true
continue
case BlockEnd:
if !inside {
return false, fmt.Errorf("unmatched netssh block in %s", profilePath)
return "", false, fmt.Errorf("unmatched netssh block in %s", profilePath)
}
inside = false
continue
@@ -142,29 +210,41 @@ func Uninstall(profilePath string) (removed bool, err error) {
if inside {
continue
}
if hookLinePattern.MatchString(value) || aliasesLinePattern.MatchString(value) {
if isLegacyLine(value) {
removed = true
continue
}
kept = append(kept, line)
}
if inside {
return false, fmt.Errorf("unclosed netssh block in %s", profilePath)
return "", false, fmt.Errorf("unclosed netssh block in %s", profilePath)
}
if !removed {
return false, nil
return data, false, nil
}
cleaned := strings.ReplaceAll(strings.Join(kept, "\n"), "\n\n\n", "\n\n")
target, err := filepath.EvalSymlinks(profilePath)
if err != nil {
return false, err
cleaned = strings.ReplaceAll(strings.Join(kept, "\n"), "\n\n\n", "\n\n")
// Drop the blank separator line Install puts in front of the block.
if strings.HasSuffix(cleaned, "\n\n") {
cleaned = strings.TrimRight(cleaned, "\n") + "\n"
}
info, err := os.Stat(target)
if err != nil {
return false, err
return cleaned, true, nil
}
// writeProfile replaces the profile atomically, following dotfile symlinks
// and keeping the permissions of an existing file.
func writeProfile(profilePath, content string) error {
target, err := filepath.EvalSymlinks(profilePath)
if os.IsNotExist(err) {
target = profilePath
} else if err != nil {
return err
}
if err := storage.WriteFile(target, []byte(cleaned), info.Mode().Perm()); err != nil {
return false, fmt.Errorf("writing profile: %w", err)
mode := os.FileMode(0o644)
if info, err := os.Stat(target); err == nil {
mode = info.Mode().Perm()
}
return true, nil
if err := storage.WriteFile(target, []byte(content), mode); err != nil {
return fmt.Errorf("writing profile: %w", err)
}
return nil
}