func watchFiles() (watcher *fsnotify.Watcher) { watcher, err := fsnotify.NewWatcher() utils.CheckError(err) changes := fsnotify.Write | fsnotify.Rename | fsnotify.Create needBuild := make(chan bool, 1) go func() { modified := false for { select { case event := <-watcher.Events: if event.Op&changes != 0 { modified = true } case <-time.After(time.Second * 1): if modified { modified = false needBuild <- true } case err := <-watcher.Errors: log.Println("error:", err) } } }() go func() { for { select { case build := <-needBuild: if build { log.Println("Rebuilding the project...") execute() } } } }() watchDirs := func(path string, info os.FileInfo, err error) error { stat, err := os.Stat(path) if err != nil { return err } if stat.IsDir() { watcher.Add(path) } return nil } err = filepath.Walk("template", watchDirs) utils.CheckError(err) watcher.Add(".") utils.CheckError(err) return }
func generateRss(channel Channel, items []Item) { imageUrl := utils.Urljoin(channel.Link, ASSETS_PATH, channel.Image) c := gopod.ChannelFactory(channel.Title, channel.Link, channel.Description, imageUrl) c.SetiTunesExplicit("No") c.SetCopyright(channel.Copyright) c.SetiTunesAuthor(channel.Author) c.SetiTunesSummary(channel.Description) c.SetCategory(strings.Join(channel.Categories, ",")) c.SetLanguage(channel.Language) for _, item := range items { url := utils.Urljoin(c.Link, ASSETS_PATH, item.Link) enclosure := gopod.Enclosure{ Url: url, Length: "0", Type: "audio/mpeg", } pubdate, err := time.Parse(time.RFC3339, item.Pubdate) utils.CheckError(err) var guid string description := truncate(item.Description) if item.Guid != "" { guid = item.Guid } else { generated_guid = true guid = uuid.NewV4().String() log.Printf("%s - guid: %s\n", item.Link, guid) } episode := gopod.Item{ Title: item.Title, Link: url, Description: description, PubDate: pubdate.Format(time.RFC1123), Guid: guid, TunesAuthor: channel.Author, TunesSubtitle: description, TunesSummary: description, Enclosure: []*gopod.Enclosure{&enclosure}, } episode.SetTunesImage(utils.Urljoin(channel.Link, ASSETS_PATH, item.Image)) c.AddItem(&episode) } f, err := os.Create(fmt.Sprintf("%s/%s", TARGET_PATH, "rss.xml")) utils.CheckError(err) defer f.Close() f.Write(c.Publish()) }
func getOriginUrl() string { originUrl, err := gitconfig.OriginURL() utils.CheckError(err) if !strings.HasPrefix(originUrl, "git@") { log.Printf("Please clone the repo with SSL clone URL. Otherwise the repo cannot be modified (Origin url is %s in .git/config)\n", originUrl) os.Exit(-1) } return originUrl }
func generatePages() { channel := getChannelData("channel.yml") items := getItemData("items.yml") current := items[0] chopped_items := chopItems(items, channel.Page) len_chopped_items := len(chopped_items) generateRss(channel, items) pages := make([]int, len_chopped_items) for i := 1; i <= len_chopped_items; i++ { pages[i-1] = i } data, _ := ioutil.ReadFile(fmt.Sprintf("%s/index.tmpl", TEMPLATE_PATH)) content := string(data[:]) funcs := template.FuncMap{"alt": alt, "trunc": truncate} t := template.Must(template.New("Podgen").Funcs(funcs).Parse(content)) for i := 1; i <= len_chopped_items; i++ { var filename string var home string if i == 1 { filename = "index.html" home = "#current" } else { filename = fmt.Sprintf("page%d.html", i) home = "index.html" } f, err := os.Create(fmt.Sprintf("%s/%s", TARGET_PATH, filename)) utils.CheckError(err) defer f.Close() err = t.Execute(f, PageTemplate{ Info: channel, Home: home, Current: current, Podcasts: chopped_items[i-1], Paginator: generatePaginator(i, len_chopped_items), }) utils.CheckError(err) } }
- title: change_me link: chang_me.mp3 image: change_me.png description: > change_me pubdate: %s guid: %s ` ) var newCmd = &cobra.Command{ Use: "new", Short: "Generate a new item into items.yml", Long: "Create a new episode from the template, appending it to items.yml. Please edit that file", Run: func(cmd *cobra.Command, args []string) { f, err := os.OpenFile("items.yml", os.O_APPEND|os.O_WRONLY, 0600) utils.CheckError(err) defer f.Close() guid := uuid.NewV4().String() pubdate := time.Now().Format(time.RFC3339) content := fmt.Sprintf(ITEM_TEMPLATE, pubdate, guid) _, err = f.WriteString(content) utils.CheckError(err) log.Println("Item info generated. Please modify items.yml.") }, }