Created
May 6, 2022 04:25
-
-
Save tony612/6af5d4afdb0824a98402e51ba9d3338e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Run this in root of https://github.com/vscode-kubernetes-tools/vscode-kubernetes-tools | |
// `snippets` dir is used. | |
// Somehow vscode can't show snippets by default. | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/fs" | |
"os" | |
"path/filepath" | |
"strings" | |
"gopkg.in/yaml.v3" | |
) | |
type RawSnippet struct { | |
Name string `json:"name" yaml:"name"` | |
Label string `json:"label" yaml:"label"` | |
Description string `json:"description" yaml:"description"` | |
Body string `json:"body" yaml:"body"` | |
} | |
type Snippet struct { | |
Prefix string `json:"prefix"` | |
Body []string `json:"body"` | |
Description string `json:"description"` | |
} | |
func load(name string) *RawSnippet { | |
name = filepath.Join("snippets", name) | |
f, err := os.Open(filepath.Clean(name)) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
rawSnippet := &RawSnippet{} | |
err = yaml.NewDecoder(f).Decode(rawSnippet) | |
if err != nil { | |
panic(err) | |
} | |
return rawSnippet | |
} | |
func main() { | |
snippets := make(map[string]Snippet) | |
filepath.WalkDir("snippets", func(path string, d fs.DirEntry, err error) error { | |
if !d.IsDir() && strings.HasSuffix(d.Name(), ".yaml") { | |
rawSnippet := load(d.Name()) | |
if rawSnippet.Label == "" { | |
return nil | |
} | |
snippets[rawSnippet.Label] = Snippet{ | |
Prefix: rawSnippet.Label, | |
Description: rawSnippet.Description, | |
Body: strings.Split(rawSnippet.Body, "\n"), | |
} | |
} | |
return nil | |
}) | |
out, err := json.MarshalIndent(snippets, "", " ") | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("%s", out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment