Skip to content

Commit

Permalink
add cli flag to render remote template directly (#1)
Browse files Browse the repository at this point in the history
* add cli flag to render remote template directly

* add releaser action

---------

Co-authored-by: euforic <[email protected]>
  • Loading branch information
euforic and euforic authored Sep 14, 2023
1 parent 22854e5 commit c215566
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Release Go project
on:
push:
tags:
- "*" # triggers only if push new tag version, like `0.8.4` or else
jobs:
build:
name: GoReleaser build
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4
with:
fetch-depth: 0 # See: https://goreleaser.com/ci/actions/
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21
id: go
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
workdir: ./cmd/templit
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22 changes: 22 additions & 0 deletions cmd/templit/templit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var flagValues = struct {
token string
branch string
remote string
}{}

// templitCmd represents the templit command
Expand Down Expand Up @@ -82,6 +83,26 @@ var renderCmd = &cobra.Command{
maps.Copy(funcMap, templit.DefaultFuncMap)
executor.Funcs(funcMap)

// If a remote repository is specified, process the template and write it to the output directory
if flagValues.remote != "" {
importParts, err := templit.ParseDepURL(flagValues.remote)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing remote: %s\n", err)
return
}

importParts.Path = inputPath

if _, err := executor.ImportFunc(outputPath)(importParts.String(), "./", values); err != nil {
fmt.Fprintf(os.Stderr, "Error processing template: %s\n", err)
}
return
}

// Copy the default function map from the templit package
maps.Copy(funcMap, templit.DefaultFuncMap)
executor.Funcs(funcMap)

// Process the templates in the input directory and write them to the output directory
if err := executor.WalkAndProcessDir(inputPath, outputPath, values); err != nil {
fmt.Fprintf(os.Stderr, "Error processing template: %s\n", err)
Expand All @@ -93,6 +114,7 @@ func init() {
templitCmd.AddCommand(renderCmd)
renderCmd.Flags().StringVarP(&flagValues.token, "git_token", "t", "", "GitHub token")
renderCmd.Flags().StringVarP(&flagValues.branch, "branch", "b", "main", "GitHub branch")
renderCmd.Flags().StringVarP(&flagValues.remote, "remote", "r", "", "remote repository to use. (example: github.com/owner/repo@ref)")
}

// main is the entrypoint of the application
Expand Down
50 changes: 40 additions & 10 deletions dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ type DepInfo struct {
Tag string
}

// String returns the string representation of a DepInfo.
func (d DepInfo) String() string {
var builder strings.Builder

builder.WriteString(d.Host)
builder.WriteRune('/')
builder.WriteString(d.Owner)
builder.WriteRune('/')
builder.WriteString(d.Repo)

if d.Path != "" {
builder.WriteRune('/')
builder.WriteString(d.Path)
}

if d.Block != "" {
builder.WriteRune('#')
builder.WriteString(d.Block)
}

if d.Tag != "" {
builder.WriteRune('@')
builder.WriteString(d.Tag)
}

return builder.String()
}

// ParseDepURL is a parsed embed URL.
func ParseDepURL(rawURL string) (*DepInfo, error) {
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
Expand All @@ -31,8 +59,15 @@ func ParseDepURL(rawURL string) (*DepInfo, error) {
return nil, err
}

// Extract the tag if it exists before splitting the path
fullPath := strings.Trim(u.Path, "/")
tag := ""
if idx := strings.Index(fullPath, "@"); idx != -1 {
fullPath, tag = splitAtSign(fullPath)
}

// Split path into components
pathParts := strings.Split(strings.Trim(u.Path, "/"), "/")
pathParts := strings.Split(fullPath, "/")
if len(pathParts) < 2 {
return nil, fmt.Errorf("invalid path format in embed URL")
}
Expand All @@ -44,15 +79,10 @@ func ParseDepURL(rawURL string) (*DepInfo, error) {
path = strings.Join(pathParts[2:], "/")
}

// Extract block and version
block, tag := extractBlockAndTag(u.Fragment)

if tag == "" {
if strings.Contains(path, "@") {
path, tag = splitAtSign(path)
} else if strings.Contains(repo, "@") {
repo, tag = splitAtSign(repo)
}
// If fragment contains the tag, then prioritize it over the tag in the path
block, fragmentTag := extractBlockAndTag(u.Fragment)
if fragmentTag != "" {
tag = fragmentTag
}

return &DepInfo{
Expand Down

0 comments on commit c215566

Please sign in to comment.