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

Implement Kopia common args and opts #2654

Merged
merged 29 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
680e782
Add safecli dependency
plar Feb 3, 2024
72011e7
add new flag implementations based on the safecli package for the Kop…
plar Feb 3, 2024
0f635ad
apply go fmt
plar Feb 4, 2024
d0a6dd1
Add common Kopia args and flags
plar Feb 6, 2024
9850f4a
Fix Apply and test.Suit
plar Feb 12, 2024
246e1c1
Remove variadic args for Common and Cache flags
plar Feb 12, 2024
419652c
Merge branch 'kopia-cli-core-infra' into kopia-cli-common-flags
plar Feb 12, 2024
24707e5
pkg/kopia/cli/internal/flag is implemented in the safecli@v0.0.4 now
plar Feb 17, 2024
c029fc1
Merge branch 'master' into kopia-cli-core-infra
plar Feb 17, 2024
cff7220
Re-implement Kopia common args and opts; Sync with the master
plar Feb 17, 2024
550d124
Add pkg/kopia/cli package
plar Feb 17, 2024
fc918a0
go mod tidy
plar Feb 17, 2024
9530c91
Merge branch 'kopia-cli-core-infra' into kopia-cli-common-flags
plar Feb 17, 2024
2c6cb6e
Convert common flags from vars to funcs
plar Feb 26, 2024
1a3ee2d
Add safecli dependency
plar Feb 3, 2024
386e7e9
add new flag implementations based on the safecli package for the Kop…
plar Feb 3, 2024
b815f63
apply go fmt
plar Feb 4, 2024
bcafabb
Fix Apply and test.Suit
plar Feb 12, 2024
6d61bb4
pkg/kopia/cli/internal/flag is implemented in the safecli@v0.0.4 now
plar Feb 17, 2024
e6ddb8f
Add pkg/kopia/cli package
plar Feb 17, 2024
4e7ffd7
go mod tidy
plar Feb 17, 2024
f16aea7
Update safecli to v0.0.5
plar Feb 27, 2024
dcd6425
Update safecli to v0.0.6
plar Feb 28, 2024
d3dfb31
Sync with upstream
plar Feb 28, 2024
b093421
Fix tests
plar Feb 28, 2024
44c133e
Fix formatting
plar Feb 29, 2024
1897a1e
Merge branch 'kopia-cli-core-infra' into kopia-cli-common-flags
plar Feb 29, 2024
33b1342
organize imports
plar Feb 29, 2024
8baab6d
Merge branch 'master' into kopia-cli-common-flags
pavannd1 Mar 5, 2024
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

require github.com/kanisterio/safecli v0.0.3

require (
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kanisterio/safecli v0.0.3 h1:ts3oRVSoRexFBv9pOdWYZsN4k068pWx5Cl4zN54mQro=
github.com/kanisterio/safecli v0.0.3/go.mod h1:fK3Mcbeiso+NtkUdhGugK0Vf4S2l8ObvFf564ry1W5A=
github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734 h1:qulsCaCv+O2y9/sQ9nd5KChnAgFOWakTHQ9ZADjs6DQ=
github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734/go.mod h1:rdqSnvOJuKCPFW/h2rVLuXOAkRnHHdp9PZcKx4HCoDM=
github.com/kastenhq/stow v0.2.6-kasten.1.0.20231101232131-9321daa23aae h1:2cl4yuAJpdmLCx7G8eIsfNlQBLEfw0JDj6mTTyqc5qg=
Expand Down
34 changes: 34 additions & 0 deletions pkg/kopia/cli/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

// The common arguments for Kopia CLI.

// CommonArgs provides the common arguments for Kopia CLI.
type CommonArgs struct {
ConfigFilePath string // the path to the config file.
LogDirectory string // the directory where logs are stored.
LogLevel string // the level of logging. Default is "error".
RepoPassword string // the password for the repository.
}

// CacheArgs provides the cache arguments for Kopia CLI.
type CacheArgs struct {
CacheDirectory string // the directory where cache is stored. Default is "/tmp/kopia-cache".
ContentCacheSizeMB int // the size of the content cache in MB.
ContentCacheSizeLimitMB int // the maximum size of the content cache in MB.
MetadataCacheSizeMB int // the size of the metadata cache in MB.
MetadataCacheSizeLimitMB int // the maximum size of the metadata cache in MB.
}
31 changes: 31 additions & 0 deletions pkg/kopia/cli/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"github.com/pkg/errors"
)

