Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "profile" flag as an alias for "name" #357

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
69 changes: 50 additions & 19 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@
flagset.IntVar(&flags.ignoreOnBattery, "ignore-on-battery", flags.ignoreOnBattery, "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.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "profile":
name = "name"

Check warning on line 120 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L119-L120

Added lines #L119 - L120 were not covered by tests
}
return pflag.NormalizedName(name)
})

if platform.IsWindows() {
// flag for internal use only
flagset.BoolVar(&flags.isChild, constants.FlagAsChild, false, "run as an elevated user child process")
Expand Down Expand Up @@ -148,27 +156,50 @@

// parse first positional argument as <profile>.<command> if the profile was not set via name
nameFlag := flagset.Lookup("name")
if (nameFlag == nil || !nameFlag.Changed) && strings.Contains(flags.resticArgs[0], ".") {
// split first argument at `.`
profileAndCommand := strings.Split(flags.resticArgs[0], ".")
// last element will be used as restic command
command := profileAndCommand[len(profileAndCommand)-1]
// remaining elements will be stiched together with `.` and used as profile name
profile := strings.Join(profileAndCommand[0:len(profileAndCommand)-1], ".")

// set command
if len(command) == 0 {
// take default command by removing it from resticArgs
flags.resticArgs = flags.resticArgs[1:]
} else {
flags.resticArgs[0] = command
}
if nameFlag == nil || !nameFlag.Changed {
if strings.Contains(flags.resticArgs[0], ".") {
// split first argument at `.`
profileAndCommand := strings.Split(flags.resticArgs[0], ".")
// last element will be used as restic command
command := profileAndCommand[len(profileAndCommand)-1]
// remaining elements will be stiched together with `.` and used as profile name
profile := strings.Join(profileAndCommand[0:len(profileAndCommand)-1], ".")

// set command
if len(command) == 0 {
// take default command by removing it from resticArgs
flags.resticArgs = flags.resticArgs[1:]
} else {
flags.resticArgs[0] = command
}

// set profile
if len(profile) == 0 {
profile = constants.DefaultProfileName
// set profile
if len(profile) == 0 {
profile = constants.DefaultProfileName
}
flags.name = profile
} else if strings.Contains(flags.resticArgs[0], "@") {
// split first argument at `@`
commandAndProfile := strings.Split(flags.resticArgs[0], "@")

Check warning on line 183 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L183

Added line #L183 was not covered by tests
// first element will be used as restic command
profile := commandAndProfile[0]

Check warning on line 185 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L185

Added line #L185 was not covered by tests
// last element will be used as profile name
command := commandAndProfile[len(commandAndProfile)-1]

Check warning on line 187 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L187

Added line #L187 was not covered by tests

// set command
if len(command) == 0 {

Check warning on line 190 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L190

Added line #L190 was not covered by tests
// take default command by removing it from resticArgs
flags.resticArgs = flags.resticArgs[1:]
} else {
flags.resticArgs[0] = command

Check warning on line 194 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L192-L194

Added lines #L192 - L194 were not covered by tests
}

// set profile
if len(profile) == 0 {
profile = constants.DefaultProfileName

Check warning on line 199 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L198-L199

Added lines #L198 - L199 were not covered by tests
}
flags.name = profile

Check warning on line 201 in flags.go

View check run for this annotation

Codecov / codecov/patch

flags.go#L201

Added line #L201 was not covered by tests
}
flags.name = profile
}

return flagset, flags, nil
Expand Down
159 changes: 79 additions & 80 deletions flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -115,84 +116,82 @@ func TestEnvOverridesError(t *testing.T) {
assert.Contains(t, mem.Logs(), `cannot convert env variable RESTICPROFILE_LOCK_WAIT="no-valid-duration": time: invalid duration "no-valid-duration"`)
}

func TestProfileCommandWithProfileNamePrecedence(t *testing.T) {
_, flags, err := loadFlags([]string{"-n", "profile2", "-v", "some.command"})
require.NoError(t, err)
assert.Equal(t, flags.name, "profile2")
assert.True(t, flags.verbose)
assert.Equal(t, flags.resticArgs, []string{"some.command"})
}

func TestProfileCommandWithProfileNamePrecedenceWithDefaultProfile(t *testing.T) {
_, flags, err := loadFlags([]string{"-n", constants.DefaultProfileName, "-v", "some.other.command"})
require.NoError(t, err)
assert.Equal(t, flags.name, constants.DefaultProfileName)
assert.True(t, flags.verbose)
assert.Equal(t, flags.resticArgs, []string{"some.other.command"})
}

func TestProfileCommandWithResticVerbose(t *testing.T) {
_, flags, err := loadFlags([]string{"profile1.check", "--", "-v"})
require.NoError(t, err)
assert.False(t, flags.help)
assert.Equal(t, flags.name, "profile1")
assert.False(t, flags.verbose)
assert.Equal(t, flags.resticArgs, []string{"check", "--", "-v"})
}

func TestProfileCommandWithResticprofileVerbose(t *testing.T) {
_, flags, err := loadFlags([]string{"-v", "pro.file1.backup"})
require.NoError(t, err)
assert.True(t, flags.verbose)
assert.Equal(t, flags.name, "pro.file1")
assert.Equal(t, flags.resticArgs, []string{"backup"})
}

func TestProfileCommandThreePart(t *testing.T) {
_, flags, err := loadFlags([]string{"bla.foo.backup"})
require.NoError(t, err)
assert.Equal(t, flags.name, "bla.foo")
assert.Equal(t, flags.resticArgs, []string{"backup"})
}

func TestProfileCommandTwoPartCommandMissing(t *testing.T) {
_, flags, err := loadFlags([]string{"bar."})
require.NoError(t, err)
assert.Equal(t, flags.name, "bar")
assert.Equal(t, flags.resticArgs, []string{})
}

func TestProfileCommandTwoPartProfileMissing(t *testing.T) {
_, flags, err := loadFlags([]string{".baz"})
require.NoError(t, err)
assert.Equal(t, flags.name, constants.DefaultProfileName)
assert.Equal(t, flags.resticArgs, []string{"baz"})
}

func TestProfileCommandTwoPartDotPrefix(t *testing.T) {
_, flags, err := loadFlags([]string{".baz.qux"})
require.NoError(t, err)
assert.Equal(t, flags.name, ".baz")
assert.Equal(t, flags.resticArgs, []string{"qux"})
}

func TestProfileCommandThreePartCommandMissing(t *testing.T) {
_, flags, err := loadFlags([]string{"quux.quuz."})
require.NoError(t, err)
assert.Equal(t, flags.name, "quux.quuz")
assert.Equal(t, flags.resticArgs, []string{})
}

func TestProfileCommandTwoPartDotPrefixCommandMissing(t *testing.T) {
_, flags, err := loadFlags([]string{".corge."})
require.NoError(t, err)
assert.Equal(t, flags.name, ".corge")
assert.Equal(t, flags.resticArgs, []string{})
}

func TestProfileCommandTwoPartProfileMissingCommandMissing(t *testing.T) {
_, flags, err := loadFlags([]string{"."})
require.NoError(t, err)
assert.Equal(t, flags.name, constants.DefaultProfileName)
assert.Equal(t, flags.resticArgs, []string{})
func TestCommandProfile(t *testing.T) {
t.Parallel()
testCases := []struct {
sourceArgs []string
expectedProfile string
expectedArgs []string
expectedVerbose bool
}{
{
sourceArgs: []string{"."},
expectedProfile: constants.DefaultProfileName,
expectedArgs: []string{},
},
{
sourceArgs: []string{".baz"},
expectedProfile: constants.DefaultProfileName,
expectedArgs: []string{"baz"},
},
{
sourceArgs: []string{"bar."},
expectedProfile: "bar",
expectedArgs: []string{},
},
{
sourceArgs: []string{".corge."},
expectedProfile: ".corge",
expectedArgs: []string{},
},
{
sourceArgs: []string{"quux.quuz."},
expectedProfile: "quux.quuz",
expectedArgs: []string{},
},
{
sourceArgs: []string{".baz.qux"},
expectedProfile: ".baz",
expectedArgs: []string{"qux"},
},
{
sourceArgs: []string{"bla.foo.backup"},
expectedProfile: "bla.foo",
expectedArgs: []string{"backup"},
},
{
sourceArgs: []string{"-v", "pro.file1.backup"},
expectedProfile: "pro.file1",
expectedArgs: []string{"backup"},
expectedVerbose: true,
},
{
sourceArgs: []string{"profile1.check", "--", "-v"},
expectedProfile: "profile1",
expectedArgs: []string{"check", "--", "-v"},
expectedVerbose: false,
},
{
sourceArgs: []string{"-n", constants.DefaultProfileName, "-v", "some.other.command"},
expectedProfile: constants.DefaultProfileName,
expectedArgs: []string{"some.other.command"},
expectedVerbose: true,
},
{
sourceArgs: []string{"-n", "profile2", "-v", "some.command"},
expectedProfile: "profile2",
expectedArgs: []string{"some.command"},
expectedVerbose: true,
},
}
for _, testCase := range testCases {
t.Run(strings.Join(testCase.sourceArgs, " "), func(t *testing.T) {
t.Parallel()
_, flags, err := loadFlags(testCase.sourceArgs)
require.NoError(t, err)
assert.Equal(t, flags.name, testCase.expectedProfile)
assert.Equal(t, flags.resticArgs, testCase.expectedArgs)
assert.Equal(t, flags.verbose, testCase.expectedVerbose)
})
}
creativeprojects marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/creativeprojects/resticprofile

go 1.21
go 1.22

require (
github.com/Masterminds/semver/v3 v3.2.1
Expand Down