diff --git a/config/v2/config.go b/config/v2/config.go index 83d90b8c9..7cc38045d 100644 --- a/config/v2/config.go +++ b/config/v2/config.go @@ -4,12 +4,14 @@ 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" ) @@ -52,6 +54,32 @@ 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 +} + // TODO: write out functions to generate tag data func (c *Config) GenerateStamp(fs afero.Fs, configFile string) { stamp := Stamp{} diff --git a/config/v2/config_test.go b/config/v2/config_test.go index 9b5aea6b6..8918d7686 100644 --- a/config/v2/config_test.go +++ b/config/v2/config_test.go @@ -8,6 +8,22 @@ 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: "git@github.com:chanzuckerberg/fogg"}, + } + for _, test := range tests { + remote := getGitRemoteOriginURL(test.path) + r.Equal(test.expectedRemote, remote) + } +} + func TestReadConfig(t *testing.T) { r := require.New(t)