// flag errors
var (
// ErrInvalidFlag is returned when the flag name is empty.
ErrInvalidFlag = errors.New("invalid flag")
// ErrInvalidCommonArgs is returned when the common flag expects at most one cli.CommonArgs argument.
ErrInvalidCommonArgs = errors.New("common flag expects at most one cli.CommonArgs argument")
// ErrInvalidCacheArgs is returned when the cache flag expects at most one cli.CacheArgs argument.
ErrInvalidCacheArgs = errors.New("cache flag expects at most one cli.CacheArgs argument")
// ErrInvalidID is returned when the ID is empty.
ErrInvalidID = errors.New("invalid ID")
)
45 changes: 45 additions & 0 deletions pkg/kopia/cli/internal/flag/bool_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flag

import (
"github.com/kanisterio/safecli"

"github.com/kanisterio/kanister/pkg/kopia/cli"
)

// boolFlag defines a boolean flag with a given flag name.
// If enabled is set to true, the flag is applied; otherwise, it is not.
type boolFlag struct {
flag string
enabled bool
}

// Apply appends the flag to the command if the flag is enabled.
func (f boolFlag) Apply(cli safecli.CommandAppender) error {
if f.enabled {
cli.AppendLoggable(f.flag)
}
return nil
}

// NewBoolFlag creates a new bool flag with a given flag name.
// If the flag name is empty, cli.ErrInvalidFlag is returned.
func NewBoolFlag(flag string, enabled bool) Applier {
if flag == "" {
return ErrorFlag(cli.ErrInvalidFlag)
}
return boolFlag{flag, enabled}
}
205 changes: 205 additions & 0 deletions pkg/kopia/cli/internal/flag/common/common_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

import (
"strconv"

"github.com/kanisterio/safecli"

"github.com/kanisterio/kanister/pkg/kopia/cli"
"github.com/kanisterio/kanister/pkg/kopia/cli/internal/flag"
)

// Flags without conditions which applied to different kopia commands.
var (
All = flag.NewBoolFlag("--all", true)
Delta = flag.NewBoolFlag("--delta", true)
ShowIdentical = flag.NewBoolFlag("--show-identical", true)
NoGRPC = flag.NewBoolFlag("--no-grpc", true)
plar marked this conversation as resolved.
Show resolved Hide resolved
)

// predefined flags
var (
CheckForUpdates = checkForUpdates{CheckForUpdates: true}
hairyhum marked this conversation as resolved.
Show resolved Hide resolved
NoCheckForUpdates = checkForUpdates{CheckForUpdates: false}
)

// flag defaults
var (
defaultLogLevel = "error"
defaultCacheDirectory = "/tmp/kopia-cache"
)

// LogDirectory creates a new log directory flag with a given directory.
func LogDirectory(dir string) flag.Applier {
return flag.NewStringFlag("--log-dir", dir)
}

// LogLevel creates a new log level flag with a given level.
// If the level is empty, the default log level is used.
func LogLevel(level string) flag.Applier {
if level == "" {
level = defaultLogLevel
}
return flag.NewStringFlag("--log-level", level)
}

// CacheDirectory creates a new cache directory flag with a given directory.
// If the directory is empty, the default cache directory is used.
func CacheDirectory(dir string) flag.Applier {
if dir == "" {
dir = defaultCacheDirectory
}
return flag.NewStringFlag("--cache-directory", dir)
}

// ConfigFilePath creates a new config file path flag with a given path.
func ConfigFilePath(path string) flag.Applier {
return flag.NewStringFlag("--config-file", path)
}

// RepoPassword creates a new repository password flag with a given password.
func RepoPassword(password string) flag.Applier {
return flag.NewRedactedStringFlag("--password", password)
}

// checkForUpdates is the flag for checking for updates.
type checkForUpdates struct {
CheckForUpdates bool
}

func (f checkForUpdates) Flag() string {
if f.CheckForUpdates {
return "--check-for-updates"
}
return "--no-check-for-updates"
}

