From 1e0c67afe4f15d8e33a220dd58fdc546f5f47bc1 Mon Sep 17 00:00:00 2001 From: sosolyht Date: Mon, 8 May 2023 17:58:18 +0900 Subject: [PATCH] feat: support path --- README.md | 4 ++-- examples.go | 1 - examples/default-sitemap/main.go | 15 ++++++++++++ main.go => examples/video-sitemap/main.go | 16 ++----------- sitemap/required.go | 7 ------ sitemap/sitemap.go | 29 ++++++++++++++++++++++- sitemap/video_sitemap.go | 29 ++++++++++++++++++++++- sitemaps/links | 5 ---- sitemaps/sitemap.xml | 4 ++-- 9 files changed, 77 insertions(+), 33 deletions(-) delete mode 100644 examples.go create mode 100644 examples/default-sitemap/main.go rename main.go => examples/video-sitemap/main.go (90%) delete mode 100644 sitemap/required.go delete mode 100644 sitemaps/links diff --git a/README.md b/README.md index b61c08f..16be2c4 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ package main import "github.com/sosolyht/go-sitemap/sitemap" func main() { -// Create a standard sitemap -s := sitemap.NewSitemap() +// Create a sitemap with path +s := sitemap.NewSitemap().Path("util/sitemaps") links := []string{ "https://google.com", diff --git a/examples.go b/examples.go deleted file mode 100644 index 06ab7d0..0000000 --- a/examples.go +++ /dev/null @@ -1 +0,0 @@ -package main diff --git a/examples/default-sitemap/main.go b/examples/default-sitemap/main.go new file mode 100644 index 0000000..e515a71 --- /dev/null +++ b/examples/default-sitemap/main.go @@ -0,0 +1,15 @@ +package main + +import "github.com/sosolyht/go-sitemap/sitemap" + +func main() { + s := sitemap.NewSitemap().Path("sitemaps") + + links := []string{ + "https://google.com", + "https://naver.com", + } + for i := range links { + s.AddURL(links[i]) + } +} diff --git a/main.go b/examples/video-sitemap/main.go similarity index 90% rename from main.go rename to examples/video-sitemap/main.go index 5c891a9..b37537e 100644 --- a/main.go +++ b/examples/video-sitemap/main.go @@ -1,21 +1,9 @@ package main -import ( - "github.com/sosolyht/go-sitemap/sitemap" -) +import "github.com/sosolyht/go-sitemap/sitemap" func main() { - s := sitemap.NewSitemap() - - links := []string{ - "https://google.com", - "https://naver.com", - } - for i := range links { - s.AddURL(links[i]) - } - - vs := sitemap.NewVideoSitemap() + vs := sitemap.NewVideoSitemap().Path("sitemaps") videoURLs := []sitemap.VideoURL{ { diff --git a/sitemap/required.go b/sitemap/required.go deleted file mode 100644 index b308e87..0000000 --- a/sitemap/required.go +++ /dev/null @@ -1,7 +0,0 @@ -package sitemap - -type URL interface { - WithLoc(loc string) URL - WithChangeFreq(freq ChangeFrequency) URL - Do() *URLs -} diff --git a/sitemap/sitemap.go b/sitemap/sitemap.go index f089c69..f79e955 100644 --- a/sitemap/sitemap.go +++ b/sitemap/sitemap.go @@ -3,8 +3,10 @@ package sitemap import ( "encoding/xml" "io" + "log" "net/http" "os" + "path/filepath" "strings" "time" ) @@ -13,6 +15,7 @@ type sitemap struct { XMLName xml.Name `xml:"urlset"` Xmlns string `xml:"xmlns,attr"` URL []URLs `xml:"url,omitempty"` + path string } type URLs struct { @@ -60,7 +63,7 @@ func (s *sitemap) AddURL(url string) (err error) { return err } - sitemapFile, err := os.Create("sitemaps/sitemap.xml") + sitemapFile, err := os.Create(s.path) if err != nil { return err } @@ -78,6 +81,30 @@ func (s *sitemap) AddURL(url string) (err error) { return } +func (s *sitemap) Path(path string) *sitemap { + currentDir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + projectRoot := filepath.Join(currentDir, "..", "..") + sitemapsDir := filepath.Join(projectRoot, path) + + _, err = os.Stat(sitemapsDir) + if err != nil { + if os.IsNotExist(err) { + err = os.MkdirAll(sitemapsDir, 0755) + if err != nil { + log.Fatal(err) + } + } + } + + output := filepath.Join(sitemapsDir, "sitemap.xml") + s.path = output + return s +} + func (s *sitemap) createSitemapFromLinksFile() ([]string, error) { linkFile, err := os.Open("sitemaps/links") if err != nil { diff --git a/sitemap/video_sitemap.go b/sitemap/video_sitemap.go index d06b9e4..73a7aec 100644 --- a/sitemap/video_sitemap.go +++ b/sitemap/video_sitemap.go @@ -2,7 +2,9 @@ package sitemap import ( "encoding/xml" + "log" "os" + "path/filepath" "time" ) @@ -11,6 +13,7 @@ type videoSitemap struct { Xmlns string `xml:"xmlns,attr"` XmlnsVideo string `xml:"xmlns:video,attr"` URL []VideoURL + path string } type VideoURL struct { @@ -67,7 +70,7 @@ func (v *videoSitemap) AddVideoURL(url VideoURL) (err error) { return err } - sitemapFile, err := os.Create("sitemaps/sitemap_video.xml") + sitemapFile, err := os.Create(v.path) if err != nil { return err } @@ -84,3 +87,27 @@ func (v *videoSitemap) AddVideoURL(url VideoURL) (err error) { return } + +func (v *videoSitemap) Path(path string) *videoSitemap { + currentDir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + projectRoot := filepath.Join(currentDir, "..", "..") + sitemapsDir := filepath.Join(projectRoot, path) + + _, err = os.Stat(sitemapsDir) + if err != nil { + if os.IsNotExist(err) { + err = os.MkdirAll(sitemapsDir, 0755) + if err != nil { + log.Fatal(err) + } + } + } + + output := filepath.Join(sitemapsDir, "sitemap_video.xml") + v.path = output + return v +} diff --git a/sitemaps/links b/sitemaps/links deleted file mode 100644 index c8cdd7a..0000000 --- a/sitemaps/links +++ /dev/null @@ -1,5 +0,0 @@ -https://google.com -https://google.com/test -https://google.com/test1 -https://google.com/test2 -https://google.com/test3 \ No newline at end of file diff --git a/sitemaps/sitemap.xml b/sitemaps/sitemap.xml index 9cef8ee..73cfa6c 100644 --- a/sitemaps/sitemap.xml +++ b/sitemaps/sitemap.xml @@ -2,10 +2,10 @@ https://google.com - 2023-04-28 + 2023-05-08 https://naver.com - 2023-04-28 + 2023-05-08 \ No newline at end of file