Skip to content

Commit

Permalink
chore: various fixes (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Nov 10, 2023
1 parent 765c2af commit 3d72803
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
15 changes: 4 additions & 11 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ type commandLineFlags struct {
noAnsi bool
theme string
resticArgs []string
selfUpdate bool
wait bool
isChild bool
parentPort int
noPriority bool
ignoreOnBattery int
run string
usagesHelp string
}

Expand All @@ -51,20 +49,16 @@ func loadFlags(args []string) (*pflag.FlagSet, commandLineFlags, error) {
flagset.StringVarP(&flags.format, "format", "f", "", "file format of the configuration (default is to use the file extension)")
flagset.StringVarP(&flags.name, "name", "n", constants.DefaultProfileName, "profile name")
flagset.StringVarP(&flags.log, "log", "l", "", "logs to a target instead of the console")

flagset.BoolVar(&flags.dryRun, "dry-run", false, "display the restic commands instead of running them")

flagset.BoolVar(&flags.noLock, "no-lock", false, "skip profile lock file")
flagset.DurationVar(&flags.lockWait, "lock-wait", 0, "wait up to duration to acquire a lock (syntax \"1h5m30s\")")

flagset.BoolVar(&flags.noAnsi, "no-ansi", false, "disable ansi control characters (disable console colouring)")
flagset.StringVar(&flags.theme, "theme", constants.DefaultTheme, "console colouring theme (dark, light, none)")
flagset.BoolVar(&flags.noPriority, "no-prio", false, "don't set any priority on load: used when started from a service that has already set the priority")
flagset.BoolVar(&flags.noPriority, "no-prio", false, "don't change the process priority: used when started from a service that has already set the priority")
flagset.BoolVarP(&flags.wait, "wait", "w", false, "wait at the end until the user presses the enter key")
flagset.IntVar(&flags.ignoreOnBattery, "ignore-on-battery", 0, "don't start the profile when the computer is running on battery. You can specify a value to ignore only when the % charge left is less or equal than the value")
flagset.Lookup("ignore-on-battery").NoOptDefVal = "100" // 0 is flag not set, 100 is for a flag with no value (meaning just battery discharge)

flagset.BoolVarP(&flags.wait, "wait", "w", false, "wait at the end until the user presses the enter key")

if platform.IsWindows() {
// flag for internal use only
flagset.BoolVar(&flags.isChild, constants.FlagAsChild, false, "run as an elevated user child process")
Expand All @@ -80,8 +74,7 @@ func loadFlags(args []string) (*pflag.FlagSet, commandLineFlags, error) {
width, _ := term.OsStdoutTerminalSize()
flags.usagesHelp = flagset.FlagUsagesWrapped(width)

err := flagset.Parse(args)
if err != nil {
if err := flagset.Parse(args); err != nil {
return flagset, flags, err
}

Expand All @@ -90,7 +83,7 @@ func loadFlags(args []string) (*pflag.FlagSet, commandLineFlags, error) {

// if there are no further arguments, no further parsing is needed
if len(flags.resticArgs) == 0 {
return flagset, flags, err
return flagset, flags, nil
}

// handle explicit help request in command args (works with restic and own commands)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.14.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/sys v0.13.0
golang.org/x/term v0.13.0
golang.org/x/text v0.13.0
gopkg.in/yaml.v3 v3.0.1
howett.net/plist v1.0.0
Expand Down Expand Up @@ -71,9 +71,9 @@ require (
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion restic/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetVersion(executable string) (string, error) {
if match := versionCommandPattern.FindSubmatch(output); match != nil {
return string(match[1]), nil
}
return "", fmt.Errorf("restic returned no valid version: %s", string(output))
return "", fmt.Errorf("restic returned no valid version: %s", strings.TrimSpace(string(output)))
} else {
return "", err
}
Expand Down
6 changes: 6 additions & 0 deletions restic/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestDownloadBinary(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip()
}
Expand Down Expand Up @@ -43,6 +44,11 @@ func TestDownloadBinary(t *testing.T) {
}
}

func TestNoVersion(t *testing.T) {
_, err := GetVersion("echo")
assert.Error(t, err)
}

func TestSourceChecksRepo(t *testing.T) {
var err error
ctx := context.Background()
Expand Down
17 changes: 8 additions & 9 deletions term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sync"

"github.com/mattn/go-colorable"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

var (
Expand All @@ -28,8 +28,7 @@ func AskYesNo(reader io.Reader, message string, defaultAnswer bool) bool {
if !strings.HasSuffix(message, "?") {
message += "?"
}
question := ""
input := ""
var question, input string
if defaultAnswer {
question = "(Y/n)"
input = "y"
Expand Down Expand Up @@ -59,13 +58,13 @@ func AskYesNo(reader io.Reader, message string, defaultAnswer bool) bool {
// ReadPassword reads a password without echoing it to the terminal.
func ReadPassword() (string, error) {
stdin := int(os.Stdin.Fd())
if !terminal.IsTerminal(stdin) {
if !term.IsTerminal(stdin) {
return ReadLine()
}
line, err := terminal.ReadPassword(stdin)
line, err := term.ReadPassword(stdin)
_, _ = fmt.Fprintln(os.Stderr)
if err != nil {
return "", fmt.Errorf("failed to read password: %v", err)
return "", fmt.Errorf("failed to read password: %w", err)
}
return string(line), nil
}
Expand All @@ -75,22 +74,22 @@ func ReadLine() (string, error) {
buf := bufio.NewReader(os.Stdin)
line, err := buf.ReadString('\n')
if err != nil {
return "", fmt.Errorf("failed to read line: %v", err)
return "", fmt.Errorf("failed to read line: %w", err)
}
return strings.TrimSpace(line), nil
}

// OsStdoutIsTerminal returns true as os.Stdout is a terminal session
func OsStdoutIsTerminal() bool {
fd := int(os.Stdout.Fd())
return terminal.IsTerminal(fd)
return term.IsTerminal(fd)
}

// OsStdoutIsTerminal returns true as os.Stdout is a terminal session
func OsStdoutTerminalSize() (width, height int) {
fd := int(os.Stdout.Fd())
var err error
width, height, err = terminal.GetSize(fd)
width, height, err = term.GetSize(fd)
if err != nil {
width, height = 0, 0
}
Expand Down

0 comments on commit 3d72803

Please sign in to comment.