|
| 1 | +// Copyright 2020 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cli |
| 16 | + |
| 17 | +import ( |
| 18 | + atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" |
| 19 | + "github.com/mongodb/mongocli/internal/flags" |
| 20 | + "github.com/mongodb/mongocli/internal/json" |
| 21 | + "github.com/mongodb/mongocli/internal/store" |
| 22 | + "github.com/mongodb/mongocli/internal/usage" |
| 23 | + "github.com/spf13/cobra" |
| 24 | +) |
| 25 | + |
| 26 | +type atlasBackupsCheckpointsListOpts struct { |
| 27 | + *globalOpts |
| 28 | + clusterName string |
| 29 | + pageNum int |
| 30 | + itemsPerPage int |
| 31 | + store store.CheckpointsLister |
| 32 | +} |
| 33 | + |
| 34 | +func (opts *atlasBackupsCheckpointsListOpts) init() error { |
| 35 | + if opts.ProjectID() == "" { |
| 36 | + return errMissingProjectID |
| 37 | + } |
| 38 | + |
| 39 | + var err error |
| 40 | + opts.store, err = store.New() |
| 41 | + return err |
| 42 | +} |
| 43 | + |
| 44 | +func (opts *atlasBackupsCheckpointsListOpts) Run() error { |
| 45 | + listOpts := opts.newListOptions() |
| 46 | + |
| 47 | + result, err := opts.store.List(opts.ProjectID(), opts.clusterName, listOpts) |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return json.PrettyPrint(result) |
| 54 | +} |
| 55 | + |
| 56 | +func (opts *atlasBackupsCheckpointsListOpts) newListOptions() *atlas.ListOptions { |
| 57 | + return &atlas.ListOptions{ |
| 58 | + PageNum: opts.pageNum, |
| 59 | + ItemsPerPage: opts.itemsPerPage, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// mongocli atlas backup(s) checkpoint(s) list clusterName [--projectId projectId] |
| 64 | +func AtlasBackupsCheckpointsListBuilder() *cobra.Command { |
| 65 | + opts := &atlasBackupsCheckpointsListOpts{ |
| 66 | + globalOpts: newGlobalOpts(), |
| 67 | + } |
| 68 | + cmd := &cobra.Command{ |
| 69 | + Use: "list", |
| 70 | + Aliases: []string{"ls"}, |
| 71 | + Short: "List continuous backup checkpoints.", |
| 72 | + Args: cobra.ExactArgs(1), |
| 73 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 74 | + return opts.init() |
| 75 | + }, |
| 76 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 77 | + opts.clusterName = args[0] |
| 78 | + return opts.Run() |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page) |
| 83 | + cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit) |
| 84 | + |
| 85 | + cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) |
| 86 | + |
| 87 | + return cmd |
| 88 | +} |
0 commit comments