-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclidocstool.go
More file actions
159 lines (146 loc) · 3.72 KB
/
clidocstool.go
File metadata and controls
159 lines (146 loc) · 3.72 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
// Copyright 2017 cli-docs-tool authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package clidocstool
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
// Options defines options for cli-docs-tool
type Options struct {
Root *cobra.Command
SourceDir string
TargetDir string
Plugin bool
}
// Client represents an active cli-docs-tool object
type Client struct {
root *cobra.Command
source string
target string
plugin bool
}
// New initializes a new cli-docs-tool client
func New(opts Options) (*Client, error) {
if opts.Root == nil {
return nil, errors.New("root cmd required")
}
if len(opts.SourceDir) == 0 {
return nil, errors.New("source dir required")
}
c := &Client{
root: opts.Root,
source: opts.SourceDir,
plugin: opts.Plugin,
}
if len(opts.TargetDir) == 0 {
c.target = c.source
} else {
c.target = opts.TargetDir
}
if err := os.MkdirAll(c.target, 0755); err != nil {
return nil, err
}
return c, nil
}
// GenAllTree creates all structured ref files for this command and
// all descendants in the directory given.
func (c *Client) GenAllTree() error {
var err error
if err = c.GenMarkdownTree(c.root); err != nil {
return err
}
if err = c.GenYamlTree(c.root); err != nil {
return err
}
return nil
}
// GenAllTreeAndRemoveOldFiles creates all structured ref files for this command and
// all descendants in the directory given then removes potential old documentation files from the origin directory tree.
func (c *Client) GenAllTreeAndRemoveOldFiles() error {
if err := c.GenAllTree(); err != nil {
return err
}
filesToRemove := make(map[string]any)
filepath.WalkDir(c.source, func(path string, entry fs.DirEntry, err error) error {
return c.checkIfShouldBeRemoved(filesToRemove, path, entry, err)
})
for file := range filesToRemove {
if err := os.Remove(file); err != nil {
return err
}
}
return nil
}
func (c *Client) checkIfShouldBeRemoved(filesToRemove map[string]any, path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.IsDir() {
if _, err := entry.Info(); err != nil {
return err
}
targetFile := filepath.Join(c.target, strings.ReplaceAll(path, c.source, ""))
if _, err := os.Stat(targetFile); os.IsNotExist(err) {
filesToRemove[path] = struct{}{}
}
}
return nil
}
func fileExists(f string) bool {
info, err := os.Stat(f)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func copyFile(src string, dst string) error {
sf, err := os.Open(src)
if err != nil {
return err
}
defer sf.Close()
df, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
return err
}
func getAliases(cmd *cobra.Command) []string {
if a := cmd.Annotations["aliases"]; a != "" {
aliases := strings.Split(a, ",")
for i := 0; i < len(aliases); i++ {
aliases[i] = strings.TrimSpace(aliases[i])
}
return aliases
}
if len(cmd.Aliases) == 0 {
return cmd.Aliases
}
var parentPath string
if cmd.HasParent() {
parentPath = cmd.Parent().CommandPath() + " "
}
aliases := []string{cmd.CommandPath()}
for _, a := range cmd.Aliases {
aliases = append(aliases, parentPath+a)
}
return aliases
}