Added "get" subcommand to return all ventures on the system.

main
diff 2020-11-13 05:33:03 +00:00
parent 4f501c5d65
commit 61f9a6e395
1 changed files with 61 additions and 2 deletions

61
main.go
View File

@ -4,12 +4,14 @@ import (
"fmt"
"flag"
"path"
"path/filepath"
"os"
"os/user"
"io"
"io/ioutil"
"bufio"
"strings"
"time"
)
func main() {
@ -20,12 +22,69 @@ func main() {
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\n", os.Args[0])
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)
fmt.Println(venture)
}
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.
func SetVenture(args []string) error {
curUser, err := user.Current()