CheckIn/main.go

129 lines
3.1 KiB
Go
Raw Normal View History

2020-11-12 19:19:36 +00:00
package main
import (
2020-11-13 05:34:00 +00:00
"bufio"
"flag"
2020-11-13 05:34:00 +00:00
"fmt"
2020-11-12 22:06:19 +00:00
"io"
"io/ioutil"
2020-11-13 05:34:00 +00:00
"os"
"os/user"
"path"
"path/filepath"
"strings"
"time"
2020-11-12 19:19:36 +00:00
)
func main() {
flag.Parse()
switch flag.Arg(0) {
case "set":
2020-11-13 03:59:45 +00:00
err := SetVenture(flag.Args()[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Could not set: %v", err)
}
case "get":
err := GetVenture(flag.Args()[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Could not get: %v", err)
}
default:
fmt.Printf("Usage:\n\t%v <set|get>\n", os.Args[0])
flag.PrintDefaults()
}
}
// GetVenture collects all recent ventures from all users on the system.
// Any errors that occur while pulling individual ventures are silently ignored
func GetVenture(args []string) error {
// Set the expiration date for ventures at 2 weeks.
freshLimit := time.Now().AddDate(0, 0, -14)
allVenturePaths, err := filepath.Glob("/home/*/.venture")
if err != nil {
// *Should* never happen, since path is hardcoded and that's the only reason Glob can error out.
return err
}
// Filter out any ventures that are older than our cutoff time.
freshVenturePaths := make([]string, 0, len(allVenturePaths))
for _, venturePath := range allVenturePaths {
ventureInfo, err := os.Stat(venturePath)
if err != nil {
continue
}
if ventureInfo.ModTime().After(freshLimit) {
freshVenturePaths = append(freshVenturePaths, venturePath)
}
}
// Print the contents of all ventures
for _, venturePath := range freshVenturePaths {
ventureBytes, err := ioutil.ReadFile(venturePath)
if err != nil {
continue
}
venture := string(ventureBytes)
// Check to see if file starts with user's name.
if !strings.HasPrefix(venture, "~") {
// Strip /home from path
homelessPath, err := filepath.Rel("/home", venturePath)
if err != nil {
continue
}
// Strip .venture from path, leaving us with the user's name.
username := filepath.Dir(homelessPath)
venture = fmt.Sprintf("~%s: %s", username, venture)
}
// Trim trailing newline (if any)
venture = strings.TrimSpace(venture)
2020-11-13 05:34:00 +00:00
fmt.Println(venture)
}
2020-11-13 05:34:00 +00:00
return nil
}
// SetVenture sets the curent user's venture, either by reading the value from the command line or by prompting the user to input it interactively.
2020-11-13 03:59:45 +00:00
func SetVenture(args []string) error {
2020-11-12 22:06:19 +00:00
curUser, err := user.Current()
if err != nil {
2020-11-13 03:59:45 +00:00
return err
2020-11-12 22:06:19 +00:00
}
2020-11-13 03:11:40 +00:00
// User status is written to ~/.venture
2020-11-12 22:13:09 +00:00
outputPath := path.Join(curUser.HomeDir, ".venture")
2020-11-13 03:11:40 +00:00
// Prompt user for input
2020-11-12 22:06:19 +00:00
fmt.Printf("What's ~%v been up to?\n~%v", curUser.Username, curUser.Username)
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
2020-11-13 03:59:45 +00:00
return err
2020-11-12 22:06:19 +00:00
}
2020-11-13 03:11:40 +00:00
// Remove file on blank input
if strings.TrimSpace(input) == "" {
2020-11-12 22:13:09 +00:00
err = os.Remove(outputPath)
// File already pre-non-existing is considered success.
if os.IsNotExist(err) {
return nil
} else if err != nil {
2020-11-13 03:59:45 +00:00
return err
2020-11-12 22:13:09 +00:00
}
2020-11-13 03:59:45 +00:00
return nil
2020-11-12 22:13:09 +00:00
}
2020-11-13 03:11:40 +00:00
// Prepend status with user's name
input = fmt.Sprintf("~%s%s", curUser.Username, input)
2020-11-13 03:11:40 +00:00
// Write file and create if it doesn't exist as world-readable.
2020-11-12 22:06:19 +00:00
err = ioutil.WriteFile(outputPath, []byte(input), 0644)
if err != nil {
2020-11-13 03:59:45 +00:00
return err
2020-11-12 22:06:19 +00:00
}
2020-11-13 03:59:45 +00:00
return nil
2020-11-13 05:34:00 +00:00
}