Skip to content

Commit

Permalink
update-imported-docs in golang (kubernetes#6642)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu authored and steveperry-53 committed Jan 3, 2018
1 parent d6249f5 commit 79d24ab
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions update-imported-docs/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
update-imported-docs/update-imported-docs.go will update the target files generated from other repos.
You should modify update-imported-docs/config.yaml to reflect the desired src and dst path.
```
go run update-imported-docs/update-imported-docs.go
```
14 changes: 14 additions & 0 deletions update-imported-docs/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- name: kubernetes
remote: https://github.com/kubernetes/kubernetes.git
branch: release-1.9
files:
- src: docs/admin/cloud-controller-manager.md
dst: docs/reference/generated/cloud-controller-manager.md
#- name: federation
# remote: https://github.com/kubernetes/federation.git
# # Change this to a release branch when federation has release branches.
# branch: master
# files:
# - src: a/foo.md
# dst: b/foo.md
124 changes: 124 additions & 0 deletions update-imported-docs/update-imported-docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"

"github.com/ghodss/yaml"
)

func main() {
websiteRepo, err := os.Getwd()
checkError(err)

content, err := ioutil.ReadFile("update-imported-docs/config.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "error when reading file: %v\n", err)
os.Exit(1)
}

var config map[string]interface{}
err = yaml.Unmarshal(content, &config)
if err != nil {
fmt.Fprintf(os.Stderr, "error when unmarshal the config file: %v\n", err)
os.Exit(1)
}

tmpDir := "/tmp/update_docs"
os.RemoveAll(tmpDir)
os.Mkdir(tmpDir, 0750)

// Match the content between 2 `---`
// It mostly have something like:
// ---
// title: ***
// notile: ***
// ---
titleRegex := regexp.MustCompile("^---\n(.*\n)*---\n")

repos := config["repos"].([]interface{})
for _, repo := range repos {
err = os.Chdir(tmpDir)
checkError(err)

r := repo.(map[string]interface{})
repoName := r["name"].(string)
cmd := "git"
args := []string{"clone", "--depth=1", "-b", r["branch"].(string), r["remote"].(string), repoName}
fmt.Fprintf(os.Stdout, "Cloning repo %q\n", repoName)
if err := exec.Command(cmd, args...).Run(); err != nil {
fmt.Fprintf(os.Stderr, "error when cloning repo %q: %v\n", repoName, err)
os.Exit(1)
}

err = os.Chdir(repoName)
checkError(err)

fmt.Fprintf(os.Stdout, "Generating docs for repo %q\n", repoName)
if err := exec.Command("hack/generate-docs.sh").Run(); err != nil {
fmt.Fprintf(os.Stderr, "error when generating docs for repo %q: %v\n", repoName, err)
os.Exit(1)
}

err = os.Chdir(websiteRepo)
checkError(err)
files := r["files"].([]interface{})
for _, file := range files {
f := file.(map[string]interface{})
src := f["src"].(string)
dst := f["dst"].(string)
absSrc, err := filepath.Abs(path.Join(tmpDir, repoName, src))
checkError(err)
absDst, err := filepath.Abs(dst)
checkError(err)
// Ignore the error if the old file is not found/
content, _ := ioutil.ReadFile(absDst)
titleBlock := titleRegex.Find(content)
content, err = ioutil.ReadFile(absSrc)
checkError(err)
dstFile, err := os.OpenFile(absDst, os.O_RDWR|os.O_CREATE, 0755)
checkError(err)
defer dstFile.Close()
_, err = dstFile.Write(titleBlock)
checkError(err)
_, err = dstFile.Write(content)
checkError(err)
dstFile.Sync()
}
}
fmt.Fprintf(os.Stdout, "Docs imported! Run 'git add .' 'git commit -m <comment>' and 'git push' to upload them\n")
}

func copyFile(src, dst string) error {
sf, err := os.Open(src)
if err != nil {
return err
}
defer sf.Close()

df, err := os.Create(dst)
if err != nil {
return err
}
defer df.Close()

_, err = io.Copy(df, sf)
if err != nil {
return err
}

return df.Sync()
}

func checkError(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

0 comments on commit 79d24ab

Please sign in to comment.