Skip to content

Commit

Permalink
Added shortnames as shortcuts to commandline switches.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlbr committed May 4, 2020
1 parent 992909d commit 5447f5e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 98 deletions.
138 changes: 44 additions & 94 deletions cmd/feiertage/feiertage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package main
import (
"flag"
"fmt"
"github.com/wlbr/feiertage"
"os"
"strconv"
"strings"
"time"

"github.com/wlbr/feiertage"
)

func fmtTaskjuggler(reg feiertage.Region) string {
Expand All @@ -16,113 +19,60 @@ func fmtTaskjuggler(reg feiertage.Region) string {
return s
}

func getRegion(region string, year int, includingSundays bool) feiertage.Region {
rs := strings.ToLower(region)
func canonicalize(in string) (out string) {
low := strings.ToLower(in)
rep := strings.NewReplacer("-", "", "ä", "ae", "ö", "oe", "ü", "ue", "ß", "ss")
rs = rep.Replace(rs)
return rep.Replace(low)

}

func getRegion(region string, year int, includingSundays bool) (feiertage.Region, error) {
rs := canonicalize(region)
var r feiertage.Region
switch rs {
case "badenwuerttemberg":
r = feiertage.BadenWürttemberg(year, includingSundays)
case "bayern":
r = feiertage.Bayern(year, includingSundays)
case "berlin":
r = feiertage.Berlin(year, includingSundays)
case "brandenburg":
r = feiertage.Brandenburg(year, includingSundays)
case "bremen":
r = feiertage.Bremen(year, includingSundays)
case "hamburg":
r = feiertage.Hamburg(year, includingSundays)
case "hessen":
r = feiertage.Hessen(year, includingSundays)
case "mecklenburgvorpommern":
r = feiertage.MecklenburgVorpommern(year, includingSundays)
case "niedersachsen":
r = feiertage.Niedersachsen(year, includingSundays)
case "nordrheinwestfalen":
r = feiertage.NordrheinWestfalen(year, includingSundays)
case "rheinlandpfalz":
r = feiertage.RheinlandPfalz(year, includingSundays)
case "saarland":
r = feiertage.Saarland(year, includingSundays)
case "sachsen":
r = feiertage.Sachsen(year, includingSundays)
case "sachsenanhalt":
r = feiertage.SachsenAnhalt(year, includingSundays)
case "schleswigholstein":
r = feiertage.SchleswigHolstein(year, includingSundays)
case "thueringen":
r = feiertage.Thüringen(year, includingSundays)
case "deutschland":
r = feiertage.Deutschland(year, includingSundays)
case "burgenland":
r = feiertage.Burgenland(year, includingSundays)
case "kaernten":
r = feiertage.Kärnten(year, includingSundays)
case "niederoesterreich":
r = feiertage.Niederösterreich(year, includingSundays)
case "oberoesterreich":
r = feiertage.Oberösterreich(year, includingSundays)
case "salzburg":
r = feiertage.Salzburg(year, includingSundays)
case "steiermark":
r = feiertage.Steiermark(year, includingSundays)
case "tirol":
r = feiertage.Tirol(year, includingSundays)
case "vorarlberg":
r = feiertage.Vorarlberg(year, includingSundays)
case "wien":
r = feiertage.Wien(year, includingSundays)
case "oesterreich":
r = feiertage.Österreich(year, includingSundays)
default:
r = feiertage.All(year, includingSundays)

allRegions := feiertage.GetAllRegions(year, includingSundays)
for _, r := range allRegions {
if canonicalize(r.Name) == rs || canonicalize(r.Shortname) == rs {
return r, nil
}
}
return r
return r, fmt.Errorf("Region '%s' unbekannt.", region)
}

func main() {
var region = flag.String("region", "all", "Feiertag für Region 'string'.\n"+
"\tRegion kann sein:\n"+
"\t\tBadenWürttemberg\n"+
"\t\tBayern\n"+
"\t\tBerlin\n"+
"\t\tBrandenburg\n"+
"\t\tBremen\n"+
"\t\tHamburg\n"+
"\t\tHessen\n"+
"\t\tMecklenburgVorpommern\n"+
"\t\tNiedersachsen\n"+
"\t\tNordrheinWestfalen\n"+
"\t\tRheinlandPfalz\n"+
"\t\tSaarland\n"+
"\t\tSachsen\n"+
"\t\tSachsenAnhalt\n"+
"\t\tSchleswigHolstein\n"+
"\t\tThüringen\n"+
"\t\tDeutschland\n"+
"\t\tBurgenland\n"+
"\t\tKärnten\n"+
"\t\tNiederösterreich\n"+
"\t\tOberösterreich\n"+
"\t\tSalzburg\n"+
"\t\tSteiermark\n"+
"\t\tTirol\n"+
"\t\tVorarlberg\n"+
"\t\tWien\n"+
"\t\tÖsterreich\n"+
"\t\tAll\n")

var regions string
for _, r := range feiertage.GetAllRegions(time.Now().Year(), false, "de") {
regions = regions + "\t" + r.Name + "\n"
}
for _, r := range feiertage.GetAllRegions(time.Now().Year(), false, "at") {
regions = regions + "\t" + r.Name + "\n"
}
regions = regions + "\tAlle\n"
var region = flag.String("region", "Alle", "Feiertag für Region 'string'.\n"+
"Region kann sein:\n"+regions)

var includingSundays = flag.Bool("inklusiveSonntage", false, "Sollen Feiertag an Sonntagen mit ausgegeben werden?")
var asTaskjugglerCode = flag.Bool("asTaskjugglerCode", false, "Taskjuggler Code ausgeben.")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n"+
" %s [options] year \n\n"+
"%s zeigt alle Feiertage eins übergebenen Jahres an.\n\n"+
"Options:\n", os.Args[0], os.Args[0])
flag.PrintDefaults()
}

flag.Parse()
if len(flag.Args()) > 0 {
year, err := strconv.Atoi(flag.Args()[0])
if err != nil {
fmt.Println("Jahr muss eine Zahl sein.")
} else {
reg := getRegion(*region, year, *includingSundays)
if *asTaskjugglerCode {
reg, e := getRegion(*region, year, *includingSundays)
if e != nil {
fmt.Println(e)
} else if *asTaskjugglerCode {
fmt.Println(fmtTaskjuggler(reg))
} else {
fmt.Println(reg)
Expand Down
21 changes: 17 additions & 4 deletions cmd/feiertage/feiertagecmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"github.com/wlbr/feiertage"
"regexp"
"testing"

"github.com/wlbr/feiertage"
)

func compareAndFail(t *testing.T, f feiertage.Feiertag, d string) {
Expand All @@ -15,18 +16,30 @@ func compareAndFail(t *testing.T, f feiertage.Feiertag, d string) {
}

func TestGetRegion(t *testing.T) {
bay := getRegion("Bayern", 2016, true)
bay, e := getRegion("Bayern", 2016, true)
if e != nil {
fmt.Printf("Could not find region 'Bayern'")
t.Fail()
}
fron := bay.Feiertage[7]
compareAndFail(t, fron, "26.05.2016")

bra := getRegion("Brandenburg", 2016, true)
bra, e := getRegion("Brandenburg", 2016, true)
if e != nil {
fmt.Printf("Could not find region 'Brandenburg'")
t.Fail()
}
ostern := bra.Feiertage[2]
compareAndFail(t, ostern, "27.03.2016")

}

func TestFmtTaskjuggler(t *testing.T) {
reg := getRegion("Brandenburg", 2016, true)
reg, e := getRegion("Brandenburg", 2016, true)
if e != nil {
fmt.Printf("Could not find region 'Brandenburg'")
t.Fail()
}
jug := fmtTaskjuggler(reg)

regex := regexp.MustCompile(`leaves holiday "Ostern" 2016-03-27`)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/wlbr/feiertage

go 1.13
36 changes: 36 additions & 0 deletions region.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package feiertage
import (
"fmt"
"sort"
"strings"
)

// Region represents a Federal State of Germany or Austria (Bundesland). Some
Expand Down Expand Up @@ -328,3 +329,38 @@ func All(y int, inklSonntage ...bool) Region {
sort.Sort(ByDate(feiertermine))
return Region{"Alle", "All", feiertermine}
}

func regionFunctionListToRegionList(rfun []func(y int, inklSonntage ...bool) Region, year int, inklSonntage ...bool) []Region {
regions := []Region{}
is := false
if len(inklSonntage) > 0 {
is = inklSonntage[0]
}
for _, r := range rfun {
regions = append(regions, r(year, is))
}
return regions
}

// GetAllRegions returns a list of all regions available. These may be filtered by providing the country ("de"|"at"|empty)
func GetAllRegions(year int, inklSonntag bool, country ...string) (regions []Region) {
germanregions := regionFunctionListToRegionList([]func(y int, inklSonntage ...bool) Region{BadenWürttemberg, Bayern, Berlin,
Brandenburg, Bremen, Hamburg, Hessen, MecklenburgVorpommern, Niedersachsen, NordrheinWestfalen,
RheinlandPfalz, Saarland, Sachsen, SachsenAnhalt, SchleswigHolstein, Thüringen, Deutschland}, year, inklSonntag)

austrianregions := regionFunctionListToRegionList([]func(y int, inklSonntage ...bool) Region{Burgenland, Kärnten, Niederösterreich,
Oberösterreich, Salzburg, Steiermark, Tirol, Vorarlberg, Wien, Österreich}, year, inklSonntag)

if len(country) > 0 {
c := strings.ToLower(country[0])
if c == "de" {
regions = germanregions
} else if c == "at" {
regions = austrianregions
}
} else {
regions = append(append(germanregions, austrianregions...), All(year, inklSonntag))
}

return regions
}

0 comments on commit 5447f5e

Please sign in to comment.