From d78d554eccda2778cfcdc6541f4ee361c6987743 Mon Sep 17 00:00:00 2001 From: Nicolas Zin Date: Sat, 16 Sep 2023 15:55:39 -0400 Subject: [PATCH] change yaml indentation to 2 spaces --- internal/engine/local.go | 22 +++++++++-- internal/entity/team.go | 12 ++++-- internal/scaffold.go | 82 ++++++++++++++++++---------------------- 3 files changed, 64 insertions(+), 52 deletions(-) diff --git a/internal/engine/local.go b/internal/engine/local.go index 3dd6c12..67fb558 100644 --- a/internal/engine/local.go +++ b/internal/engine/local.go @@ -445,11 +445,18 @@ func syncUsersViaUserPlugin(repoconfig *config.RepositoryConfig, fs afero.Fs, us // check if user changed if !newuser.Equals(user) { // changed user - newContent, err := yaml.Marshal(newuser) + file, err := fs.Create(filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username))) + if err != nil { + return nil, nil, err + } + defer file.Close() + + encoder := yaml.NewEncoder(file) + encoder.SetIndent(2) + err = encoder.Encode(newuser) if err != nil { return nil, nil, err } - afero.WriteFile(fs, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username)), newContent, 0644) updatedusers = append(updatedusers, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username))) } @@ -458,11 +465,18 @@ func syncUsersViaUserPlugin(repoconfig *config.RepositoryConfig, fs afero.Fs, us } for username, user := range newOrgUsers { // new user - newContent, err := yaml.Marshal(user) + file, err := fs.Create(filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username))) + if err != nil { + return nil, nil, err + } + defer file.Close() + + encoder := yaml.NewEncoder(file) + encoder.SetIndent(2) + err = encoder.Encode(user) if err != nil { return nil, nil, err } - afero.WriteFile(fs, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username)), newContent, 0644) updatedusers = append(updatedusers, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username))) } return deletedusers, updatedusers, nil diff --git a/internal/entity/team.go b/internal/entity/team.go index c2ef20b..08bdbfd 100644 --- a/internal/entity/team.go +++ b/internal/entity/team.go @@ -203,11 +203,17 @@ func (t *Team) Update(fs afero.Fs, filename string, users map[string]*User) (boo } t.Spec.Members = members - yamlTeam, err := yaml.Marshal(t) + file, err := fs.Create(filename) if err != nil { - return changed, err + return changed, fmt.Errorf("Not able to create file %s: %v", filename, err) } - err = afero.WriteFile(fs, filename, yamlTeam, 0644) + defer file.Close() + encoder := yaml.NewEncoder(file) + encoder.SetIndent(2) + err = encoder.Encode(t) + if err != nil { + return changed, fmt.Errorf("Not able to write file %s: %v", filename, err) + } return changed, err } diff --git a/internal/scaffold.go b/internal/scaffold.go index 06c29a4..9a61c57 100644 --- a/internal/scaffold.go +++ b/internal/scaffold.go @@ -166,15 +166,9 @@ func (s *Scaffold) generateTeams(fs afero.Fs, teamspath string, usermap map[stri // put the right user name instead of the github id lTeam.Spec.Owners = append(lTeam.Spec.Owners, usermap[m]) } - out, err := yaml.Marshal(&lTeam) - - if err == nil { - fs.MkdirAll(path.Join(teamspath, team), 0755) - if err := writeFile(path.Join(teamspath, team, "team.yaml"), out, fs); err != nil { - logrus.Error(err) - } - } else { - logrus.Errorf("not able to marshall team %s", team) + fs.MkdirAll(path.Join(teamspath, team), 0755) + if err := writeYamlFile(path.Join(teamspath, team, "team.yaml"), &lTeam, fs); err != nil { + logrus.Errorf("not able to write team file %s: %v", team, err) } // write repos @@ -200,13 +194,8 @@ func (s *Scaffold) generateTeams(fs afero.Fs, teamspath string, usermap map[stri break } } - out, err := yaml.Marshal(&lRepo) - if err == nil { - if err := writeFile(path.Join(teamspath, team, r+".yaml"), out, fs); err != nil { - logrus.Error(err) - } - } else { - logrus.Errorf("not able to marshall repo %s", r) + if err := writeYamlFile(path.Join(teamspath, team, r+".yaml"), &lRepo, fs); err != nil { + logrus.Errorf("not able to write repo file %s/%s.yaml: %v", team, r, err) } } } @@ -231,15 +220,9 @@ func (s *Scaffold) generateTeams(fs afero.Fs, teamspath string, usermap map[stri // put the right user name instead of the github id lTeam.Spec.Owners = append(lTeam.Spec.Owners, usermap[m]) } - out, err := yaml.Marshal(&lTeam) - - if err == nil { - fs.MkdirAll(path.Join(teamspath, team), 0755) - if err := writeFile(path.Join(teamspath, team, "team.yaml"), out, fs); err != nil { - logrus.Error(err) - } - } else { - logrus.Errorf("not able to marshall team %s", team) + fs.MkdirAll(path.Join(teamspath, team), 0755) + if err := writeYamlFile(path.Join(teamspath, team, "team.yaml"), &lTeam, fs); err != nil { + logrus.Errorf("not able to write team file %s/team.yaml: %v", team, err) } } @@ -264,13 +247,8 @@ func (s *Scaffold) generateUsers(fs afero.Fs, userspath string) (map[string]stri logrus.Debug("SAML integration enabled") for username, user := range users { usermap[user.Spec.GithubID] = username - out, err := yaml.Marshal(&user) - if err == nil { - if err := writeFile(path.Join(userspath, "org", username+".yaml"), out, fs); err != nil { - logrus.Error(err) - } - } else { - logrus.Errorf("Not able to marshal user %s: %v", username, err) + if err := writeYamlFile(path.Join(userspath, "org", username+".yaml"), &user, fs); err != nil { + logrus.Errorf("Not able to write user file org/%s.yaml: %v", username, err) } } } else { @@ -284,13 +262,8 @@ func (s *Scaffold) generateUsers(fs afero.Fs, userspath string) (map[string]stri user.Name = githubid user.Spec.GithubID = githubid - out, err := yaml.Marshal(&user) - if err == nil { - if err := writeFile(path.Join(userspath, "org", githubid+".yaml"), out, fs); err != nil { - logrus.Error(err) - } - } else { - logrus.Errorf("Not able to marshal user %s: %v", githubid, err) + if err := writeYamlFile(path.Join(userspath, "org", githubid+".yaml"), user, fs); err != nil { + logrus.Errorf("Not able to write user file org/%s.yaml: %v", githubid, err) } } } @@ -450,15 +423,34 @@ You can archive a repository, by a PR that move the yaml repository file into th return nil } +// helper function to write a yaml file (with 2 spaces indentation) +func writeYamlFile(filename string, in interface{}, fs afero.Fs) error { + file, err := fs.Create(filename) + if err != nil { + return fmt.Errorf("Not able to create file %s: %v", filename, err) + } + defer file.Close() + + encoder := yaml.NewEncoder(file) + encoder.SetIndent(2) + err = encoder.Encode(in) + if err != nil { + return fmt.Errorf("Not able to write to file %s: %v", filename, err) + } + return nil +} + +// helper function to write a file func writeFile(filename string, content []byte, fs afero.Fs) error { file, err := fs.Create(filename) - if err == nil { - _, err := file.Write(content) - if err != nil { - return fmt.Errorf("Not able to write to file %s: %v", filename, err) - } - } else { + if err != nil { return fmt.Errorf("Not able to create file %s: %v", filename, err) } + defer file.Close() + + _, err = file.Write(content) + if err != nil { + return fmt.Errorf("Not able to write to file %s: %v", filename, err) + } return nil }