|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +// This tool is used to move the generated commands documentation from the `web-docs` directory to the `hcp-docs` repository. |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "flag" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "io/fs" |
| 17 | + |
| 18 | + "github.com/hashicorp/hcp/internal/pkg/cmd" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + if err := run(); err != nil { |
| 23 | + fmt.Fprintf(os.Stderr, "%v\n", err) |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func run() error { |
| 29 | + // Define the flags |
| 30 | + var srcCommandsDir string |
| 31 | + var destCommandsDir string |
| 32 | + var srcNavJSON string |
| 33 | + var destNavJSON string |
| 34 | + |
| 35 | + flag.StringVar(&srcCommandsDir, "generated-commands-dir", "web-docs/commands", "The generated commands documentation to move to hcp-docs repository") |
| 36 | + flag.StringVar(&destCommandsDir, "dest-commands-dir", "../hcp-docs/content/docs/cli/commands/", "The destination directory for the generated commands documentation") |
| 37 | + flag.StringVar(&srcNavJSON, "generated-nav-json", "web-docs/nav.json", "The output path for the generated nav json") |
| 38 | + flag.StringVar(&destNavJSON, "dest-nav-json", "../hcp-docs/data/docs-nav-data.json", "Path to `hcp-docs` nav json file") |
| 39 | + |
| 40 | + // Parse the flags |
| 41 | + flag.Parse() |
| 42 | + |
| 43 | + // Delete the existing commands directory |
| 44 | + if err := os.RemoveAll(destCommandsDir); err != nil { |
| 45 | + return fmt.Errorf("failed to remove destination commands directory: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Move the commands directory |
| 49 | + if err := CopyDir(destCommandsDir, srcCommandsDir); err != nil { |
| 50 | + return fmt.Errorf("failed to copy commands directory: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Open the existing nav JSON for both read and writing. |
| 54 | + dstNavFD, err := os.OpenFile(destNavJSON, os.O_RDWR, 0644) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("failed to open destination nav JSON: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + // Parse the JSON |
| 60 | + var navData []cmd.DocNavItem |
| 61 | + if err := json.NewDecoder(dstNavFD).Decode(&navData); err != nil { |
| 62 | + return fmt.Errorf("failed to decode destination nav JSON: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Find the HCP CLI section to inject into |
| 66 | + var hcpCommandsSection *cmd.DocNavItem |
| 67 | + |
| 68 | +OUTER: |
| 69 | + for _, root := range navData { |
| 70 | + if root.Title != "HCP CLI" { |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + // Find the generated commands section |
| 75 | + for _, route := range root.Routes { |
| 76 | + if route.Title != "Commands (CLI)" { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + hcpCommandsSection = route |
| 81 | + break OUTER |
| 82 | + } |
| 83 | + } |
| 84 | + if hcpCommandsSection == nil { |
| 85 | + return fmt.Errorf("failed to find HCP CLI section in destination nav JSON") |
| 86 | + } |
| 87 | + |
| 88 | + // Open the generated nav JSON |
| 89 | + srcNavFD, err := os.Open(srcNavJSON) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("failed to open source nav JSON: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + // Parse the JSON |
| 95 | + var srcNavData cmd.DocNavItem |
| 96 | + if err := json.NewDecoder(srcNavFD).Decode(&srcNavData); err != nil { |
| 97 | + return fmt.Errorf("failed to decode source nav JSON: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Inject the HCP CLI section |
| 101 | + *hcpCommandsSection = srcNavData |
| 102 | + |
| 103 | + // Serialize the JSON |
| 104 | + if _, err := dstNavFD.Seek(0, 0); err != nil { |
| 105 | + return fmt.Errorf("failed to seek destination nav JSON: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + e := json.NewEncoder(dstNavFD) |
| 109 | + e.SetIndent("", " ") |
| 110 | + e.SetEscapeHTML(false) |
| 111 | + if err := e.Encode(navData); err != nil { |
| 112 | + return fmt.Errorf("failed to encode destination nav JSON: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// CopyDir copies the content of src to dst. src should be a full path. |
| 119 | +func CopyDir(dst, src string) error { |
| 120 | + return filepath.Walk(src, func(path string, info fs.FileInfo, err error) error { |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + // copy to this path |
| 126 | + outpath := filepath.Join(dst, strings.TrimPrefix(path, src)) |
| 127 | + if info.IsDir() { |
| 128 | + if err := os.MkdirAll(outpath, info.Mode()); err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + return nil // means recursive |
| 132 | + } |
| 133 | + |
| 134 | + // handle irregular files |
| 135 | + if !info.Mode().IsRegular() { |
| 136 | + if info.Mode().Type()&os.ModeType == os.ModeSymlink { |
| 137 | + link, err := os.Readlink(path) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + return os.Symlink(link, outpath) |
| 142 | + } |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + // copy contents of regular file efficiently |
| 147 | + |
| 148 | + // open input |
| 149 | + in, err := os.Open(path) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + defer in.Close() |
| 154 | + |
| 155 | + // create output |
| 156 | + fh, err := os.Create(outpath) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + defer fh.Close() |
| 161 | + |
| 162 | + // make it the same |
| 163 | + if err := fh.Chmod(info.Mode()); err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + // copy content |
| 168 | + _, err = io.Copy(fh, in) |
| 169 | + return err |
| 170 | + }) |
| 171 | +} |
0 commit comments