From bb8f0d581f954d412092a293a45dda295b800cae Mon Sep 17 00:00:00 2001 From: "pavel.larkin" Date: Tue, 6 Feb 2024 17:23:59 -0800 Subject: [PATCH] Add kopia CLI repository connect server command --- pkg/kopia/cli/internal/command/commands.go | 1 + .../repository/repository_connect_server.go | 55 +++++++++++++++++++ pkg/kopia/cli/repository/repository_test.go | 38 +++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 pkg/kopia/cli/repository/repository_connect_server.go diff --git a/pkg/kopia/cli/internal/command/commands.go b/pkg/kopia/cli/internal/command/commands.go index fb54d5dbd2..f5d6ce2a5c 100644 --- a/pkg/kopia/cli/internal/command/commands.go +++ b/pkg/kopia/cli/internal/command/commands.go @@ -24,6 +24,7 @@ var ( Repository = Command{"repository"} Create = Command{"create"} Connect = Command{"connect"} + Server = Command{"server"} ) // Repository storage sub commands. diff --git a/pkg/kopia/cli/repository/repository_connect_server.go b/pkg/kopia/cli/repository/repository_connect_server.go new file mode 100644 index 0000000000..8286d6d008 --- /dev/null +++ b/pkg/kopia/cli/repository/repository_connect_server.go @@ -0,0 +1,55 @@ +// 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 repository + +import ( + "github.com/kanisterio/safecli" + + "github.com/kanisterio/kanister/pkg/log" + + "github.com/kanisterio/kanister/pkg/kopia/cli" + "github.com/kanisterio/kanister/pkg/kopia/cli/internal/command" + flagcommon "github.com/kanisterio/kanister/pkg/kopia/cli/internal/flag/common" + flagrepo "github.com/kanisterio/kanister/pkg/kopia/cli/internal/flag/repository" +) + +// ConnectServerArgs defines the arguments for the `kopia repository connect server` command. +type ConnectServerArgs struct { + cli.CommonArgs + cli.CacheArgs + + Hostname string // hostname of the repository + Username string // username of the repository + ServerURL string // URL of the Kopia Repository API server + Fingerprint string // fingerprint of the server's TLS certificate + ReadOnly bool // connect to a repository in read-only mode + + Logger log.Logger +} + +// ConnectServer creates a new `kopia repository connect server...` command. +func ConnectServer(args ConnectServerArgs) (safecli.CommandBuilder, error) { + return command.NewKopiaCommandBuilder(args.CommonArgs, + command.Repository, command.Connect, command.Server, + flagcommon.NoCheckForUpdates, + flagcommon.NoGRPC, + flagcommon.ReadOnly(args.ReadOnly), + flagcommon.Cache(args.CacheArgs), + flagrepo.Hostname(args.Hostname), + flagrepo.Username(args.Username), + flagrepo.ServerURL(args.ServerURL), + flagrepo.ServerCertFingerprint(args.Fingerprint), + ) +} diff --git a/pkg/kopia/cli/repository/repository_test.go b/pkg/kopia/cli/repository/repository_test.go index ace602952b..76b3418bc4 100644 --- a/pkg/kopia/cli/repository/repository_test.go +++ b/pkg/kopia/cli/repository/repository_test.go @@ -343,3 +343,41 @@ var _ = check.Suite(test.NewCommandSuite([]test.CommandTest{ }, }, })) + +// Test Repository Connect Server command +var _ = check.Suite(test.NewCommandSuite([]test.CommandTest{ + { + Name: "repository connect server", + CLI: func() (safecli.CommandBuilder, error) { + args := ConnectServerArgs{ + CommonArgs: test.CommonArgs, + CacheArgs: cacheArgs, + Hostname: "test-hostname", + Username: "test-username", + ServerURL: "http://test-server", + Fingerprint: "test-fingerprint", + ReadOnly: true, + } + return ConnectServer(args) + }, + ExpectedCLI: []string{"kopia", + "--config-file=path/kopia.config", + "--log-level=error", + "--log-dir=cache/log", + "--password=encr-key", + "repository", + "connect", + "server", + "--no-check-for-updates", + "--no-grpc", + "--readonly", + "--cache-directory=/tmp/cache.dir", + "--content-cache-size-limit-mb=0", + "--metadata-cache-size-limit-mb=0", + "--override-hostname=test-hostname", + "--override-username=test-username", + "--url=http://test-server", + "--server-cert-fingerprint=test-fingerprint", + }, + }, +}))