Add functionality to accept setting status non-interactively

main
diff 2020-11-14 06:38:35 +00:00
parent 69f41ad157
commit 48b89eaff5
1 changed files with 36 additions and 6 deletions

42
main.go
View File

@ -100,10 +100,17 @@ func SetStatus(args []string) error {
// User status is written to ~/.checkin
outputPath := path.Join(curUser.HomeDir, ".checkin")
// Prompt user for input
input, err := PromptInput()
if err != nil {
return err
var input string
// Check if input was provided on the command line.
if len(setFlags.Args()) > 0 {
input = ArgsToStatus(setFlags.Args(), curUser)
} else {
// Prompt user for input
input, err = PromptInput()
if err != nil {
return err
}
}
// Remove file on blank input
@ -137,6 +144,29 @@ func SetStatus(args []string) error {
return nil
}
// ArgsToStatus concatenates all arguments left over from a call to flag.Parse
// and returns it as a well-formed status.
func ArgsToStatus(args []string, curUser *user.User) string {
status := strings.Join(args, " ")
status = strings.TrimRight(status, " \n\t\r")
if status == "" {
return status
}
if strings.HasPrefix(status, "~"+curUser.Username) {
return status
}
if strings.HasPrefix(status, curUser.Username) {
return "~" + status
}
return fmt.Sprintf("~%s: %s", curUser.Username, status)
}
// GetFriendlyWd returns the friendliest equivalent to the working directory.
// If we're in the user's home directory, we return a path relative to ~user.
// If we're in that user's public_html directory, we return an http link to it.
func GetFriendlyWd(curUser *user.User) (string, error) {
wd, err := os.Getwd()
if err != nil {
@ -157,11 +187,11 @@ func GetFriendlyWd(curUser *user.User) (string, error) {
}
// If the path is within the user's public_html directory, return an http link.
weblessPath := strings.TrimPrefix(homelessPath, "public_html/")
weblessPath := strings.TrimPrefix(homelessPath, "public_html/")
if weblessPath != homelessPath {
return fmt.Sprintf("https://tilde.town/~%s/%s", curUser.Username, weblessPath), nil
}
// Otherwise make a friendly ~user/path.
return filepath.Join("~"+curUser.Username, homelessPath), nil
}
}