-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from suba327777/feat/svg-link
Feat/svg link
- Loading branch information
Showing
4 changed files
with
100 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func WriteSvgFile(svg []byte) error { | ||
directory := "./mountain-output" | ||
filePath := directory + "/mountain.svg" | ||
|
||
if _, err := os.Stat(directory); os.IsNotExist(err) { | ||
err := os.Mkdir(directory, 0o755) | ||
if err != nil { | ||
return fmt.Errorf("Error creating directory: %v\n", err) | ||
} | ||
} | ||
|
||
svgFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666) | ||
if err != nil { | ||
return fmt.Errorf("Error opening mountain.svg: %v\n", err) | ||
} | ||
defer svgFile.Close() | ||
|
||
_, err = svgFile.Write(svg) | ||
if err != nil { | ||
return fmt.Errorf("Error writing to SVG file: %v\n", err) | ||
} | ||
|
||
fmt.Println("SVG has been saved successfully.") | ||
return nil | ||
} | ||
|
||
func WriteReadmeFile() error { | ||
directory := "./mountain-output" | ||
readmePath := directory + "/README.md" | ||
|
||
readmeFile, err := os.OpenFile(readmePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666) | ||
if err != nil { | ||
return fmt.Errorf("Error opening README.md file: %v\n", err) | ||
} | ||
defer readmeFile.Close() | ||
|
||
username := os.Getenv("USERNAME") | ||
theme := os.Getenv("THEME") | ||
branch, err := getHeadBranch() | ||
if err != nil { | ||
return fmt.Errorf("Error getting HEAD branch: %v\n", err) | ||
} | ||
|
||
startReadme := fmt.Sprintf("## %s\n\n", theme) | ||
showSvg := "![](./mountain.svg)\n\n" | ||
usageText := "### Now you can add this to your markdown\n" | ||
startCodeBlock := "```\n" | ||
inCodeBlock := fmt.Sprintf(`<img src="https://raw.githubusercontent.com/%s/%s/%s/mountain-output/mountain.svg" />`, username, username, branch) | ||
endCodeBlock := "\n```" | ||
readmeContent := startReadme + showSvg + usageText + startCodeBlock + inCodeBlock + endCodeBlock | ||
|
||
fmt.Println(readmeContent) | ||
|
||
_, err = readmeFile.Write([]byte(readmeContent)) | ||
if err != nil { | ||
return fmt.Errorf("Error writing to README.md file: %v\n", err) | ||
} | ||
|
||
fmt.Println("README has been saved successfully.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func getHeadBranch() (string, error) { | ||
cmd := exec.Command("git", "remote", "show", "origin") | ||
output, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
lines := strings.Split(string(output), "\n") | ||
for _, line := range lines { | ||
if strings.Contains(line, "HEAD branch:") { | ||
parts := strings.Split(line, ":") | ||
if len(parts) == 2 { | ||
return strings.TrimSpace(parts[1]), nil | ||
} | ||
} | ||
} | ||
return "", fmt.Errorf("HEAD branch not found") | ||
} |