Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compute file path tag #930

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"io/fs"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"

"github.com/pkg/errors"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -521,10 +523,24 @@ func removeExtension(path string) string {
return strings.TrimSuffix(path, filepath.Ext(path))
}

func getGitRemoteOriginURL(cwd ...string) string {
dir := "."
if len(cwd) > 0 {
dir = cwd[0]
}
cmd := exec.Command("git", "remote", "get-url", "--push", "origin")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
logrus.Warnf("unable to get git output: %s", err)
return ""
}
return strings.TrimSpace(string(out))
}

func applyTemplate(sourceFile io.Reader, commonTemplates fs.FS, dest afero.Fs, path string, overrides interface{}) error {
dir, _ := filepath.Split(path)
ospath := filepath.FromSlash(dir)
err := dest.MkdirAll(ospath, 0775)
dir := filepath.Dir(path)
err := dest.MkdirAll(dir, 0775)
if err != nil {
return errs.WrapUserf(err, "couldn't create %s directory", dir)
}
Expand All @@ -534,7 +550,14 @@ func applyTemplate(sourceFile io.Reader, commonTemplates fs.FS, dest afero.Fs, p
if err != nil {
return errs.WrapUser(err, "unable to open file")
}
t, e := util.OpenTemplate(path, sourceFile, commonTemplates)
t, e := util.OpenTemplate(path, sourceFile, commonTemplates, []template.FuncMap{
{
"cwd": func() string {
return filepath.Dir(writer.Name())
},
"git_origin": getGitRemoteOriginURL,
},
}...)
if e != nil {
return e
}
Expand Down
16 changes: 16 additions & 0 deletions apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ func init() {
logrus.SetFormatter(formatter)
}

type testGitRemote struct {
path string
expectedRemote string
}

func TestGetGitRemoveOriginURL(t *testing.T) {
r := require.New(t)
tests := []testGitRemote{
{path: ".", expectedRemote: "[email protected]:chanzuckerberg/fogg"},
}
for _, test := range tests {
remote := getGitRemoteOriginURL(test.path)
r.Equal(test.expectedRemote, remote)
}
}

func randomString(n int) string {
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

Expand Down
27 changes: 1 addition & 26 deletions config/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/chanzuckerberg/fogg/errs"
"github.com/chanzuckerberg/fogg/plugins"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
yaml "gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -54,29 +52,10 @@ func (c *Config) Write(fs afero.Fs, path string) error {
return encoder.Encode(c)
}

func getGitRemoteOriginURL(cwd ...string) string {
dir := "."
if len(cwd) > 0 {
dir = cwd[0]
}
cmd := exec.Command("git", "remote", "get-url", "--push", "origin")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
logrus.Warnf("unable to get git output: %s", err)
return ""
}
return strings.TrimSpace(string(out))
}

// TODO: write out functions to generate tag data
func (c *Config) GenerateStamp(fs afero.Fs, configFile string) {
stamp := Stamp{}
stamp.Date = DateAsTag()
stamp.FilePath = fs.Name()
stamp.FoggUser = ""
stamp.GitRepository = getGitRemoteOriginURL()
stamp.CommitHash = ""
c.Stamp = stamp
}

Expand All @@ -87,11 +66,7 @@ func DateAsTag() string {
}

type Stamp struct {
Date string
FilePath string
FoggUser string
GitRepository string
CommitHash string
FoggUser string
}

type Config struct {
Expand Down
16 changes: 0 additions & 16 deletions config/v2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

type testGitRemote struct {
path string
expectedRemote string
}

func TestGetGitRemoveOriginURL(t *testing.T) {
r := require.New(t)
tests := []testGitRemote{
{path: ".", expectedRemote: "[email protected]:chanzuckerberg/fogg"},
}
for _, test := range tests {
remote := getGitRemoteOriginURL(test.path)
r.Equal(test.expectedRemote, remote)
}
}

func TestReadConfig(t *testing.T) {
r := require.New(t)

Expand Down
4 changes: 0 additions & 4 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,11 +1287,7 @@ func resolveComponentCommon(stamp v2.Stamp, commons ...v2.Common) ComponentCommo
TravisCI: travisPlan,
CircleCI: circlePlan,
GitHubActionsCI: githubActionsPlan,
Date: stamp.Date,
FilePath: stamp.FilePath,
FoggUser: stamp.FoggUser,
GitRepository: stamp.GitRepository,
CommitHash: stamp.CommitHash,
}
}

Expand Down
20 changes: 9 additions & 11 deletions templates/templates/component/terraform/fogg.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,15 @@ variable "owner" {
variable "tags" {
type = object({project: string, env: string, service: string, owner: string, managedBy: string})
default = {
project = "{{ .Project }}"
env = "{{ .Env }}"
service = "{{ .Name }}"
owner = "{{ .Owner }}"
date = "{{ .Date }}"
filePath = "{{ .FilePath }}"
foggUser = "{{ .FoggUser }}"
gitRepository = "{{ .GitRepository }}"
commitHash = "{{ .CommitHash }}"
managedBy = "terraform"

project = "{{ .Project }}"
env = "{{ .Env }}"
service = "{{ .Name }}"
owner = "{{ .Owner }}"
terraformLastApplyTime = timestamp()
terraformWorkspaceDir = "{{ cwd }}"
foggUser = "{{ .FoggUser }}"
gitRepository = "{{ git_origin }}"
managedBy = "terraform"
}
}

Expand Down
19 changes: 9 additions & 10 deletions testdata/auth0_provider_yaml/terraform/accounts/foo/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/auth0_provider_yaml/terraform/envs/bar/bam/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/auth0_provider_yaml/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/bless_provider_yaml/terraform/accounts/foo/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/bless_provider_yaml/terraform/envs/bar/bam/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/bless_provider_yaml/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions testdata/circleci/terraform/global/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading