Added "get" subcommand to return all ventures on the system.
parent
4f501c5d65
commit
61f9a6e395
61
main.go
61
main.go
|
@ -4,12 +4,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"flag"
|
"flag"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"bufio"
|
"bufio"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -20,12 +22,69 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Could not set: %v", err)
|
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:
|
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()
|
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.
|
// 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 {
|
func SetVenture(args []string) error {
|
||||||
curUser, err := user.Current()
|
curUser, err := user.Current()
|
||||||
|
|
Loading…
Reference in New Issue