func (f checkForUpdates) Apply(cli safecli.CommandAppender) error {
hairyhum marked this conversation as resolved.
Show resolved Hide resolved
cli.AppendLoggable(f.Flag())
return nil
}

// ReadOnly creates a new read only flag.
func ReadOnly(readOnly bool) flag.Applier {
return flag.NewBoolFlag("--readonly", readOnly)
}

// ContentCacheSizeLimitMB creates a new content cache size flag with a given size.
func ContentCacheSizeLimitMB(size int) flag.Applier {
val := strconv.Itoa(size)
return flag.NewStringFlag("--content-cache-size-limit-mb", val)
}

// ContentCacheSizeMB creates a new content cache size flag with a given size.
func ContentCacheSizeMB(size int) flag.Applier {
val := strconv.Itoa(size)
return flag.NewStringFlag("--content-cache-size-mb", val)
}

// MetadataCacheSizeLimitMB creates a new metadata cache size flag with a given size.
func MetadataCacheSizeLimitMB(size int) flag.Applier {
val := strconv.Itoa(size)
return flag.NewStringFlag("--metadata-cache-size-limit-mb", val)
}

// MetadataCacheSizeMB creates a new metadata cache size flag with a given size.
func MetadataCacheSizeMB(size int) flag.Applier {
val := strconv.Itoa(size)
return flag.NewStringFlag("--metadata-cache-size-mb", val)
}

// common are the global flags for Kopia.
type common struct {
cli.CommonArgs
}

// Apply applies the global flags to the command.
func (f common) Apply(cmd safecli.CommandAppender) error {
return flag.Apply(cmd,
ConfigFilePath(f.ConfigFilePath),
LogLevel(f.LogLevel),
LogDirectory(f.LogDirectory),
RepoPassword(f.RepoPassword),
)
}

// Common creates a new common flag.
// If no arguments are provided, the default common flags are used.
// If one argument is provided, the common flags are used.
// If more than one argument is provided, ErrInvalidCommonArgs is returned.
func Common(args ...cli.CommonArgs) flag.Applier {
hairyhum marked this conversation as resolved.
Show resolved Hide resolved
switch len(args) {
case 0:
return common{cli.CommonArgs{}}
case 1:
return common{args[0]}
default:
return flag.ErrorFlag(cli.ErrInvalidCommonArgs)
}
}

// cache defines cache flags and implements Applier interface for the cache flags.
type cache struct {
cli.CacheArgs
}

// Apply applies the cache flags to the command.
func (f cache) Apply(cmd safecli.CommandAppender) error {
return flag.Apply(cmd,
CacheDirectory(f.CacheDirectory),
ContentCacheSizeLimitMB(f.ContentCacheSizeLimitMB),
MetadataCacheSizeLimitMB(f.MetadataCacheSizeLimitMB),
// ContentCacheSizeMB(f.ContentCacheSizeMB),
// MetadataCacheSizeMB(f.MetadataCacheSizeMB),
)
}

// Cache creates a new cache flag.
// If no arguments are provided, the default cache flags are used.
// If one argument is provided, the cache flags are used.
// If more than one argument is provided, ErrInvalidCacheArgs is returned.
func Cache(args ...cli.CacheArgs) flag.Applier {
hairyhum marked this conversation as resolved.
Show resolved Hide resolved
switch len(args) {
case 0:
return cache{cli.CacheArgs{}}
case 1:
return cache{args[0]}
default:
return flag.ErrorFlag(cli.ErrInvalidCacheArgs)
}
}

// JSONOutput creates a new JSON output flag.
func JSONOutput(enable bool) flag.Applier {
return flag.NewBoolFlag("--json", enable)
}

// JSON flag enables JSON output for different kopia commands.
var JSON = JSONOutput(true)

// Delete creates a new delete flag.
func Delete(enable bool) flag.Applier {
return flag.NewBoolFlag("--delete", enable)
}

// ID create the Kopia ID argument for different commands.
func ID(id string) flag.Applier {
if id == "" {
return flag.ErrorFlag(cli.ErrInvalidID)
}
return flag.NewStringArgument(id)
}
Loading