-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpterm-ci-cmd.go
136 lines (111 loc) · 4.33 KB
/
pterm-ci-cmd.go
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
package pcli
import (
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
// ptermCICmd represents the ptermCi command
// ! Do not delete this file. It it used inside the CI system.
var ptermCICmd = &cobra.Command{
Use: "ptermCI",
Short: "Run internal CI-System to update documentation.",
Long: `This command is used in the CI-System to generate new documentation of the CLI tool.
It should not be used outside the development of this tool.`,
Run: func(cmd *cobra.Command, args []string) {
pterm.Info.Printfln("Running PtermCI for %s", rootCmd.Name())
started := time.Now()
originURL := detectOriginURL()
if !strings.Contains(originURL, "/cli-template") {
if _, err := os.Stat(getPathTo("./setup/main.go")); err == nil {
pterm.DefaultSection.Println("Running template setup")
cmd := exec.Command("go", "run", getPathTo("./setup/main.go"))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pterm.Fatal.PrintOnError(cmd.Run())
pterm.DefaultSection.Println("Deleting setup script")
pterm.Fatal.PrintOnError(os.RemoveAll(getPathTo("./setup")))
}
}
pterm.DefaultSection.Println("Generating markdown documentation")
markdownDoc := GenerateMarkdownDoc(rootCmd)
pterm.Fatal.PrintOnError(ioutil.WriteFile(getPathTo("/docs/docs.md"), []byte(markdownDoc.Markdown), 0777))
project := struct {
ProjectPath string
Name string
RepoName string
UserName string
URL string
Short string
Long string
GitHubPagesURL string
InstallCommandWindows string
InstallCommandLinux string
InstallCommandMacOS string
}{}
projectParts := strings.Split(strings.TrimPrefix(originURL, "https://github.com/"), "/")
project.UserName = projectParts[0]
project.RepoName = projectParts[1]
project.ProjectPath = pterm.Sprintf("%s/%s", project.UserName, project.RepoName)
project.Name = rootCmd.Name()
project.URL = pterm.Sprintf("https://github.com/%s", project.ProjectPath)
project.Short = rootCmd.Short
project.Long = rootCmd.Long
project.InstallCommandWindows = pterm.Sprintf(`iwr instl.sh/%s/windows | iex`, project.ProjectPath)
project.InstallCommandLinux = pterm.Sprintf(`curl -sSL instl.sh/%s/linux | bash`, project.ProjectPath)
project.InstallCommandMacOS = pterm.Sprintf(`curl -sSL instl.sh/%s/macos | bash`, project.ProjectPath)
project.GitHubPagesURL = pterm.Sprintf("https://%s.github.io/%s", project.UserName, project.RepoName)
pterm.DefaultSection.Println("Processing '*.template.[md|html|js|css]' files")
walkOverExt("", ".template.md,.template.html,.template.js,.template.css", func(path string) {
contentBytes, err := ioutil.ReadFile(path)
content := string(contentBytes)
tmpl, err := template.New(filepath.Base(path)).Parse(content)
pterm.Fatal.PrintOnError(err)
file, err := os.OpenFile(strings.ReplaceAll(path, ".template", ""), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
pterm.Fatal.PrintOnError(err)
pterm.Fatal.PrintOnError(tmpl.Execute(file, project))
pterm.Fatal.PrintOnError(file.Close())
})
pterm.DefaultSection.Println("Copying README.md to docs/REAMDE.md")
input, err := ioutil.ReadFile(getPathTo("/README.md"))
pterm.Fatal.PrintOnError(err)
pterm.Fatal.PrintOnError(ioutil.WriteFile(getPathTo("/docs/README.md"), input, 0777))
pterm.Success.Printfln("The PTerm-CI System took %v to complete.", time.Since(started))
},
Hidden: true,
}
func detectOriginURL() (url string) {
out, err := exec.Command("git", "remote", "-v").Output()
pterm.Fatal.PrintOnError(err)
pterm.Debug.Printfln("Git output:\n%s", string(out))
output := string(out)
for _, s := range strings.Split(output, "\n") {
s = strings.TrimSpace(strings.TrimPrefix(s, "origin"))
if strings.HasPrefix(s, "https://github.com/") && strings.Contains(s, "push") {
pterm.Debug.Printfln("Detected GitHub Repo: %s", s)
url = strings.TrimSpace(strings.TrimRight(s, "(push)"))
return
}
}
return
}
func walkOverExt(path, exts string, f func(path string)) {
_ = filepath.Walk(getPathTo(path), func(path string, info fs.FileInfo, err error) error {
for _, ext := range strings.Split(exts, ",") {
if strings.HasSuffix(path, ext) {
f(path)
}
}
return nil
})
}
func getPathTo(file string) string {
// dir, _ := os.Getwd()
return filepath.Join("./", file)
}