-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
226 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package commons | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
var ( | ||
selectedAll bool = false | ||
) | ||
|
||
func Input(msg string) string { | ||
terminalWriter := GetTerminalWriter() | ||
|
||
terminalWriter.Lock() | ||
defer terminalWriter.Unlock() | ||
|
||
red := "\033[31m" | ||
reset := "\033[0m" | ||
|
||
fmt.Printf("%s%s: %s", red, msg, reset) | ||
|
||
userInput := "" | ||
fmt.Scanln(&userInput) | ||
|
||
return userInput | ||
} | ||
|
||
// InputYN inputs Y or N | ||
// true for Y, false for N | ||
func InputYN(msg string) bool { | ||
if selectedAll { | ||
return true | ||
} | ||
|
||
for { | ||
inputString := Input(fmt.Sprintf("%s [yes(y)/no(n)/all(a)]", msg)) | ||
inputString = strings.ToLower(inputString) | ||
if inputString == "y" || inputString == "yes" || inputString == "true" { | ||
return true | ||
} else if inputString == "n" || inputString == "no" || inputString == "false" { | ||
return false | ||
} else if inputString == "a" || inputString == "all" { | ||
selectedAll = true | ||
return true | ||
} | ||
} | ||
} | ||
|
||
func InputInt(msg string) int { | ||
inputString := Input(msg) | ||
if len(inputString) == 0 { | ||
return 0 | ||
} | ||
|
||
v, err := strconv.Atoi(inputString) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
return v | ||
} | ||
|
||
func InputPassword(msg string) string { | ||
terminalWriter := GetTerminalWriter() | ||
|
||
terminalWriter.Lock() | ||
defer terminalWriter.Unlock() | ||
|
||
red := "\033[31m" | ||
reset := "\033[0m" | ||
|
||
fmt.Printf("%s%s: %s", red, msg, reset) | ||
bytePassword, err := term.ReadPassword(int(syscall.Stdin)) | ||
fmt.Print("\n") | ||
|
||
if err != nil { | ||
return "" | ||
} | ||
|
||
return string(bytePassword) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package commons | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"sync" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
terminalOutput *TerminalWriter | ||
) | ||
|
||
type TerminalWriter struct { | ||
mutex sync.Mutex | ||
} | ||
|
||
func (writer *TerminalWriter) Write(p []byte) (n int, err error) { | ||
writer.mutex.Lock() | ||
defer writer.mutex.Unlock() | ||
return os.Stdout.Write(p) | ||
} | ||
|
||
func (writer *TerminalWriter) Lock() { | ||
writer.mutex.Lock() | ||
} | ||
|
||
func (writer *TerminalWriter) Unlock() { | ||
writer.mutex.Unlock() | ||
} | ||
|
||
func InitTerminalOutput() { | ||
terminalOutput = &TerminalWriter{} | ||
} | ||
|
||
func GetTerminalWriter() *TerminalWriter { | ||
return terminalOutput | ||
} | ||
|
||
func PrintInfoln(a ...any) (n int, err error) { | ||
if log.GetLevel() > log.InfoLevel { | ||
return Println(a...) | ||
} | ||
return 0, nil | ||
} | ||
|
||
func PrintInfof(format string, a ...any) (n int, err error) { | ||
if log.GetLevel() > log.InfoLevel { | ||
return Printf(format, a...) | ||
} | ||
return 0, nil | ||
} | ||
|
||
func Print(a ...any) (n int, err error) { | ||
return fmt.Fprint(terminalOutput, a...) | ||
} | ||
|
||
func Printf(format string, a ...any) (n int, err error) { | ||
return fmt.Fprintf(terminalOutput, format, a...) | ||
} | ||
|
||
func Println(a ...any) (n int, err error) { | ||
return fmt.Fprintln(terminalOutput, a...) | ||
} | ||
|
||
func Fprintf(w io.Writer, format string, a ...any) (int, error) { | ||
terminalOutput.Lock() | ||
defer terminalOutput.Unlock() | ||
|
||
return fmt.Fprintf(w, format, a...) | ||
} | ||
|
||
func PrintErrorf(format string, a ...any) (int, error) { | ||
terminalOutput.Lock() | ||
defer terminalOutput.Unlock() | ||
|
||
red := "\033[31m" | ||
reset := "\033[0m" | ||
|
||
fmt.Fprint(os.Stderr, red) | ||
n, err := fmt.Fprintf(os.Stderr, format, a...) | ||
fmt.Fprint(os.Stderr, reset) | ||
|
||
return n, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/cyverse/irodsfs | ||
|
||
go 1.18 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/cyverse/go-irodsclient v0.15.6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.