diff --git a/internal/bundle/prompt.go b/internal/bundle/prompt.go index 4dcb2b5a..c2c685d3 100644 --- a/internal/bundle/prompt.go +++ b/internal/bundle/prompt.go @@ -3,10 +3,12 @@ package bundle import ( "errors" "fmt" + "os" "regexp" "strings" "github.com/AlecAivazis/survey/v2" + "github.com/go-git/go-git/v5" "github.com/manifoldco/promptui" "github.com/massdriver-cloud/mass/internal/templatecache" "github.com/spf13/afero" @@ -70,6 +72,7 @@ var promptsNew = []func(t *templatecache.TemplateData) error{ getTemplate, GetConnections, getOutputDir, + getSourceUrl, } func RunPromptNew(t *templatecache.TemplateData) error { @@ -261,3 +264,27 @@ func getOutputDir(t *templatecache.TemplateData) error { t.OutputDir = result return nil } + +func getSourceUrl(t *templatecache.TemplateData) error { + // No actual prompt here - just check if we are in a github repo so we can automatically + // set the "source_url". Otherwise use a safe default. Ignore any errors since the user + // may not be in a git repo. + t.SourceURL = fmt.Sprintf("github.com/YOUR_ORGANIZATION/%s", t.Name) + dir, err := os.Getwd() + if err != nil { + return nil + } + repo, err := git.PlainOpen(dir) + if err != nil { + return nil + } + remote, err := repo.Remote("origin") + if err != nil { + return nil + } + url := remote.Config().URLs[0] + url = strings.Replace(url, "git@github.com:", "github.com/", 1) + t.SourceURL = strings.TrimSuffix(url, ".git") + + return nil +} diff --git a/internal/templatecache/template_cache.go b/internal/templatecache/template_cache.go index 1f61400b..b46111ee 100644 --- a/internal/templatecache/template_cache.go +++ b/internal/templatecache/template_cache.go @@ -12,6 +12,7 @@ type TemplateData struct { Description string `json:"description"` Access string `json:"access"` Location string `json:"location"` + SourceURL string `json:"source_url"` TemplateName string `json:"templateName"` TemplateRepo string `json:"templateRepo"` TemplateSource string `json:"templateSource"`