Skip to content

Commit

Permalink
Modify KEPify JSON output
Browse files Browse the repository at this point in the history
This makes the JSON output of the kepify tool a bit easier parsable,
since we now marshal into an array of objects. Beside this, we add
the KEP ID (hashed value) as `ID` field to each proposal as well.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Jan 24, 2020
1 parent 716269a commit c28a668
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 49 deletions.
41 changes: 8 additions & 33 deletions cmd/kepify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package main

import (
"crypto/md5"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -135,40 +135,15 @@ func printJSONOutput(filePath string, proposals keps.Proposals) error {
total := len(proposals)
fmt.Printf("Total KEPs: %d\n", total)

fmt.Fprintln(file, "{")
for i, kep := range proposals {
fmt.Fprintf(file, "\t\"%s\": {\n", hash(kep.OwningSIG+":"+kep.Title))
fmt.Fprintf(file, "\t\t\"%s\": \"%s\",\n", "title", kep.Title)
fmt.Fprintf(file, "\t\t\"%s\": \"%s\",\n", "owning-sig", kep.OwningSIG)
fmt.Fprintf(file, "\t\t\"%s\": %s,\n", "participating-sigs", marshal(kep.ParticipatingSIGs))
fmt.Fprintf(file, "\t\t\"%s\": %s,\n", "reviewers", marshal(kep.Reviewers))
fmt.Fprintf(file, "\t\t\"%s\": %s,\n", "authors", marshal(kep.Authors))
fmt.Fprintf(file, "\t\t\"%s\": \"%s\",\n", "editor", kep.Editor)
fmt.Fprintf(file, "\t\t\"%s\": \"%s\",\n", "creation-date", kep.CreationDate)
fmt.Fprintf(file, "\t\t\"%s\": \"%s\",\n", "last-updated", kep.LastUpdated)
fmt.Fprintf(file, "\t\t\"%s\": \"%s\",\n", "status", kep.Status)
fmt.Fprintf(file, "\t\t\"%s\": %s,\n", "see-also", marshal(kep.SeeAlso))
fmt.Fprintf(file, "\t\t\"%s\": %s,\n", "replaces", marshal(kep.Replaces))
fmt.Fprintf(file, "\t\t\"%s\": %s,\n", "superseded-by", marshal(kep.SupersededBy))
contents, _ := json.Marshal(kep.Contents)
fmt.Fprintf(file, "\t\t\"%s\": %s\n", "markdown", contents)
if i < total-1 {
fmt.Fprintln(file, "\t},")
} else {
fmt.Fprintln(file, "\t}")
}
data, err := json.Marshal(proposals)
if err != nil {
return err
}
if err := ioutil.WriteFile(filePath, data, 0755); err != nil {
return err
}
fmt.Fprintln(file, "}")
return nil
}

func marshal(array []string) string {
contents, _ := json.Marshal(array)
return string(contents)
}

func hash(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
return nil
}

/// ignore certain files in the keps/ subdirectory
Expand Down
40 changes: 24 additions & 16 deletions pkg/kepval/keps/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package keps
import (
"bufio"
"bytes"
"crypto/md5"
"fmt"
"io"
"strings"

Expand All @@ -34,23 +36,24 @@ func (p *Proposals) AddProposal(proposal *Proposal) {
}

type Proposal struct {
Title string `yaml:"title"`
Authors []string `yaml:,flow`
OwningSIG string `yaml:"owning-sig"`
ParticipatingSIGs []string `yaml:"participating-sigs",flow,omitempty`
Reviewers []string `yaml:,flow`
Approvers []string `yaml:,flow`
Editor string `yaml:"editor,omitempty"`
CreationDate string `yaml:"creation-date"`
LastUpdated string `yaml:"last-updated"`
Status string `yaml:"status"`
SeeAlso []string `yaml:"see-also,omitempty"`
Replaces []string `yaml:"replaces,omitempty"`
SupersededBy []string `yaml:"superseded-by,omitempty"`
ID string `json:"id"`
Title string `json:"title" yaml:"title"`
Authors []string `json:"authors" yaml:",flow"`
OwningSIG string `json:"owningSig" yaml:"owning-sig"`
ParticipatingSIGs []string `json:"participatingSigs" yaml:"participating-sigs,flow,omitempty"`
Reviewers []string `json:"reviewers" yaml:",flow"`
Approvers []string `json:"approvers" yaml:",flow"`
Editor string `json:"editor" yaml:"editor,omitempty"`
CreationDate string `json:"creationDate" yaml:"creation-date"`
LastUpdated string `json:"lastUpdated" yaml:"last-updated"`
Status string `json:"status" yaml:"status"`
SeeAlso []string `json:"seeAlso" yaml:"see-also,omitempty"`
Replaces []string `json:"replaces" yaml:"replaces,omitempty"`
SupersededBy []string `json:"supersededBy" yaml:"superseded-by,omitempty"`

Filename string `yaml:"-"`
Error error `yaml:"-"`
Contents string `yaml:"-"`
Filename string `json:"-" yaml:"-"`
Error error `json:"-" yaml:"-"`
Contents string `json:"markdown" yaml:"-"`
}

type Parser struct{}
Expand Down Expand Up @@ -92,5 +95,10 @@ func (p *Parser) Parse(in io.Reader) *Proposal {
}

proposal.Error = yaml.UnmarshalStrict(metadata, proposal)
proposal.ID = hash(proposal.OwningSIG + ":" + proposal.Title)
return proposal
}

func hash(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}

0 comments on commit c28a668

Please sign in to comment.