Skip to content

Instantly share code, notes, and snippets.

@tony612
Created May 6, 2022 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tony612/6af5d4afdb0824a98402e51ba9d3338e to your computer and use it in GitHub Desktop.
Save tony612/6af5d4afdb0824a98402e51ba9d3338e to your computer and use it in GitHub Desktop.
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