diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | src/c_go2work.go | 56 | ||||
-rw-r--r-- | src/c_player.go | 4 | ||||
-rw-r--r-- | src/u_checks.go | 80 | ||||
-rw-r--r-- | src/u_utils.go | 26 |
5 files changed, 137 insertions, 32 deletions
@@ -11,7 +11,7 @@ # ======================== # # go2work: Makefile -# Tue Mar 29 22:14:45 CEST 2022 +# Wed Mar 30 01:26:09 CEST 2022 # Joe # # GNU Makefile @@ -27,6 +27,7 @@ TRGT_DIR = ./ SRCS_NAME = c_go2work SRCS_NAME += c_player +SRCS_NAME += u_checks SRCS_NAME += u_prints SRCS_NAME += u_utils diff --git a/src/c_go2work.go b/src/c_go2work.go index 2536f01..073fb43 100644 --- a/src/c_go2work.go +++ b/src/c_go2work.go @@ -39,7 +39,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/c_go2work.go - * Wed Mar 30 00:16:51 CEST 2022 + * Wed Mar 30 01:38:10 CEST 2022 * Joe * * The main. @@ -48,6 +48,7 @@ package main import ( + "fmt" "log" "os" "time" @@ -56,12 +57,15 @@ import ( ) const ( - PROGNAME = "go2work" - VERSION = "0.1.0" - HOURS = 0 - MINS = 1 - SECS = 2 - INTERVAL = 500 + PROGNAME = "go2work" + VERSION = "0.1.0" + HOURS = 0 + MINS = 1 + SECS = 2 + INTERVAL = 500 + USE_FORTUNE = true + MEDIA_PLAYER = "mpv" + LOG_FORMAT = "bad time format" ) func main() { @@ -71,7 +75,7 @@ func main() { log.SetPrefix(PROGNAME + ": ") log.SetFlags(0) if len(os.Args[0:]) == 1 { - log.Fatal("No arguments") + log.Fatal("no arguments") return } switch os.Args[1] { @@ -87,6 +91,9 @@ func main() { case "-t", "--test": dest_t = get_test_time() default: + if strings.Contains(os.Args[1], ":") == false { + log.Fatal("bad time format") + } str_dest_t := strings.Split(os.Args[1], ":") tmp, _ = strconv.Atoi(str_dest_t[HOURS]) dest_t[HOURS] = byte(tmp) @@ -94,6 +101,17 @@ func main() { dest_t[MINS] = byte(tmp) dest_t[SECS] = 0 } + if check_media_player(MEDIA_PLAYER) == false { + log.Fatal("media player (" + MEDIA_PLAYER + ") not found") + return + } + if USE_FORTUNE == true && check_fortune() == false { + fmt.Println("Beware, fortune is set on but was not found") + } + if check_time_format(dest_t) == false { + log.Fatal("bad time format") + return + } curr_t = get_time() print_time_left(curr_t, dest_t) ticker := time.NewTicker(INTERVAL * time.Millisecond) @@ -107,8 +125,8 @@ func main() { curr_t[MINS] == dest_t[MINS] && curr_t[SECS] == dest_t[SECS] { exec_player( - true, - "mpv", + USE_FORTUNE, + MEDIA_PLAYER, "--no-video", "/usr/home/jozan/mu/progressive/progressive_black_metal/deathspell_omega/2010_paracletus/02_wings_of_predation.flac", ) @@ -120,21 +138,3 @@ func main() { } } } - -func get_time() [3]byte { - var curr_t [3]byte - var tmp int - now := time.Now() - t := strings.Split(now.Format("15:04:05"), ":") - tmp, _ = strconv.Atoi(t[HOURS]) - curr_t[HOURS] = byte(tmp) - tmp, _ = strconv.Atoi(t[MINS]) - curr_t[MINS] = byte(tmp) - tmp, _ = strconv.Atoi(t[SECS]) - curr_t[SECS] = byte(tmp) - return curr_t -} - -func get_test_time() [3]byte { - return seconds_to_time(time_to_seconds(get_time()) + 3) -} diff --git a/src/c_player.go b/src/c_player.go index 6c384f8..623adaa 100644 --- a/src/c_player.go +++ b/src/c_player.go @@ -39,10 +39,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/c_player.go - * Tue Mar 29 20:59:52 CEST 2022 + * Wed Mar 30 01:18:12 CEST 2022 * Joe * - * Simple func that plays the file + * Funcs to play the file */ package main diff --git a/src/u_checks.go b/src/u_checks.go new file mode 100644 index 0000000..1a33232 --- /dev/null +++ b/src/u_checks.go @@ -0,0 +1,80 @@ +/* + * ======================== + * ===== =============== + * ====== ================ + * ====== ================ + * ====== ==== ==== == + * ====== === == = = + * ====== === = == = + * = === === = == ==== + * = === === = == = = + * == ===== ==== == + * ======================== + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2022 Joe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the organization nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * go2work: src/u_checks.go + * Wed Mar 30 01:25:35 CEST 2022 + * Joe + * + * Useful checks. + */ + +package main + +import ( + "os/exec" +) + +func check_media_player(media_player string) bool { + cmd := exec.Command("command", "-v", media_player) + err := cmd.Run() + if err != nil { + return false + } + return true +} + +func check_fortune() bool { + cmd := exec.Command("command", "-v", "fortune") + err := cmd.Run() + if err != nil { + return false + } + return true +} + +func check_time_format(time [3]byte) bool { + if time[HOURS] > 23 { + return false + } + if time[MINS] > 59 { + return false + } + return true +} diff --git a/src/u_utils.go b/src/u_utils.go index 598efed..b1d846f 100644 --- a/src/u_utils.go +++ b/src/u_utils.go @@ -39,12 +39,18 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * go2work: src/u_utils.go - * Wed Mar 30 00:02:25 CEST 2022 + * Wed Mar 30 01:25:20 CEST 2022 * Joe */ package main +import ( + "time" + "strconv" + "strings" +) + func time_to_seconds(time [3]byte) uint { return (3600 * uint(time[HOURS])) + (60 * uint(time[MINS])) + @@ -61,3 +67,21 @@ func seconds_to_time(seconds uint) [3]byte { time[SECS] = byte((seconds - (hours * 3600)) - mins * 60) return time } + +func get_time() [3]byte { + var curr_t [3]byte + var tmp int + now := time.Now() + t := strings.Split(now.Format("15:04:05"), ":") + tmp, _ = strconv.Atoi(t[HOURS]) + curr_t[HOURS] = byte(tmp) + tmp, _ = strconv.Atoi(t[MINS]) + curr_t[MINS] = byte(tmp) + tmp, _ = strconv.Atoi(t[SECS]) + curr_t[SECS] = byte(tmp) + return curr_t +} + +func get_test_time() [3]byte { + return seconds_to_time(time_to_seconds(get_time()) + 3) +} |