Skip to content

Commit

Permalink
Merge pull request #50 from goark/develop
Browse files Browse the repository at this point in the history
Fix options for calendar command
  • Loading branch information
spiegel-im-spiegel authored Jun 2, 2024
2 parents 95d0760 + 639b646 commit 1324a26
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,14 @@ Available Commands:
post Post astronomical calendar data to TL
Flags:
--eclipse output eclipse
--end string end of date (YYYY-MM-DD)
--ephemeris output ephemeris
--ephemeris-all output all ephemeris
-h, --help help for calendar
--holiday output holiday
--moon-phase output moon-phase
--planet output planet
--solar-term output solar-term
--start string start of date (YYYY-MM-DD)
--template string template file for Output format
Expand Down
54 changes: 44 additions & 10 deletions calendar/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ const defaultTemplate = `{{ range . }}{{ .Date }} {{ .Title }}

// Config type is configurations for calendar package.
type Config struct {
start, end value.DateJp
holiday, ephemeris bool
tempDir *tempdir.TempDir
templateFile string
start, end value.DateJp
holiday bool
moonPhase bool
solarTerm bool
eclipse bool
planet bool
tempDir *tempdir.TempDir
templateFile string
}

// NewConfig function creates new Config instance.
func NewConfig(start, end string, holiday, ephemeris bool, tempDir *tempdir.TempDir, templateFile string) (*Config, error) {
func NewConfig(start, end string, holiday, moonPhase, solarTerm, eclipse, planet bool, tempDir *tempdir.TempDir, templateFile string) (*Config, error) {
startdate := value.NewDate(time.Time{})
enddate := value.NewDate(time.Time{})
if len(start) == 0 && len(end) == 0 {
Expand All @@ -38,14 +42,32 @@ func NewConfig(start, end string, holiday, ephemeris bool, tempDir *tempdir.Temp
if len(start) > 0 {
dt, err := value.DateFrom(start)
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("start", start), errs.WithContext("end", end), errs.WithContext("holiday", holiday), errs.WithContext("ephemeris", ephemeris), errs.WithContext("templateFile", templateFile))
return nil, errs.Wrap(
err,
errs.WithContext("start", start),
errs.WithContext("end", end),
errs.WithContext("holiday", holiday),
errs.WithContext("moonPhase", moonPhase),
errs.WithContext("solarTerm", solarTerm),
errs.WithContext("eclipse", eclipse),
errs.WithContext("planet", planet),
errs.WithContext("templateFile", templateFile))
}
startdate = dt
}
if len(start) > 0 {
dt, err := value.DateFrom(end)
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("start", start), errs.WithContext("end", end), errs.WithContext("holiday", holiday), errs.WithContext("ephemeris", ephemeris), errs.WithContext("templateFile", templateFile))
return nil, errs.Wrap(
err,
errs.WithContext("start", start),
errs.WithContext("end", end),
errs.WithContext("holiday", holiday),
errs.WithContext("moonPhase", moonPhase),
errs.WithContext("solarTerm", solarTerm),
errs.WithContext("eclipse", eclipse),
errs.WithContext("planet", planet),
errs.WithContext("templateFile", templateFile))
}
enddate = dt
}
Expand All @@ -54,7 +76,10 @@ func NewConfig(start, end string, holiday, ephemeris bool, tempDir *tempdir.Temp
start: startdate,
end: enddate,
holiday: holiday,
ephemeris: ephemeris,
moonPhase: moonPhase,
solarTerm: solarTerm,
eclipse: eclipse,
planet: planet,
tempDir: tempDir,
templateFile: templateFile,
}, nil
Expand Down Expand Up @@ -106,8 +131,17 @@ func (cal *Config) GetEvents() ([]koyomi.Event, error) {
if cal.holiday {
ids = append(ids, koyomi.Holiday)
}
if cal.ephemeris {
ids = append(ids, koyomi.MoonPhase, koyomi.SolarTerm, koyomi.Eclipse, koyomi.Planet)
if cal.moonPhase {
ids = append(ids, koyomi.MoonPhase)
}
if cal.solarTerm {
ids = append(ids, koyomi.SolarTerm)
}
if cal.eclipse {
ids = append(ids, koyomi.Eclipse)
}
if cal.planet {
ids = append(ids, koyomi.Planet)
}
if len(ids) == 0 {
return []koyomi.Event{}, nil
Expand Down
32 changes: 29 additions & 3 deletions facade/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func newCalendarCmd(ui *rwi.RWI) *cobra.Command {
calendarCmd.PersistentFlags().StringP("start", "", "", "start of date (YYYY-MM-DD)")
calendarCmd.PersistentFlags().StringP("end", "", "", "end of date (YYYY-MM-DD)")
calendarCmd.PersistentFlags().BoolP("holiday", "", false, "output holiday")
calendarCmd.PersistentFlags().BoolP("ephemeris", "", false, "output ephemeris")
calendarCmd.PersistentFlags().BoolP("ephemeris-all", "", false, "output all ephemeris")
calendarCmd.PersistentFlags().BoolP("moon-phase", "", false, "output moon-phase")
calendarCmd.PersistentFlags().BoolP("solar-term", "", false, "output solar-term")
calendarCmd.PersistentFlags().BoolP("eclipse", "", false, "output eclipse")
calendarCmd.PersistentFlags().BoolP("planet", "", false, "output planet")
calendarCmd.PersistentFlags().StringP("template", "", "", "template file for Output format")

calendarCmd.AddCommand(
Expand All @@ -45,15 +49,37 @@ func getCalendarConfig(cmd *cobra.Command, gopts *globalOptions) (*calendar.Conf
if err != nil {
return nil, errs.Wrap(err)
}
ephemerisFlag, err := cmd.Flags().GetBool("ephemeris")
moonPhaseFlag, err := cmd.Flags().GetBool("moon-phase")
if err != nil {
return nil, errs.Wrap(err)
}
solarTermFlag, err := cmd.Flags().GetBool("solar-term")
if err != nil {
return nil, errs.Wrap(err)
}
eclipseFlag, err := cmd.Flags().GetBool("eclipse")
if err != nil {
return nil, errs.Wrap(err)
}
planetFlag, err := cmd.Flags().GetBool("planet")
if err != nil {
return nil, errs.Wrap(err)
}
ephemerisAllFlag, err := cmd.Flags().GetBool("ephemeris-all")
if err != nil {
return nil, errs.Wrap(err)
}
if ephemerisAllFlag {
moonPhaseFlag = true
solarTermFlag = true
eclipseFlag = true
planetFlag = true
}
templateFile, err := cmd.Flags().GetString("template")
if err != nil {
return nil, errs.Wrap(err)
}
return calendar.NewConfig(start, end, holidayFlag, ephemerisFlag, gopts.TempDir, templateFile)
return calendar.NewConfig(start, end, holidayFlag, moonPhaseFlag, solarTermFlag, eclipseFlag, planetFlag, gopts.TempDir, templateFile)
}

/* Copyright 2024 Spiegel
Expand Down

0 comments on commit 1324a26

Please sign in to comment.