-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REPORTTING: ability to add days off, and get total days of work in mo…
…nth (#8)
- Loading branch information
jeff-knurek
authored
Oct 26, 2020
1 parent
1a2fb14
commit aeec1e2
Showing
4 changed files
with
148 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const dFormat = "2006-01-02" | ||
|
||
// dayOffCmd represents the dayOff command | ||
var dayOffCmd = &cobra.Command{ | ||
Use: "dayOff [day]", | ||
Short: "Add a day that isn't calcualted for total. (format: 2016-04-28)", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return errors.New("requires a day (format: 2016-04-28)") | ||
} | ||
if validDay(args[0]) { | ||
return nil | ||
} | ||
return fmt.Errorf("invalid day format (yyyy-mm-dd): %s", args[0]) | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
addDayOff(args[0]) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(dayOffCmd) | ||
} | ||
|
||
func validDay(day string) bool { | ||
_, err := time.Parse(dFormat, day) | ||
if err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func addDayOff(day string) { | ||
t, _ := time.Parse(dFormat, day) | ||
newDay := t.Format(dFormat) | ||
days := viper.GetStringSlice("days_off") | ||
for _, d := range days { | ||
if d == newDay { | ||
fmt.Println(newDay, "already exists") | ||
return | ||
} | ||
} | ||
days = append(days, newDay) | ||
|
||
viper.Set("days_off", days) | ||
if err := viper.WriteConfig(); err != nil { | ||
fmt.Println("problem with updating config file", err) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters