summaryrefslogtreecommitdiffhomepage
path: root/tempfork/spf13/cobra/comp.go
blob: 4def2c068d70db38b2f3eaacbbf6a24e5d950f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cobra

import (
	"bytes"
	"compress/gzip"
	_ "embed"
	"fmt"
	"io"
)

//go:generate go run gen.go

//go:embed comp.bash.gz
var compBash string

func ScriptBash(w io.Writer, name, compCmd, nameForVar string) error {
	return fmtgz(
		w, compBash,
		name, compCmd,
		ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
		ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
	)
}

//go:embed comp.zsh.gz
var compZsh string

func ScriptZsh(w io.Writer, name, compCmd, nameForVar string) error {
	return fmtgz(
		w, compZsh,
		name, compCmd,
		ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
		ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
	)
}

//go:embed comp.fish.gz
var compFish string

func ScriptFish(w io.Writer, name, compCmd, nameForVar string) error {
	return fmtgz(
		w, compFish,
		nameForVar, name, compCmd,
		ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
		ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
	)
}

//go:embed comp.ps1.gz
var compPowershell string

func ScriptPowershell(w io.Writer, name, compCmd, nameForVar string) error {
	return fmtgz(
		w, compPowershell,
		name, nameForVar, compCmd,
		ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
		ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
	)
}

func fmtgz(w io.Writer, formatgz string, args ...any) error {
	f, err := gzip.NewReader(bytes.NewBufferString(formatgz))
	if err != nil {
		return fmt.Errorf("decompressing script: %w", err)
	}
	format, err := io.ReadAll(f)
	if err != nil {
		return fmt.Errorf("decompressing script: %w", err)
	}
	_, err = fmt.Fprintf(w, string(format), args...)
	return err
}