Skip to content

Commit

Permalink
Merge pull request #58 from suba327777/feat/svg-link
Browse files Browse the repository at this point in the history
Feat/svg link
  • Loading branch information
suba327777 authored Jul 4, 2024
2 parents 67e19d8 + db4913b commit 05836ee
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
27 changes: 5 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"os"

"github-readme-mountain/api"
"github-readme-mountain/utils"
)

func main() {
Expand All @@ -14,31 +14,14 @@ func main() {
return
}

// ファイルパスを設定
directory := "./mountain-output"
filePath := "./mountain-output/mountain.svg"

// ディレクトリが存在しない場合は作成
if _, err := os.Stat(directory); os.IsNotExist(err) {
err := os.Mkdir(directory, 0o755)
if err != nil {
fmt.Printf("Error creating directory: %v\n", err)
return
}
}

file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666)
err = utils.WriteSvgFile(svg)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
fmt.Printf("Error calling WriteSvgFile: %v\n", err)
return
}
defer file.Close()

_, err = file.Write(svg)
err = utils.WriteReadmeFile()
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
return
fmt.Printf("Error calling WriteReadmeFile: %v\n", err)
}

fmt.Println("SVG has been saved successfully.")
}
2 changes: 1 addition & 1 deletion ui/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func startSVG() string {
return fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width="340" height="200" viewBox="0 0 340 200" >`)
return `<svg xmlns="http://www.w3.org/2000/svg" width="340" height="200" viewBox="0 0 340 200" >`
}

func rect(bgColor, borderColor string) string {
Expand Down
68 changes: 68 additions & 0 deletions utils/file_writer.go
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
}
26 changes: 26 additions & 0 deletions utils/git.go
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")
}

0 comments on commit 05836ee

Please sign in to comment.