The output failed to handle the year, resulting in some odd \x18 path generation.
In reviewing the values from returned from time.Now().Date, I realized it wasn’t an int value being returned.
To work through the definition, I figured this would be a good chance to use the cli only to find the docs.
1
go doc 'time.Now'
returned the following:
1
2
3
4
package time // import "time"
func Now() Time
Now returns the current local time.
To get the source code for the function:
go doc -src 'time.Now'
go doc -src 'time.Date'
This shows the return value of Date() is actually time type, not int.
Still couldn’t see where the multiple return parameters were defined so I ran:
1
2
go install golang.org/x/tools/cmd/godoc@latest
godoc --http 127.0.0.1:3030
Ok… Figured it out. I was looking at the func Date().
However, what I should have been looking at was the exported method func (Time) Date.
This correctly shows:
1
func (t Time) Date() (year int, month Month, day int)
I still couldn’t figure this out until I tried running it in the playground.
Strongly typed languages are awesome, but this type of behavior is not as easy to figure out coming from a background with dynamic languages. I
In contrast, PowerShell would be: Get-Date -Format 'yyyy'.
Here’s an example of the Mage command then to generate a blog post with a nice selection and prompt.
1
2
3
4
5
6
7
8
9
10
11
12
// calculatePostDir calculates the post directory based on the post title and the date.
funccalculatePostDir(titlestring)string{year,month,day:=time.Now().Date()str:=stringy.New(title)kebabTitle:=str.KebabCase().ToLower()slugTitle:=strings.Join(string(year),string(month),string(day),kebabTitle,"-")///stringy.ToKebabCase(title)
pterm.Success.Printf("Slugify Title: %s",slugTitle)filepath:=filepath.Join(contentDir,string(year),slugTitle)pterm.Success.Printf("calculatePostDir: %s",slugTitle)returnfilepath}
// New namespace groups the new post generatation commands.
typeNewmg.Namespace// NewPost creates a new post in the Hugo format.
func(New)Post()error{prompt:=promptui.Select{Label:"Select Type of Post j/k to navigate",Items:[]string{"100DaysOfCode","microblog","blog"},}_,result,err:=prompt.Run()iferr!=nil{pterm.Success.Printf("Prompt failed %v\n",err)returnerr}pterm.Success.Printf("New Post: [%s]",result)promptTitle:=promptui.Prompt{Label:"Enter Title",}title,err:=promptTitle.Run()iferr!=nil{pterm.Error.Printf("Prompt failed %v\n",err)returnerr}// the archetype in archtytpes directory to use
varkindstringswitchresult{case"100DaysOfCode":kind="code"default:kind=result}fileName:=calculatePostDir(title)iferr:=sh.RunV("hugo","new",fileName,"--kind",kind);err!=nil{returnerr}returnnil}
Webmentions