Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ func getOwnCommands() []ownCommand {
}
}

func panicCommand(_ io.Writer, _ commandRequest) error {
func panicCommand(_ io.Writer, _ commandContext) error {
panic("you asked for it")
}

func completeCommand(output io.Writer, request commandRequest) error {
args := request.args
func completeCommand(output io.Writer, ctx commandContext) error {
args := ctx.request.arguments
requester := "unknown"
requesterVersion := 0

Expand All @@ -178,7 +178,7 @@ func completeCommand(output io.Writer, request commandRequest) error {
return nil
}

completions := NewCompleter(request.ownCommands.All(), DefaultFlagsLoader).Complete(args)
completions := NewCompleter(ctx.ownCommands.All(), DefaultFlagsLoader).Complete(args)
if len(completions) > 0 {
for _, completion := range completions {
fmt.Fprintln(output, completion)
Expand All @@ -193,8 +193,8 @@ var bashCompletionScript string
//go:embed contrib/completion/zsh-completion.sh
var zshCompletionScript string

func generateCommand(output io.Writer, request commandRequest) (err error) {
args := request.args
func generateCommand(output io.Writer, ctx commandContext) (err error) {
args := ctx.request.arguments
// enforce no-log
logger := clog.GetDefaultLogger()
handler := logger.GetHandler()
Expand All @@ -207,8 +207,8 @@ func generateCommand(output io.Writer, request commandRequest) (err error) {
} else if slices.Contains(args, "--json-schema") {
err = generateJsonSchema(output, args[slices.Index(args, "--json-schema")+1:])
} else if slices.Contains(args, "--random-key") {
request.flags.resticArgs = args[slices.Index(args, "--random-key"):]
err = randomKey(output, request)
ctx.flags.resticArgs = args[slices.Index(args, "--random-key"):]
err = randomKey(output, ctx)
} else if slices.Contains(args, "--zsh-completion") {
_, err = fmt.Fprintln(output, zshCompletionScript)
} else {
Expand Down Expand Up @@ -278,9 +278,9 @@ func sortedProfileKeys(data map[string]*config.Profile) []string {
return keys
}

func showProfile(output io.Writer, request commandRequest) error {
c := request.config
flags := request.flags
func showProfile(output io.Writer, ctx commandContext) error {
c := ctx.config
flags := ctx.flags

// Load global section
global, err := c.GetGlobalSection()
Expand Down Expand Up @@ -340,9 +340,9 @@ func showSchedules(output io.Writer, schedulesConfig []*config.ScheduleConfig) {
}

// randomKey simply display a base64'd random key to the console
func randomKey(output io.Writer, request commandRequest) error {
func randomKey(output io.Writer, ctx commandContext) error {
var err error
flags := request.flags
flags := ctx.flags
size := uint64(1024)
// flags.resticArgs contain the command and the rest of the command line
if len(flags.resticArgs) > 1 {
Expand Down Expand Up @@ -398,10 +398,10 @@ func flagsForProfile(flags commandLineFlags, profileName string) commandLineFlag
}

// createSchedule accepts one argument from the commandline: --no-start
func createSchedule(_ io.Writer, request commandRequest) error {
c := request.config
flags := request.flags
args := request.args
func createSchedule(_ io.Writer, ctx commandContext) error {
c := ctx.config
flags := ctx.flags
args := ctx.request.arguments

defer c.DisplayConfigurationIssues()

Expand Down Expand Up @@ -453,10 +453,10 @@ func createSchedule(_ io.Writer, request commandRequest) error {
return nil
}

func removeSchedule(_ io.Writer, request commandRequest) error {
c := request.config
flags := request.flags
args := request.args
func removeSchedule(_ io.Writer, ctx commandContext) error {
c := ctx.config
flags := ctx.flags
args := ctx.request.arguments

// Unschedule all jobs of all selected profiles
for _, profileName := range selectProfiles(c, flags, args) {
Expand All @@ -476,10 +476,10 @@ func removeSchedule(_ io.Writer, request commandRequest) error {
return nil
}

func statusSchedule(w io.Writer, request commandRequest) error {
c := request.config
flags := request.flags
args := request.args
func statusSchedule(w io.Writer, ctx commandContext) error {
c := ctx.config
flags := ctx.flags
args := ctx.request.arguments

defer c.DisplayConfigurationIssues()

Expand Down Expand Up @@ -572,9 +572,9 @@ func getRemovableScheduleJobs(c *config.Config, flags commandLineFlags) (schedul
return scheduler, profile, schedules, nil
}

func testElevationCommand(_ io.Writer, request commandRequest) error {
if request.flags.isChild {
client := remote.NewClient(request.flags.parentPort)
func testElevationCommand(_ io.Writer, ctx commandContext) error {
if ctx.flags.isChild {
client := remote.NewClient(ctx.flags.parentPort)
term.Print("first line", "\n")
term.Println("second", "one")
term.Printf("value = %d\n", 11)
Expand All @@ -585,7 +585,7 @@ func testElevationCommand(_ io.Writer, request commandRequest) error {
return nil
}

return elevated(request.flags)
return elevated(ctx.flags)
}

func retryElevated(err error, flags commandLineFlags) error {
Expand Down
47 changes: 24 additions & 23 deletions commands_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func getCommonUsageHelpLine(commandName string, withProfile bool) string {
)
}

func displayOwnCommands(output io.Writer, request commandRequest) {
out, closer := displayWriter(output, request.flags)
func displayOwnCommands(output io.Writer, ctx commandContext) {
out, closer := displayWriter(output, ctx.flags)
defer closer()

for _, command := range request.ownCommands.commands {
for _, command := range ctx.ownCommands.commands {
if command.hide {
continue
}
Expand All @@ -86,12 +86,12 @@ func displayOwnCommands(output io.Writer, request commandRequest) {
}
}

func displayOwnCommandHelp(output io.Writer, commandName string, request commandRequest) {
out, closer := displayWriter(output, request.flags)
func displayOwnCommandHelp(output io.Writer, commandName string, ctx commandContext) {
out, closer := displayWriter(output, ctx.flags)
defer closer()

var command *ownCommand
for _, c := range request.ownCommands.commands {
for _, c := range ctx.ownCommands.commands {
if c.name == commandName {
command = &c
break
Expand Down Expand Up @@ -130,8 +130,8 @@ func displayOwnCommandHelp(output io.Writer, commandName string, request command
}
}

func displayCommonUsageHelp(output io.Writer, request commandRequest) {
out, closer := displayWriter(output, request.flags)
func displayCommonUsageHelp(output io.Writer, ctx commandContext) {
out, closer := displayWriter(output, ctx.flags)
defer closer()

out("resticprofile is a configuration profiles manager for backup profiles and ")
Expand All @@ -142,10 +142,10 @@ func displayCommonUsageHelp(output io.Writer, request commandRequest) {
out("\t%s [command specific flags]\n", getCommonUsageHelpLine("resticprofile-command", true))
out("\n")
out(ansiBold("resticprofile flags:\n"))
out(request.flags.usagesHelp)
out(ctx.flags.usagesHelp)
out("\n\n")
out(ansiBold("resticprofile own commands:\n"))
displayOwnCommands(out(), request)
displayOwnCommands(out(), ctx)
out("\n")

out("%s at %s\n",
Expand Down Expand Up @@ -218,10 +218,10 @@ func displayResticHelp(output io.Writer, configuration *config.Config, flags com
}
}

func displayHelpCommand(output io.Writer, request commandRequest) error {
flags := request.flags
func displayHelpCommand(output io.Writer, ctx commandContext) error {
flags := ctx.flags

out, closer := displayWriter(output, request.flags)
out, closer := displayWriter(output, ctx.flags)
defer closer()

if flags.log == "" {
Expand All @@ -237,26 +237,27 @@ func displayHelpCommand(output io.Writer, request commandRequest) error {
}

if helpForCommand == nil {
displayCommonUsageHelp(out("\n"), request)
displayCommonUsageHelp(out("\n"), ctx)

} else if request.ownCommands.Exists(*helpForCommand, true) || request.ownCommands.Exists(*helpForCommand, false) {
displayOwnCommandHelp(out("\n"), *helpForCommand, request)
} else if ctx.ownCommands.Exists(*helpForCommand, true) || ctx.ownCommands.Exists(*helpForCommand, false) {
displayOwnCommandHelp(out("\n"), *helpForCommand, ctx)

} else {
displayResticHelp(out(), request.config, flags, *helpForCommand)
displayResticHelp(out(), ctx.config, flags, *helpForCommand)
}

return nil
}

func displayVersion(output io.Writer, request commandRequest) error {
out, closer := displayWriter(output, request.flags)
func displayVersion(output io.Writer, ctx commandContext) error {
out, closer := displayWriter(output, ctx.flags)
defer closer()

out("resticprofile version %s commit %s\n", ansiBold(version), ansiYellow(commit))

// allow for the general verbose flag, or specified after the command
if request.flags.verbose || (len(request.args) > 0 && (request.args[0] == "-v" || request.args[0] == "--verbose")) {
arguments := ctx.request.arguments
if ctx.flags.verbose || (len(arguments) > 0 && (arguments[0] == "-v" || arguments[0] == "--verbose")) {
out("\n")
out("\t%s:\t%s\n", "home", "https://github.com/creativeprojects/resticprofile")
out("\t%s:\t%s\n", "os", runtime.GOOS)
Expand All @@ -280,9 +281,9 @@ func displayVersion(output io.Writer, request commandRequest) error {
return nil
}

func displayProfilesCommand(output io.Writer, request commandRequest) error {
displayProfiles(output, request.config, request.flags)
displayGroups(output, request.config, request.flags)
func displayProfilesCommand(output io.Writer, ctx commandContext) error {
displayProfiles(output, ctx.config, ctx.flags)
displayGroups(output, ctx.config, ctx.flags)
return nil
}

Expand Down
13 changes: 13 additions & 0 deletions commands_display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -295,3 +296,15 @@ https://creativeprojects.github.io/resticprofile/
}
}
}

func TestDisplayVersionVerbose1(t *testing.T) {
buffer := &bytes.Buffer{}
displayVersion(buffer, commandContext{Context: Context{flags: commandLineFlags{verbose: true}}})
assert.True(t, strings.Contains(buffer.String(), runtime.GOOS))
}

func TestDisplayVersionVerbose2(t *testing.T) {
buffer := &bytes.Buffer{}
displayVersion(buffer, commandContext{Context: Context{request: Request{arguments: []string{"-v"}}}})
assert.True(t, strings.Contains(buffer.String(), runtime.GOOS))
}
Loading