-
Notifications
You must be signed in to change notification settings - Fork 199
Add scanner cmd
#396
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
Merged
Merged
Add scanner cmd
#396
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b27a676
Added `scanner create` command.
muaz-32 6145a99
Improved the `scanner create` command. It tests the connection before…
muaz-32 011fbee
Update pkg/api/scanner_handler.go
muaz-32 1164f30
Added the code review suggestions.
muaz-32 2c319c5
Added the `view` command for scanner.
muaz-32 0ca2656
Added the `metadata` command for scanner.
muaz-32 a8ea7e8
Added the `set-default` command for scanner.
muaz-32 e8e2199
Added the `delete` command for scanner.
muaz-32 136a8d5
Added the `update` command for scanner and fixed the `create` command…
muaz-32 eececca
Added the `ping` option while creating scanner to mimic the web UI in…
muaz-32 7746681
Merge branch 'main' into scanner-cmd
rizul2108 110474f
add header and gofmt
rizul2108 65c85ef
add docs
rizul2108 927ac5b
lint fixes
rizul2108 ae372f0
lint fixes
rizul2108 ac5c0d7
update list cmd
rizul2108 63f05f1
add header
rizul2108 83445a5
improve ui
rizul2108 a904e2c
fix lint issues
rizul2108 c6492d6
Update pkg/views/scanner/list/view.go
rizul2108 0b47ef2
Update cmd/harbor/root/cmd.go
rizul2108 9b3cf54
fix: implement changes and apply lint fixes
rizul2108 1dddf42
Merge branch 'main' into scanner-cmd
rizul2108 087781c
Merge branch 'main' into scanner-cmd
rizul2108 3e15892
implement changes and update docs
rizul2108 c4b5543
small fix
rizul2108 5a4e255
Merge branch 'main' into scanner-cmd
rizul2108 d5b1ecc
Merge branch 'main' into scanner-cmd
bupd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import "github.com/spf13/cobra" | ||
|
|
||
| func Scanner() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "scanner", | ||
| Short: "scanner commands", | ||
| } | ||
|
|
||
| cmd.AddCommand( | ||
| CreateScannerCommand(), | ||
| ListScannerCommand(), | ||
| ViewCommand(), | ||
| MetadataCommand(), | ||
| SetDefaultCommand(), | ||
| DeleteCommand(), | ||
| UpdateCommand(), | ||
| ) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import ( | ||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/views/scanner/create" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func CreateScannerCommand() *cobra.Command { | ||
| var opts create.CreateView | ||
| var ping bool | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create a scanner", | ||
| Args: cobra.NoArgs, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| if opts.Name == "" || opts.Description == "" || opts.Auth == "" || opts.AccessCredential == "" || opts.URL == "" { | ||
| create.CreateScannerView(&opts) | ||
| } | ||
|
|
||
| if ping { | ||
| err := api.PingScanner(opts) | ||
| if err != nil { | ||
| log.Errorf("failed to ping the scanner adapter: %v", err) | ||
| } | ||
| } else { | ||
| err := api.CreateScanner(opts) | ||
| if err != nil { | ||
| log.Errorf("failed to create scanner: %v", err.Error()) | ||
| } | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVarP(&opts.Name, "name", "", "", "Name of the scanner") | ||
| flags.StringVarP(&opts.Description, "des", "", "", "Description of the scanner") | ||
| flags.StringVarP(&opts.Auth, "auth", "", "", "Authentication approach of the scanner [None|Basic|Bearer|X-ScannerAdapter-API-Key]") | ||
| flags.StringVarP(&opts.AccessCredential, "cred", "", "", "HTTP Authorization header value sent with each request to the Scanner Adapter API") | ||
| flags.StringVarP(&opts.URL, "url", "", "", "Base URL of the scanner adapter") | ||
| flags.BoolVarP(&opts.Disabled, "disable", "", false, "Indicate whether the registration is enabled or not") | ||
| flags.BoolVarP(&opts.SkipCertVerify, "skip", "", false, "Indicate if skip the certificate verification when sending HTTP requests") | ||
| flags.BoolVarP(&opts.UseInternalAddr, "internal", "", false, "Indicate whether use internal registry addr for the scanner to pull content or not") | ||
| flags.BoolVarP(&ping, "ping", "", false, "Ping the scanner adapter without creating it.") | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import ( | ||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/prompt" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func DeleteCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "delete", | ||
| Short: "delete scanner", | ||
| Args: cobra.MaximumNArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| var err error | ||
| var registrationID string | ||
| if len(args) > 0 { | ||
| registrationID = args[0] | ||
| } else { | ||
| registrationID = prompt.GetScannerIdFromUser() | ||
| } | ||
|
rizul2108 marked this conversation as resolved.
Outdated
|
||
| err = api.DeleteScanner(registrationID) | ||
| if err != nil { | ||
| log.Errorf("failed to delete scanner: %v", err) | ||
| } else { | ||
| log.Infof("scanner deleted successfully") | ||
| } | ||
| }, | ||
| } | ||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/utils" | ||
| list "github.com/goharbor/harbor-cli/pkg/views/scanner/list" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func ListScannerCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List scanners", | ||
| Args: cobra.ExactArgs(0), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| scannersResp, err := api.ListScanners() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list scanners: %v", err) | ||
| } | ||
|
|
||
| scanners := scannersResp.Payload | ||
| if len(scanners) == 0 { | ||
| log.Info("No scanners found") | ||
| return nil | ||
| } | ||
|
|
||
| formatFlag := viper.GetString("output-format") | ||
| if formatFlag != "" { | ||
| err = utils.PrintFormat(scanners, formatFlag) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| list.ListScanners(scanners) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/prompt" | ||
| "github.com/goharbor/harbor-cli/pkg/utils" | ||
| "github.com/goharbor/harbor-cli/pkg/views/scanner/metadata" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func MetadataCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "metadata [scanner-id]", | ||
| Short: "Get scanner metadata by ID", | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| var registrationID string | ||
| if len(args) > 0 { | ||
| registrationID = args[0] | ||
| } else { | ||
| registrationID = prompt.GetScannerIdFromUser() | ||
| } | ||
|
|
||
| meta, err := api.GetScannerMetadata(registrationID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get scanner metadata: %v", err) | ||
| } | ||
|
|
||
| formatFlag := viper.GetString("output-format") | ||
| if formatFlag != "" { | ||
| err = utils.PrintFormat(meta, formatFlag) | ||
| if err != nil { | ||
| return err | ||
|
rizul2108 marked this conversation as resolved.
|
||
| } | ||
| } else { | ||
| metadata.DisplayScannerMetadata(meta.Payload) | ||
|
rizul2108 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import ( | ||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/prompt" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func SetDefaultCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "set-default", | ||
| Short: "set default scanner", | ||
| Aliases: []string{"sd"}, | ||
| Args: cobra.MaximumNArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| var err error | ||
| var registrationID string | ||
| if len(args) > 0 { | ||
| registrationID = args[0] | ||
| } else { | ||
| registrationID = prompt.GetScannerIdFromUser() | ||
| } | ||
|
rizul2108 marked this conversation as resolved.
|
||
| err = api.SetDefaultScanner(registrationID) | ||
| if err != nil { | ||
| log.Errorf("failed to set default scanner: %v", err) | ||
| } | ||
| }, | ||
| } | ||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright Project Harbor 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 scanner | ||
|
|
||
| import ( | ||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/prompt" | ||
| "github.com/goharbor/harbor-cli/pkg/views/scanner/create" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func UpdateCommand() *cobra.Command { | ||
|
rizul2108 marked this conversation as resolved.
|
||
| var opts create.CreateView | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "update", | ||
| Short: "update scanner", | ||
| Args: cobra.MaximumNArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| var err error | ||
| var registrationID string | ||
|
|
||
| if len(args) > 0 { | ||
| registrationID = args[0] | ||
| } else { | ||
| registrationID = prompt.GetScannerIdFromUser() | ||
| } | ||
|
|
||
| if opts.Name == "" || opts.Description == "" || opts.Auth == "" || opts.AccessCredential == "" || opts.URL == "" { | ||
| create.CreateScannerView(&opts) | ||
| } | ||
|
|
||
| err = api.UpdateScanner(registrationID, opts) | ||
|
|
||
| if err != nil { | ||
| log.Errorf("failed to update scanner: %v", err) | ||
| } else { | ||
| log.Infof("scanner updated successfully") | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVarP(&opts.Name, "name", "", "", "Name of the scanner") | ||
| flags.StringVarP(&opts.Description, "des", "", "", "Description of the scanner") | ||
| flags.StringVarP(&opts.Auth, "auth", "", "", "Authentication approach of the scanner [None|Basic|Bearer|X-ScannerAdapter-API-Key]") | ||
| flags.StringVarP(&opts.AccessCredential, "cred", "", "", "HTTP Authorization header value sent with each request to the Scanner Adapter API") | ||
| flags.StringVarP(&opts.URL, "url", "", "", "Base URL of the scanner adapter") | ||
| flags.BoolVarP(&opts.Disabled, "disable", "", false, "Indicate whether the registration is enabled or not") | ||
| flags.BoolVarP(&opts.SkipCertVerify, "skip", "", false, "Indicate if skip the certificate verification when sending HTTP requests") | ||
| flags.BoolVarP(&opts.UseInternalAddr, "internal", "", false, "Indicate whether use internal registry addr for the scanner to pull content or not") | ||
|
|
||
| return cmd | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.