Skip to content

Commit

Permalink
Merge pull request #60 from AlayaCare/indentation
Browse files Browse the repository at this point in the history
change yaml indentation to 2 spaces
  • Loading branch information
nzin-alayacare authored Sep 16, 2023
2 parents 7ac4f86 + d78d554 commit e762ad1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 52 deletions.
22 changes: 18 additions & 4 deletions internal/engine/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions internal/entity/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
82 changes: 37 additions & 45 deletions internal/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}

}
Expand All @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit e762ad1

Please sign in to comment.