|
| 1 | +// Copyright 2022 The Falco Authors |
| 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 cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/spf13/cobra" |
| 19 | + |
| 20 | + "github.com/falcosecurity/falcoctl/pkg/oci" |
| 21 | + "github.com/falcosecurity/falcoctl/pkg/oci/authn" |
| 22 | + commonoptions "github.com/falcosecurity/falcoctl/pkg/options" |
| 23 | +) |
| 24 | + |
| 25 | +type logoutOptions struct { |
| 26 | + *commonoptions.CommonOptions |
| 27 | + hostname string |
| 28 | +} |
| 29 | + |
| 30 | +// Validate validates the `list` command options. |
| 31 | +func (o *logoutOptions) Validate(args []string) error { |
| 32 | + if len(args) != 0 { |
| 33 | + o.hostname = args[0] |
| 34 | + } else { |
| 35 | + o.hostname = oci.DefaultRegistry |
| 36 | + } |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +// NewLogoutCmd returns the logout command. |
| 41 | +func NewLogoutCmd(opt *commonoptions.CommonOptions) *cobra.Command { |
| 42 | + o := logoutOptions{ |
| 43 | + CommonOptions: opt, |
| 44 | + } |
| 45 | + |
| 46 | + cmd := &cobra.Command{ |
| 47 | + Use: "logout hostname", |
| 48 | + DisableFlagsInUseLine: true, |
| 49 | + Short: "Logout from an OCI registry", |
| 50 | + Long: "Logout from an OCI registry", |
| 51 | + Args: cobra.MaximumNArgs(1), |
| 52 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 53 | + o.Printer.CheckErr(o.Validate(args)) |
| 54 | + }, |
| 55 | + Run: func(cmd *cobra.Command, args []string) { |
| 56 | + o.Printer.CheckErr(o.RunLogout(args)) |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + return cmd |
| 61 | +} |
| 62 | + |
| 63 | +func (o *logoutOptions) RunLogout(args []string) error { |
| 64 | + err := authn.Logout(o.hostname) |
| 65 | + if err != nil { |
| 66 | + o.Printer.Error.Println(err.Error()) |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + o.Printer.DefaultText.Println("Logout succeeded") |
| 71 | + return nil |
| 72 | +} |
0 commit comments