-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
184 lines (160 loc) · 4.78 KB
/
Copy pathmain.go
File metadata and controls
184 lines (160 loc) · 4.78 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"strings"
"github.com/umlx5h/zsh-manpage-completion-generator/internal/converter"
"github.com/umlx5h/zsh-manpage-completion-generator/internal/util"
)
// These variables are set in build step
var (
version = "unset"
commit = "unset"
date = "unset"
)
var (
excludeCmds = []string{
"[",
"]",
"sudo",
}
)
func main() {
dataDir := os.Getenv("XDG_DATA_HOME")
if dataDir == "" {
dataDir = filepath.Join(os.Getenv("HOME"), ".local/share")
}
filepath.Join(dataDir)
srcDir := filepath.Join(dataDir, "fish/generated_completions")
dstDir := filepath.Join(dataDir, "zsh/generated_man_completions")
src := flag.String("src", srcDir, "fish generated_completions src folder")
dst := flag.String("dst", dstDir, "zsh generated_completions destination folder")
clean := flag.Bool("clean", false, "CAUTION: remove destination folder before converting")
verbose := flag.Bool("verbose", false, "verbose log")
version_ := flag.Bool("version", false, "show version")
flag.Parse()
srcDir = *src
dstDir = *dst
isClean := *clean
isVerbose := *verbose
isVersion := *version_
if isVersion {
if commit != "unset" {
fmt.Printf("Version: %s, Commit: %s, Date: %s\n", version, commit, date)
} else if buildInfo, ok := debug.ReadBuildInfo(); ok {
fmt.Printf("Version: %s\n", buildInfo.Main.Version)
} else {
fmt.Printf("Version: %s\n", "(unknown)")
}
os.Exit(0)
}
srcDirEntry, err := os.ReadDir(srcDir)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open srcDir, are you sure to install fish?: %s", err)
os.Exit(1)
}
dir, err := os.Stat(dstDir)
if err != nil && !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "could not open dstDir: %s", err)
os.Exit(1)
} else if err == nil && !dir.IsDir() {
fmt.Fprintf(os.Stderr, "could not open dstDir as directory: %s", dstDir)
os.Exit(1)
}
if isClean {
fmt.Printf("Cleaning zsh completions folder: %s\n", dstDir)
if err = os.RemoveAll(dstDir); err != nil {
fmt.Fprintf(os.Stderr, "could not remove dstDir: %s", err)
os.Exit(1)
}
if err = os.MkdirAll(dstDir, 0777); err != nil {
fmt.Fprintf(os.Stderr, "could not mkdir dstDir: %s", err)
os.Exit(1)
}
} else if os.IsNotExist(err) {
if err = os.MkdirAll(dstDir, 0777); err != nil {
fmt.Fprintf(os.Stderr, "could not mkdir dstDir: %s", err)
os.Exit(1)
}
}
var (
// stat
convertNum int
convertedNum int
skippedNum int
)
fmt.Printf("Converting fish completions: %s -> %s\n", srcDir, dstDir)
for _, f := range srcDirEntry {
func() {
if f.Type().IsRegular() {
fileName := f.Name()
if !strings.HasSuffix(fileName, ".fish") {
fmt.Printf("skipped non fish file: %s\n", fileName)
return
}
if strings.HasSuffix(fileName, ".1posix.fish") {
// In zsh, posix and non-posix versions need to be represented as one common command, so skip posix version
// TODO: if only found posix version, then convert it.
if isVerbose {
fmt.Printf("skipped posix version: %s\n", fileName)
}
skippedNum++
return
}
cmdName := strings.TrimSuffix(fileName, ".fish")
// delete unneeded command
if util.Contains(excludeCmds, cmdName) {
if isVerbose {
fmt.Printf("skipped unneeded command: %s\n", fileName)
}
skippedNum++
return
}
// NOTE: exclude git commands to prevent weird errors
// _git:8182: bad math expression: operand expected at `/home/user/...'
// _git:8182: bad math expression: operand expected at `/home/user/...'
// _git:8182: math recursion limit exceeded: name-rev
// _git:8182: bad math expression: operator expected at `browse'
if cmdName == "git" || strings.HasPrefix(cmdName, "git-") {
if isVerbose {
fmt.Printf("skipped git command: %s\n", fileName)
}
skippedNum++
return
}
srcFilePath := filepath.Join(srcDir, fileName)
srcFile, err := os.Open(filepath.Join(srcFilePath))
if err != nil {
fmt.Fprintf(os.Stderr, "could not open srcFile: %s: %s", srcFilePath, err)
os.Exit(1)
}
defer srcFile.Close()
convertNum++
converter := converter.NewConverter(srcFile, cmdName)
fileContent, err := converter.Convert()
if err != nil {
if isVerbose {
fmt.Printf("failed to convert: %s: %s\n", fileName, err)
}
return
}
dstFilePath := filepath.Join(dstDir, "_"+cmdName)
dstFile, err := os.Create(dstFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create dstFile: %s: %s", dstFilePath, err)
os.Exit(1)
}
defer dstFile.Close()
dstFile.WriteString(fileContent)
if isVerbose {
fmt.Printf("converted: %s\n", f.Name())
}
convertedNum++
}
}()
}
fmt.Printf("Completed. converted: %d/%d, skipped: %d\n", convertedNum, convertNum, skippedNum)
}