Skip to content

Commit

Permalink
switch from json to yaml and preserve top comments
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Ramlot <[email protected]>
  • Loading branch information
inteon committed Dec 15, 2023
1 parent 89d2300 commit 13f6542
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 35 deletions.
20 changes: 0 additions & 20 deletions example/klone.json

This file was deleted.

14 changes: 14 additions & 0 deletions example/klone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Comment on top of the file

targets:
projects:
- folder_name: kube_apis
repo_url: [email protected]:kubernetes/kubernetes.git
repo_ref: master
repo_hash: 56d7898510f2a973f92fda13c2ba3a5e756d9621
repo_path: api
- folder_name: kube_changelog
repo_url: [email protected]:kubernetes/kubernetes.git
repo_ref: master
repo_hash: 56d7898510f2a973f92fda13c2ba3a5e756d9621
repo_path: CHANGELOG
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/otiai10/copy v1.12.0
github.com/rogpeppe/go-internal v1.11.0
github.com/spf13/cobra v1.7.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
78 changes: 63 additions & 15 deletions pkg/mod/index.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
package mod

import (
"encoding/json"
"bufio"
"io"
"path/filepath"
"sort"
"strings"

"github.com/rogpeppe/go-internal/lockedfile"
"gopkg.in/yaml.v3"
)

const kloneFileName = "klone.json"
const kloneFileName = "klone.yaml"

type WorkDir string

type kloneFile struct {
Targets map[string]KloneFolder `json:"targets"`
Targets map[string]KloneFolder `yaml:"targets"`
}

type KloneFolder []KloneItem

type KloneItem struct {
FolderName string `json:"folder_name"`
KloneSource `json:",inline"`
FolderName string `yaml:"folder_name"`
KloneSource `yaml:",inline"`
}

type KloneSource struct {
RepoURL string `json:"repo_url"`
RepoRef string `json:"repo_ref"`
RepoHash string `json:"repo_hash"`
RepoPath string `json:"repo_path"`
RepoURL string `yaml:"repo_url"`
RepoRef string `yaml:"repo_ref"`
RepoHash string `yaml:"repo_hash"`
RepoPath string `yaml:"repo_path"`
}

func (w WorkDir) editKloneFile(fn func(*kloneFile) error) error {
Expand All @@ -44,7 +46,7 @@ func (w WorkDir) editKloneFile(fn func(*kloneFile) error) error {
index := kloneFile{}

// decode current contents of index file
if err := json.NewDecoder(file).Decode(&index); err != nil && err != io.EOF {
if err := yaml.NewDecoder(file).Decode(&index); err != nil && err != io.EOF {
return err
}

Expand All @@ -53,11 +55,57 @@ func (w WorkDir) editKloneFile(fn func(*kloneFile) error) error {
return err
}

file.Seek(0, 0)
file.Truncate(0)
topComments := ""

encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
{
// go back to the beginning of the file
if _, err := file.Seek(0, 0); err != nil {
return err
}

comments := strings.Builder{}

// read lines until the first non-comment line
reader := bufio.NewReader(file)
for {
line, isPrefix, err := reader.ReadLine()
if err != nil {
return err
}

if !isPrefix && (len(line) > 0 && line[0] != '#') {
break
}

if _, err := comments.Write(line); err != nil {
return err
}

if !isPrefix {
if _, err := comments.WriteRune('\n'); err != nil {
return err
}
}
}

topComments = comments.String()
}

// truncate file
if _, err := file.Seek(0, 0); err != nil {
return err
}
if err := file.Truncate(0); err != nil {
return err
}

// write comments
if _, err := file.WriteString(topComments); err != nil {
return err
}

encoder := yaml.NewEncoder(file)
encoder.SetIndent(2)

if err := encoder.Encode(index); err != nil {
return err
Expand All @@ -79,7 +127,7 @@ func (w WorkDir) readKloneFile() (*kloneFile, error) {
index := kloneFile{}

// decode current contents of index file
if err := json.NewDecoder(file).Decode(&index); err != nil && err != io.EOF {
if err := yaml.NewDecoder(file).Decode(&index); err != nil && err != io.EOF {
return nil, err
}

Expand Down

0 comments on commit 13f6542

Please sign in to comment.